@@ -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 }
0 commit comments