@@ -48,14 +48,16 @@ import {
4848 executeSelectionInteractive ,
4949} from "./executors" ;
5050import { ExtensionHost } from "../../host" ;
51- import { hasHooks } from "../../host/hooks " ;
51+ import { tryAcquirePositronApi } from "@posit-dev/positron " ;
5252import { isKnitrDocument } from "../../host/executors" ;
5353import { commands } from "vscode" ;
5454import { virtualDocForCode , withVirtualDocUri } from "../../vdoc/vdoc" ;
5555import { embeddedLanguage } from "../../vdoc/languages" ;
5656import { Uri } from "vscode" ;
5757import { StatementRange } from "positron" ;
5858
59+ const isPositron = tryAcquirePositronApi ( ) ;
60+
5961export function cellCommands ( host : ExtensionHost , engine : MarkdownEngine ) : Command [ ] {
6062 return [
6163 new RunCurrentCommand ( host , engine ) ,
@@ -150,9 +152,7 @@ abstract class RunCommand {
150152 }
151153
152154 private async hasExecutorForLanguage ( language : string , document : TextDocument , engine : MarkdownEngine ) {
153- // TODO: this is incorrect right? `cellExecutorForLanguage` returns a promise, and a promise will always be truthy?
154- // We should have to await it before doing `!!`
155- return ! ! this . cellExecutorForLanguage ( language , document , engine ) ;
155+ return undefined !== ( await this . cellExecutorForLanguage ( language , document , engine ) ) ;
156156 }
157157
158158}
@@ -264,15 +264,15 @@ class RunPreviousCellCommand extends RunCommand implements Command {
264264}
265265
266266// More permissive type than `Position` so its easier to construct via a literal
267- type LineAndCharPos = { line : number , character : number }
267+ type LineAndCharPos = { line : number , character : number ; } ;
268268// More permissive type than `Range` so its easier to construct via a literal
269- type LineAndCharRange = { start : LineAndCharPos , end : LineAndCharPos }
269+ type LineAndCharRange = { start : LineAndCharPos , end : LineAndCharPos ; } ;
270270
271271function extractRangeFromCode ( code : string , range : LineAndCharRange ) : string {
272- const extractedRange = lines ( code ) . slice ( range . start . line , range . end . line + 1 )
273- extractedRange [ 0 ] = extractedRange [ 0 ] . slice ( range . start . character )
274- extractedRange [ extractedRange . length - 1 ] = extractedRange [ extractedRange . length - 1 ] . slice ( 0 , range . end . character )
275- return extractedRange . join ( '\n' )
272+ const extractedRange = lines ( code ) . slice ( range . start . line , range . end . line + 1 ) ;
273+ extractedRange [ 0 ] = extractedRange [ 0 ] . slice ( range . start . character ) ;
274+ extractedRange [ extractedRange . length - 1 ] = extractedRange [ extractedRange . length - 1 ] . slice ( 0 , range . end . character ) ;
275+ return extractedRange . join ( '\n' ) ;
276276}
277277
278278// Run the code at the cursor
@@ -308,7 +308,7 @@ class RunCurrentCommand extends RunCommand implements Command {
308308 const resolveToRunCell = editor . selection . isEmpty &&
309309 ! this . runSelection_ &&
310310 ! isKnitrDocument ( editor . document , this . engine_ ) &&
311- ( ! hasHooks ( ) && ( language === "python" || language === "r" ) ) ;
311+ ( ! isPositron && ( language === "python" || language === "r" ) ) ;
312312
313313 if ( resolveToRunCell ) {
314314 const code = codeWithoutOptionsFromBlock ( block ) ;
@@ -361,25 +361,25 @@ class RunCurrentCommand extends RunCommand implements Command {
361361 await executeInteractive ( executor , [ selection ] , editor . document ) ;
362362 await editor . setBlockSelection ( context , action ) ;
363363 }
364- }
364+ } ;
365365
366366 // if in Positron
367- if ( hasHooks ( ) ) {
367+ if ( isPositron ) {
368368 if ( activeBlock && selection . length <= 0 ) {
369- const codeLines = lines ( activeBlock . code )
369+ const codeLines = lines ( activeBlock . code ) ;
370370 const vdoc = virtualDocForCode ( codeLines , embeddedLanguage ( activeBlock . language ) ! ) ;
371371 if ( vdoc ) {
372372 const parentUri = Uri . file ( editor . document . fileName ) ;
373- const injectedLines = ( vdoc . language ?. inject ?. length ?? 0 )
373+ const injectedLines = ( vdoc . language ?. inject ?. length ?? 0 ) ;
374374
375375 const positionIntoVdoc = ( p : LineAndCharPos ) =>
376- new Position ( p . line + injectedLines , p . character )
376+ new Position ( p . line + injectedLines , p . character ) ;
377377 const positionOutOfVdoc = ( p : LineAndCharPos ) =>
378- new Position ( p . line - injectedLines , p . character )
378+ new Position ( p . line - injectedLines , p . character ) ;
379379 const rangeOutOfVdoc = ( r : Range ) : LineAndCharRange => ( {
380380 start : positionOutOfVdoc ( r . start ) ,
381381 end : positionOutOfVdoc ( r . end )
382- } )
382+ } ) ;
383383 const getStatementRange = async ( pos : LineAndCharPos ) => {
384384 const result = await withVirtualDocUri ( vdoc , parentUri , "statementRange" , async ( uri ) => {
385385 return await commands . executeCommand < StatementRange > (
@@ -388,29 +388,29 @@ class RunCurrentCommand extends RunCommand implements Command {
388388 positionIntoVdoc ( pos )
389389 ) ;
390390 } ) ;
391- return rangeOutOfVdoc ( result . range )
392- }
391+ return rangeOutOfVdoc ( result . range ) ;
392+ } ;
393393
394- const range = await getStatementRange ( context . selection . start )
395- const code = extractRangeFromCode ( activeBlock . code , range )
394+ const range = await getStatementRange ( context . selection . start ) ;
395+ const code = extractRangeFromCode ( activeBlock . code , range ) ;
396396
397397 // BEGIN ref: https://github.com/posit-dev/positron/blob/main/src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts#L428
398398 // strategy from Positron using `StatementRangeProvider` to find range of next statement
399399 // and move cursor based on that.
400400 if ( range . end . line + 1 <= codeLines . length ) {
401401 // get range of statement at line after current statement)
402- const nextRange = await getStatementRange ( new Position ( range . end . line + 1 , 1 ) )
402+ const nextRange = await getStatementRange ( new Position ( range . end . line + 1 , 1 ) ) ;
403403
404404 if ( nextRange . start . line > range . end . line ) {
405- exec ( nextRange . start , code )
405+ exec ( nextRange . start , code ) ;
406406 // the next statement range may start before & end after the current statement if e.g. inside a function:
407407 } else if ( nextRange . end . line > range . end . line ) {
408- exec ( nextRange . end , code )
408+ exec ( nextRange . end , code ) ;
409409 } else {
410- exec ( "nextline" , code )
410+ exec ( "nextline" , code ) ;
411411 }
412412 } else {
413- exec ( "nextline" , code )
413+ exec ( "nextline" , code ) ;
414414 }
415415 // END ref.
416416 }
@@ -428,9 +428,9 @@ class RunCurrentCommand extends RunCommand implements Command {
428428 }
429429 } else {
430430 if ( selection . length > 0 ) {
431- exec ( "nextline" , selection )
431+ exec ( "nextline" , selection ) ;
432432 } else if ( activeBlock ) { // if the selection is empty take the whole line as the selection
433- exec ( "nextline" , lines ( activeBlock . code ) [ context . selection . start . line ] )
433+ exec ( "nextline" , lines ( activeBlock . code ) [ context . selection . start . line ] ) ;
434434 }
435435 }
436436 }
0 commit comments