@@ -340,8 +340,11 @@ class RunCurrentCommand extends RunCommand implements Command {
340340 let selection = context . selectedText ;
341341 const activeBlock = context . blocks . find ( block => block . active ) ;
342342
343+ // idea: first check that we are in Positron
344+ // and leave the others untouched
345+
343346 // if the selection is empty and this isn't a knitr document then it resolves to run cell
344- if ( selection . length <= 0 && ! isKnitrDocument ( editor . document , this . engine_ ) ) {
347+ if ( false ) {
345348 if ( activeBlock ) {
346349 const executor = await this . cellExecutorForLanguage ( activeBlock . language , editor . document , this . engine_ ) ;
347350 if ( executor ) {
@@ -359,37 +362,46 @@ class RunCurrentCommand extends RunCommand implements Command {
359362 const vdoc = virtualDocForCode ( codeLines , embeddedLanguage ( activeBlock . language ) ! ) ;
360363 if ( vdoc ) {
361364 const parentUri = Uri . file ( editor . document . fileName ) ;
365+ const injectedLines = ( vdoc . language ?. inject ?. length ?? 0 )
366+
367+ const positionIntoVdoc = ( p : { line : number , character : number } ) =>
368+ new Position ( p . line + injectedLines , p . character )
369+ const positionOutOfVdoc = ( p : { line : number , character : number } ) =>
370+ new Position ( p . line - injectedLines , p . character )
371+
362372 const result = await withVirtualDocUri ( vdoc , parentUri , "statementRange" , async ( uri ) => {
363373 return await commands . executeCommand < StatementRange > (
364374 "vscode.executeStatementRangeProvider" ,
365375 uri ,
366- context . selection . start
376+ positionIntoVdoc ( context . selection . start )
367377 ) ;
368378 } ) ;
369- const { range : { start, end } } = result
370- const slicedLines = lines ( activeBlock . code ) . slice ( start . line , end . line + 1 )
371- slicedLines [ 0 ] = slicedLines [ 0 ] . slice ( start . character )
372- slicedLines [ slicedLines . length - 1 ] = slicedLines [ slicedLines . length - 1 ] . slice ( 0 , end . character )
379+ const { range, code } = result
380+ if ( code === undefined ) return
381+ const adjustedEnd = positionOutOfVdoc ( range . end )
373382
374- selection = slicedLines . join ( '\n' )
383+ selection = code
375384 action = "nextline" ;
376385
377386 // BEGIN ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
378387 // strategy from Positron using `StatementRangeProvider` to find range of next statement
379388 // and move cursor based on that.
380- if ( end . line + 1 <= codeLines . length ) {
389+ if ( adjustedEnd . line + 1 <= codeLines . length ) {
381390 const nextStatementRange = await withVirtualDocUri ( vdoc , parentUri , "statementRange" , async ( uri ) => {
382391 return await commands . executeCommand < StatementRange > (
383392 "vscode.executeStatementRangeProvider" ,
384393 uri ,
385- new Position ( end . line + 1 , 1 ) // look for statement at line after current statement
394+ positionIntoVdoc ( new Position ( adjustedEnd . line + 1 , 1 ) ) // look for statement at line after current statement
386395 ) ;
387396 } ) ;
388- const nextStatement = nextStatementRange . range ;
389- if ( nextStatement . start . line > end . line ) {
397+ const nextStatement = {
398+ start : positionOutOfVdoc ( nextStatementRange . range . start ) ,
399+ end : positionOutOfVdoc ( nextStatementRange . range . end )
400+ } ;
401+ if ( nextStatement . start . line > adjustedEnd . line ) {
390402 action = nextStatement . start
391403 // the nextStatement may start before & end after the current statement if e.g. inside a function:
392- } else if ( nextStatement . end . line > end . line ) {
404+ } else if ( nextStatement . end . line > adjustedEnd . line ) {
393405 action = nextStatement . end
394406 }
395407 }
0 commit comments