Skip to content

Commit f6d1168

Browse files
committed
Need unadjustedRange() for returned statement range
1 parent ad2a87c commit f6d1168

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

apps/vscode/src/host/hooks.ts

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ import { ExtensionHost, HostWebviewPanel, HostStatementRangeProvider } from '.';
2020
import { CellExecutor, cellExecutorForLanguage, executableLanguages, isKnitrDocument, pythonWithReticulate } from './executors';
2121
import { ExecuteQueue } from './execute-queue';
2222
import { MarkdownEngine } from '../markdown/engine';
23-
import { virtualDoc, virtualDocUri, adjustedPosition } from "../vdoc/vdoc";
23+
import { virtualDoc, virtualDocUri, adjustedPosition, unadjustedRange } from "../vdoc/vdoc";
24+
import { EmbeddedLanguage } from '../vdoc/languages';
2425

2526
declare global {
26-
function acquirePositronApi() : hooks.PositronApi;
27+
function acquirePositronApi(): hooks.PositronApi;
2728
}
2829

29-
let api : hooks.PositronApi | null | undefined;
30+
let api: hooks.PositronApi | null | undefined;
3031

31-
export function hooksApi() : hooks.PositronApi | null {
32+
export function hooksApi(): hooks.PositronApi | null {
3233
if (api === undefined) {
3334
try {
3435
api = acquirePositronApi();
@@ -43,20 +44,20 @@ export function hasHooks() {
4344
return !!hooksApi();
4445
}
4546

46-
export function hooksExtensionHost() : ExtensionHost {
47+
export function hooksExtensionHost(): ExtensionHost {
4748
return {
4849
// supported executable languages (we delegate to the default for langugaes
4950
// w/o runtimes so we support all languages)
5051
executableLanguages,
5152

52-
cellExecutorForLanguage: async (language: string, document: vscode.TextDocument, engine: MarkdownEngine, silent?: boolean)
53+
cellExecutorForLanguage: async (language: string, document: vscode.TextDocument, engine: MarkdownEngine, silent?: boolean)
5354
: Promise<CellExecutor | undefined> => {
54-
switch(language) {
55+
switch (language) {
5556
// use hooks for known runtimes
5657
case "python":
5758
case "r":
5859
return {
59-
execute: async (blocks: string[], _editorUri?: vscode.Uri) : Promise<void> => {
60+
execute: async (blocks: string[], _editorUri?: vscode.Uri): Promise<void> => {
6061
const runtime = hooksApi()?.runtime;
6162

6263
if (runtime === undefined) {
@@ -68,7 +69,7 @@ export function hooksExtensionHost() : ExtensionHost {
6869
language = "r";
6970
blocks = blocks.map(pythonWithReticulate);
7071
}
71-
72+
7273
// Our callback executes each block sequentially
7374
const callback = async () => {
7475
for (const block of blocks) {
@@ -78,9 +79,9 @@ export function hooksExtensionHost() : ExtensionHost {
7879

7980
await ExecuteQueue.instance.add(language, callback);
8081
},
81-
executeSelection: async () : Promise<void> => {
82-
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', {languageId: language});
83-
}
82+
executeSelection: async (): Promise<void> => {
83+
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', { languageId: language });
84+
}
8485
};
8586

8687
// delegate for other languages
@@ -92,16 +93,16 @@ export function hooksExtensionHost() : ExtensionHost {
9293
registerStatementRangeProvider: (engine: MarkdownEngine): vscode.Disposable => {
9394
const hooks = hooksApi();
9495
if (hooks) {
95-
return hooks.languages.registerStatementRangeProvider('quarto',
96+
return hooks.languages.registerStatementRangeProvider('quarto',
9697
new EmbeddedStatementRangeProvider(engine));
9798
}
98-
return new vscode.Disposable(() => {});
99+
return new vscode.Disposable(() => { });
99100
},
100101

101102
createPreviewPanel: (
102-
viewType: string,
103+
viewType: string,
103104
title: string,
104-
preserveFocus?: boolean,
105+
preserveFocus?: boolean,
105106
options?: vscode.WebviewPanelOptions & vscode.WebviewOptions
106107
): HostWebviewPanel => {
107108

@@ -117,7 +118,7 @@ export function hooksExtensionHost() : ExtensionHost {
117118
portMapping: options?.portMapping
118119
}
119120
)!;
120-
121+
121122
// adapt to host interface
122123
return new HookWebviewPanel(panel);
123124
}
@@ -126,7 +127,7 @@ export function hooksExtensionHost() : ExtensionHost {
126127

127128

128129
class HookWebviewPanel implements HostWebviewPanel {
129-
constructor(private readonly panel_: hooks.PreviewPanel) {}
130+
constructor(private readonly panel_: hooks.PreviewPanel) { }
130131

131132
get webview() { return this.panel_.webview; };
132133
get visible() { return this.panel_.visible; };
@@ -139,43 +140,49 @@ class HookWebviewPanel implements HostWebviewPanel {
139140
}
140141

141142
class EmbeddedStatementRangeProvider implements HostStatementRangeProvider {
142-
private readonly _engine: MarkdownEngine;
143+
private readonly _engine: MarkdownEngine;
143144

144-
constructor(
145-
readonly engine: MarkdownEngine,
146-
) {
147-
this._engine = engine;
148-
}
145+
constructor(
146+
readonly engine: MarkdownEngine,
147+
) {
148+
this._engine = engine;
149+
}
149150

150151
async provideStatementRange(
151-
document: vscode.TextDocument,
152-
position: vscode.Position,
153-
token: vscode.CancellationToken): Promise<hooks.StatementRange | undefined> {
154-
const vdoc = await virtualDoc(document, position, this._engine);
155-
if (vdoc) {
156-
const vdocUri = await virtualDocUri(vdoc, document.uri, "statementRange");
157-
try {
158-
return getStatementRange(vdocUri.uri, adjustedPosition(vdoc.language, position));
159-
} catch (error) {
160-
return undefined;
161-
} finally {
162-
if (vdocUri.cleanup) {
163-
await vdocUri.cleanup();
164-
}
165-
}
166-
} else {
152+
document: vscode.TextDocument,
153+
position: vscode.Position,
154+
token: vscode.CancellationToken): Promise<hooks.StatementRange | undefined> {
155+
const vdoc = await virtualDoc(document, position, this._engine);
156+
if (vdoc) {
157+
const vdocUri = await virtualDocUri(vdoc, document.uri, "statementRange");
158+
try {
159+
return getStatementRange(
160+
vdocUri.uri,
161+
adjustedPosition(vdoc.language, position),
162+
vdoc.language
163+
);
164+
} catch (error) {
167165
return undefined;
166+
} finally {
167+
if (vdocUri.cleanup) {
168+
await vdocUri.cleanup();
169+
}
168170
}
169-
};
171+
} else {
172+
return undefined;
173+
}
174+
};
170175
}
171176

172177
async function getStatementRange(
173178
uri: vscode.Uri,
174179
position: vscode.Position,
180+
language: EmbeddedLanguage
175181
) {
176-
return await vscode.commands.executeCommand<hooks.StatementRange>(
182+
const result = await vscode.commands.executeCommand<hooks.StatementRange>(
177183
"vscode.executeStatementRangeProvider",
178184
uri,
179185
position
180186
);
187+
return { range: unadjustedRange(language, result.range), code: result.code };
181188
}

0 commit comments

Comments
 (0)