Skip to content

Commit 7f35b63

Browse files
committed
fix advancing to next statement
1 parent c58152f commit 7f35b63

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

apps/vscode/src/providers/cell/commands.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,28 +374,26 @@ class RunCurrentCommand extends RunCommand implements Command {
374374
selection = slicedLines.join('\n')
375375
action = "nextline";
376376

377-
// ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
377+
// BEGIN ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
378+
// strategy from Positron using `StatementRangeProvider` to find range of next statement
379+
// and move cursor based on that.
378380
if (end.line + 1 <= codeLines.length) {
379381
const nextStatementRange = await withVirtualDocUri(vdoc, parentUri, "statementRange", async (uri) => {
380382
return await commands.executeCommand<StatementRange>(
381383
"vscode.executeStatementRangeProvider",
382384
uri,
383-
new Position(end.line + 1, 1)
385+
new Position(end.line + 1, 1) // look for statement at line after current statement
384386
);
385387
});
386388
const nextStatement = nextStatementRange.range;
387389
if (nextStatement.start.line > end.line) {
388-
// If the next statement's start is after this statement's end,
389-
// then move to the start of the next statement.
390390
action = nextStatement.start
391+
// the nextStatement may start before & end after the current statement if e.g. inside a function:
391392
} else if (nextStatement.end.line > end.line) {
392-
// If the above condition failed, but the next statement's end
393-
// is after this statement's end, assume we are exiting some
394-
// nested scope (like running an individual line of an R
395-
// function) and move to the end of the next statement.
396393
action = nextStatement.end
397394
}
398395
}
396+
// END ref.
399397
}
400398
}
401399

packages/editor-codemirror/src/behaviors/trackselection.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { DispatchEvent, codeViewCellContext, kCodeViewNextLineTransaction } from
2727
import { Behavior, BehaviorContext, State } from ".";
2828

2929
// track the selection in prosemirror
30-
export function trackSelectionBehavior(context: BehaviorContext) : Behavior {
30+
export function trackSelectionBehavior(context: BehaviorContext): Behavior {
3131

3232
let unsubscribe: VoidFunction;
3333

@@ -50,32 +50,32 @@ export function trackSelectionBehavior(context: BehaviorContext) : Behavior {
5050
unsubscribe = context.pmContext.events.subscribe(DispatchEvent, (tr: Transaction | undefined) => {
5151
if (tr) {
5252
// track selection changes that occur when we don't have focus
53-
if (!cmView.hasFocus && tr.selectionSet && !tr.docChanged && (tr.selection instanceof TextSelection)) {
53+
if (tr.selectionSet && !tr.docChanged && (tr.selection instanceof TextSelection)) {
5454
const cmSelection = asCodeMirrorSelection(context.view, cmView, context.getPos);
5555
context.withState(State.Updating, () => {
5656
if (cmSelection) {
5757
cmView.dispatch({ selection: cmSelection });
5858
} else {
59-
cmView.dispatch({ selection: EditorSelection.single(0)})
60-
}
59+
cmView.dispatch({ selection: EditorSelection.single(0) })
60+
}
6161
})
6262
} else if (tr.getMeta(kCodeViewNextLineTransaction) === true) {
6363
// NOTE: this is a special directive to advance to the next line. as distinct
6464
// from the block above it is not a reporting of a change in the PM selection
65-
// but rather an instruction to move the CM selection to the next line. as
65+
// but rather an instruction to move the CM selection to the next line. as
6666
// such we do not encose the code in State.Updating, because we want an update
6767
// to the PM selection to occur
6868
const cmSelection = asCodeMirrorSelection(context.view, cmView, context.getPos);
6969
if (cmSelection) {
7070
if (cursorLineDown(cmView)) {
7171
cursorLineStart(cmView);
72-
}
72+
}
7373
}
74-
// for other selection changes
74+
// for other selection changes
7575
} else if (cmView.hasFocus && tr.selectionSet && (tr.selection instanceof TextSelection)) {
7676
codeViewAssist();
7777
}
78-
}
78+
}
7979
});
8080
},
8181

@@ -91,7 +91,7 @@ export const asCodeMirrorSelection = (
9191
cmView: EditorView,
9292
getPos: (() => number) | boolean
9393
) => {
94-
if (typeof(getPos) === "function") {
94+
if (typeof (getPos) === "function") {
9595
const offset = getPos() + 1;
9696
const node = pmView.state.doc.nodeAt(getPos());
9797
if (node) {
@@ -104,8 +104,8 @@ export const asCodeMirrorSelection = (
104104
} else if (selection.from <= cmRange.from && selection.to >= cmRange.to) {
105105
return EditorSelection.single(0, cmView.state.doc.length);
106106
}
107-
107+
108108
}
109109
}
110110
return undefined;
111-
}
111+
}

packages/editor/src/api/codeview.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,17 @@ export function codeViewSetBlockSelection(
211211
if (typeof action === 'object') {
212212
// convert action line and character in code block space to pos in prosemirror space
213213
const block = context.blocks[activeIndex]
214+
// asummes the meta line looks like this:
215+
const metaLine = '{' + block.language + '}\n'
214216
const code = lines(block.code)
215217
if (action.line > code.length) throw 'trying to move cursor outside block!'
216-
let pos = block.pos
217-
for (let i = 0; i <= action.line; i++) {
218-
pos += code[i].length
218+
let pos = block.pos + metaLine.length
219+
for (let i = 0; i < action.line; i++) {
220+
pos += code[i].length + 1
219221
}
220222
pos += action.character
221223

222-
console.log('yoooo', pos, navigateToPos(view, pos, false));
224+
navigateToPos(view, pos, false)
223225
}
224226
else if (action === "nextline") {
225227
const tr = view.state.tr;

0 commit comments

Comments
 (0)