Skip to content

Commit 9ea8e83

Browse files
committed
Add isPositron, semicolons
1 parent e944e72 commit 9ea8e83

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

apps/vscode/src/host/executors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export function executableLanguages() {
3232
return kCellExecutors.map((executor) => executor.language);
3333
}
3434

35+
// This function is always used by the `defaultExtensionHost`, and is used
36+
// by the `hooksExtensionHost` as a backup. Please see `hooksExtensionHost`
37+
// how executors are retrieved in Positron.
3538
export async function cellExecutorForLanguage(
3639
language: string,
3740
document: TextDocument,

apps/vscode/src/providers/cell/commands.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ import {
4848
executeSelectionInteractive,
4949
} from "./executors";
5050
import { ExtensionHost } from "../../host";
51-
import { hasHooks } from "../../host/hooks";
51+
import { tryAcquirePositronApi } from "@posit-dev/positron";
5252
import { isKnitrDocument } from "../../host/executors";
5353
import { commands } from "vscode";
5454
import { virtualDocForCode, withVirtualDocUri } from "../../vdoc/vdoc";
5555
import { embeddedLanguage } from "../../vdoc/languages";
5656
import { Uri } from "vscode";
5757
import { StatementRange } from "positron";
5858

59+
const isPositron = tryAcquirePositronApi();
60+
5961
export 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

271271
function 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

Comments
 (0)