@@ -343,8 +343,11 @@ class RunCurrentCommand extends RunCommand implements Command {
343343 let selection = context . selectedText ;
344344 const activeBlock = context . blocks . find ( block => block . active ) ;
345345
346+ // idea: first check that we are in Positron
347+ // and leave the others untouched
348+
346349 // if the selection is empty and this isn't a knitr document then it resolves to run cell
347- if ( selection . length <= 0 && ! isKnitrDocument ( editor . document , this . engine_ ) ) {
350+ if ( false ) {
348351 if ( activeBlock ) {
349352 const executor = await this . cellExecutorForLanguage ( activeBlock . language , editor . document , this . engine_ ) ;
350353 if ( executor ) {
@@ -362,37 +365,46 @@ class RunCurrentCommand extends RunCommand implements Command {
362365 const vdoc = virtualDocForCode ( codeLines , embeddedLanguage ( activeBlock . language ) ! ) ;
363366 if ( vdoc ) {
364367 const parentUri = Uri . file ( editor . document . fileName ) ;
368+ const injectedLines = ( vdoc . language ?. inject ?. length ?? 0 )
369+
370+ const positionIntoVdoc = ( p : { line : number , character : number } ) =>
371+ new Position ( p . line + injectedLines , p . character )
372+ const positionOutOfVdoc = ( p : { line : number , character : number } ) =>
373+ new Position ( p . line - injectedLines , p . character )
374+
365375 const result = await withVirtualDocUri ( vdoc , parentUri , "statementRange" , async ( uri ) => {
366376 return await commands . executeCommand < StatementRange > (
367377 "vscode.executeStatementRangeProvider" ,
368378 uri ,
369- context . selection . start
379+ positionIntoVdoc ( context . selection . start )
370380 ) ;
371381 } ) ;
372- const { range : { start, end } } = result
373- const slicedLines = lines ( activeBlock . code ) . slice ( start . line , end . line + 1 )
374- slicedLines [ 0 ] = slicedLines [ 0 ] . slice ( start . character )
375- slicedLines [ slicedLines . length - 1 ] = slicedLines [ slicedLines . length - 1 ] . slice ( 0 , end . character )
382+ const { range, code } = result
383+ if ( code === undefined ) return
384+ const adjustedEnd = positionOutOfVdoc ( range . end )
376385
377- selection = slicedLines . join ( '\n' )
386+ selection = code
378387 action = "nextline" ;
379388
380389 // BEGIN ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
381390 // strategy from Positron using `StatementRangeProvider` to find range of next statement
382391 // and move cursor based on that.
383- if ( end . line + 1 <= codeLines . length ) {
392+ if ( adjustedEnd . line + 1 <= codeLines . length ) {
384393 const nextStatementRange = await withVirtualDocUri ( vdoc , parentUri , "statementRange" , async ( uri ) => {
385394 return await commands . executeCommand < StatementRange > (
386395 "vscode.executeStatementRangeProvider" ,
387396 uri ,
388- new Position ( end . line + 1 , 1 ) // look for statement at line after current statement
397+ positionIntoVdoc ( new Position ( adjustedEnd . line + 1 , 1 ) ) // look for statement at line after current statement
389398 ) ;
390399 } ) ;
391- const nextStatement = nextStatementRange . range ;
392- if ( nextStatement . start . line > end . line ) {
400+ const nextStatement = {
401+ start : positionOutOfVdoc ( nextStatementRange . range . start ) ,
402+ end : positionOutOfVdoc ( nextStatementRange . range . end )
403+ } ;
404+ if ( nextStatement . start . line > adjustedEnd . line ) {
393405 action = nextStatement . start
394406 // the nextStatement may start before & end after the current statement if e.g. inside a function:
395- } else if ( nextStatement . end . line > end . line ) {
407+ } else if ( nextStatement . end . line > adjustedEnd . line ) {
396408 action = nextStatement . end
397409 }
398410 }
0 commit comments