Skip to content

Commit c58152f

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 2dede3f commit c58152f

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
@@ -355,10 +355,10 @@ class RunCurrentCommand extends RunCommand implements Command {
355355
// if the selection is empty and we are in Positron:
356356
// try to get the statement's range and use that as the selection
357357
if (selection.length <= 0 && activeBlock && hasHooks()) {
358-
const language = embeddedLanguage(activeBlock.language);
359-
const vdoc = virtualDocForCode(lines(activeBlock.code), language!);
360-
const parentUri = Uri.file(editor.document.fileName);
358+
const codeLines = lines(activeBlock.code)
359+
const vdoc = virtualDocForCode(codeLines, embeddedLanguage(activeBlock.language)!);
361360
if (vdoc) {
361+
const parentUri = Uri.file(editor.document.fileName);
362362
const result = await withVirtualDocUri(vdoc, parentUri, "statementRange", async (uri) => {
363363
return await commands.executeCommand<StatementRange>(
364364
"vscode.executeStatementRangeProvider",
@@ -373,6 +373,29 @@ class RunCurrentCommand extends RunCommand implements Command {
373373

374374
selection = slicedLines.join('\n')
375375
action = "nextline";
376+
377+
// ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
378+
if (end.line + 1 <= codeLines.length) {
379+
const nextStatementRange = await withVirtualDocUri(vdoc, parentUri, "statementRange", async (uri) => {
380+
return await commands.executeCommand<StatementRange>(
381+
"vscode.executeStatementRangeProvider",
382+
uri,
383+
new Position(end.line + 1, 1)
384+
);
385+
});
386+
const nextStatement = nextStatementRange.range;
387+
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.
390+
action = nextStatement.start
391+
} 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.
396+
action = nextStatement.end
397+
}
398+
}
376399
}
377400
}
378401

@@ -389,8 +412,10 @@ class RunCurrentCommand extends RunCommand implements Command {
389412
await executeInteractive(executor, [selection], editor.document);
390413

391414
// advance cursor if necessary
415+
//
392416
if (action) {
393-
editor.setBlockSelection(context, "nextline");
417+
console.log('action!!!', action, this.id)
418+
await editor.setBlockSelection(context, action);
394419
}
395420
}
396421
}

packages/editor-types/src/codeview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface CodeViewActiveBlockContext {
3131
selectedText: string;
3232
}
3333

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

3636
export interface CodeViewCellContext {
3737
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)