Skip to content

Commit 3a513f0

Browse files
committed
WIP fix advancing to next statement
I don't think my changes to `codeViewSetBlockSelection` work. It seems that `navigateToPos` does not work inside codeMirror? That must be why only "nextline" works in the command - it doesn't use `navigateToPos` it uses a special codeView thing.
1 parent 26098ab commit 3a513f0

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ class RunCurrentCommand extends RunCommand implements Command {
358358
// if the selection is empty and we are in Positron:
359359
// try to get the statement's range and use that as the selection
360360
if (selection.length <= 0 && activeBlock && hasHooks()) {
361-
const language = embeddedLanguage(activeBlock.language);
362-
const vdoc = virtualDocForCode(lines(activeBlock.code), language!);
363-
const parentUri = Uri.file(editor.document.fileName);
361+
const codeLines = lines(activeBlock.code)
362+
const vdoc = virtualDocForCode(codeLines, embeddedLanguage(activeBlock.language)!);
364363
if (vdoc) {
364+
const parentUri = Uri.file(editor.document.fileName);
365365
const result = await withVirtualDocUri(vdoc, parentUri, "statementRange", async (uri) => {
366366
return await commands.executeCommand<StatementRange>(
367367
"vscode.executeStatementRangeProvider",
@@ -376,6 +376,29 @@ class RunCurrentCommand extends RunCommand implements Command {
376376

377377
selection = slicedLines.join('\n')
378378
action = "nextline";
379+
380+
// ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
381+
if (end.line + 1 <= codeLines.length) {
382+
const nextStatementRange = await withVirtualDocUri(vdoc, parentUri, "statementRange", async (uri) => {
383+
return await commands.executeCommand<StatementRange>(
384+
"vscode.executeStatementRangeProvider",
385+
uri,
386+
new Position(end.line + 1, 1)
387+
);
388+
});
389+
const nextStatement = nextStatementRange.range;
390+
if (nextStatement.start.line > end.line) {
391+
// If the next statement's start is after this statement's end,
392+
// then move to the start of the next statement.
393+
action = nextStatement.start
394+
} else if (nextStatement.end.line > end.line) {
395+
// If the above condition failed, but the next statement's end
396+
// is after this statement's end, assume we are exiting some
397+
// nested scope (like running an individual line of an R
398+
// function) and move to the end of the next statement.
399+
action = nextStatement.end
400+
}
401+
}
379402
}
380403
}
381404

@@ -392,8 +415,10 @@ class RunCurrentCommand extends RunCommand implements Command {
392415
await executeInteractive(executor, [selection], editor.document);
393416

394417
// advance cursor if necessary
418+
//
395419
if (action) {
396-
editor.setBlockSelection(context, "nextline");
420+
console.log('action!!!', action, this.id)
421+
await editor.setBlockSelection(context, action);
397422
}
398423
}
399424
}

packages/editor-types/src/codeview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface CodeViewActiveBlockContext {
3434
selectedText: string;
3535
}
3636

37-
export type CodeViewSelectionAction = "nextline" | "nextblock" | "prevblock";
37+
export type CodeViewSelectionAction = "nextline" | "nextblock" | "prevblock" | { line: number, character: number };
3838

3939
export interface CodeViewCellContext {
4040
filepath: string;

packages/editor/src/api/codeview.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,23 @@ export function codeViewSetBlockSelection(
205205
context: CodeViewActiveBlockContext,
206206
action: CodeViewSelectionAction
207207
) {
208-
209-
210208
const activeIndex = context.blocks.findIndex(block => block.active);
211209

212210
if (activeIndex !== -1) {
213-
if (action === "nextline") {
211+
if (typeof action === 'object') {
212+
// convert action line and character in code block space to pos in prosemirror space
213+
const block = context.blocks[activeIndex]
214+
const code = lines(block.code)
215+
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
219+
}
220+
pos += action.character
221+
222+
console.log('yoooo', pos, navigateToPos(view, pos, false));
223+
}
224+
else if (action === "nextline") {
214225
const tr = view.state.tr;
215226
tr.setMeta(kCodeViewNextLineTransaction, true);
216227
view.dispatch(tr);
@@ -222,13 +233,11 @@ export function codeViewSetBlockSelection(
222233
navigatePos = context.blocks[activeIndex - 1]?.pos;
223234
}
224235
if (navigatePos) {
236+
console.log('yoooo22', navigatePos)
225237
navigateToPos(view, navigatePos!, false);
226238
}
227239
}
228240
}
229-
230-
231-
232241
}
233242

234243

0 commit comments

Comments
 (0)