-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathhooks.ts
More file actions
221 lines (192 loc) · 6.79 KB
/
hooks.ts
File metadata and controls
221 lines (192 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* hooks.ts
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
import * as vscode from 'vscode';
import * as hooks from 'positron';
import { ExtensionHost, HostWebviewPanel, HostStatementRangeProvider, HostHelpTopicProvider } from '.';
import { CellExecutor, cellExecutorForLanguage, executableLanguages, isKnitrDocument, pythonWithReticulate } from './executors';
import { ExecuteQueue } from './execute-queue';
import { MarkdownEngine } from '../markdown/engine';
import { virtualDoc, virtualDocUri, adjustedPosition, unadjustedRange, withVirtualDocUri } from "../vdoc/vdoc";
import { EmbeddedLanguage } from '../vdoc/languages';
declare global {
function acquirePositronApi(): hooks.PositronApi;
}
let api: hooks.PositronApi | null | undefined;
export function hooksApi(): hooks.PositronApi | null {
if (api === undefined) {
try {
api = acquirePositronApi();
} catch {
api = null;
}
}
return api;
}
export function hasHooks() {
return !!hooksApi();
}
export function hooksExtensionHost(): ExtensionHost {
return {
// supported executable languages (we delegate to the default for languages
// w/o runtimes so we support all languages)
executableLanguages,
cellExecutorForLanguage: async (language: string, document: vscode.TextDocument, engine: MarkdownEngine, silent?: boolean)
: Promise<CellExecutor | undefined> => {
switch (language) {
// use hooks for known runtimes
case "python":
case "csharp":
case "r":
return {
execute: async (blocks: string[], _editorUri?: vscode.Uri): Promise<void> => {
const runtime = hooksApi()?.runtime;
if (runtime === undefined) {
// Can't do anything without a runtime
return;
}
if (language === "python" && isKnitrDocument(document, engine)) {
language = "r";
blocks = blocks.map(pythonWithReticulate);
}
// Our callback executes each block sequentially
const callback = async () => {
for (const block of blocks) {
await runtime.executeCode(language, block, false);
}
}
await ExecuteQueue.instance.add(language, callback);
},
executeSelection: async (): Promise<void> => {
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', { languageId: language });
}
};
// delegate for other languages
default:
return cellExecutorForLanguage(language, document, engine, silent);
}
},
registerStatementRangeProvider: (engine: MarkdownEngine): vscode.Disposable => {
const hooks = hooksApi();
if (hooks) {
return hooks.languages.registerStatementRangeProvider('quarto',
new EmbeddedStatementRangeProvider(engine));
}
return new vscode.Disposable(() => { });
},
registerHelpTopicProvider: (engine: MarkdownEngine): vscode.Disposable => {
const hooks = hooksApi();
if (hooks) {
return hooks.languages.registerHelpTopicProvider('quarto',
new EmbeddedHelpTopicProvider(engine));
}
return new vscode.Disposable(() => { });
},
createPreviewPanel: (
viewType: string,
title: string,
preserveFocus?: boolean,
options?: vscode.WebviewPanelOptions & vscode.WebviewOptions
): HostWebviewPanel => {
// create preview panel
const panel = hooksApi()?.window.createPreviewPanel(
viewType,
title,
preserveFocus,
{
enableScripts: options?.enableScripts,
enableForms: options?.enableForms,
localResourceRoots: options?.localResourceRoots,
portMapping: options?.portMapping
}
)!;
// adapt to host interface
return new HookWebviewPanel(panel);
}
};
}
class HookWebviewPanel implements HostWebviewPanel {
constructor(private readonly panel_: hooks.PreviewPanel) { }
get webview() { return this.panel_.webview; };
get visible() { return this.panel_.visible; };
reveal(_viewColumn?: vscode.ViewColumn, preserveFocus?: boolean) {
this.panel_.reveal(preserveFocus);
}
onDidChangeViewState = this.panel_.onDidChangeViewState;
onDidDispose = this.panel_.onDidDispose;
dispose() { this.panel_.dispose(); };
}
class EmbeddedStatementRangeProvider implements HostStatementRangeProvider {
private readonly _engine: MarkdownEngine;
constructor(
readonly engine: MarkdownEngine,
) {
this._engine = engine;
}
async provideStatementRange(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken): Promise<hooks.StatementRange | undefined> {
const vdoc = await virtualDoc(document, position, this._engine);
if (vdoc) {
return await withVirtualDocUri(vdoc, document.uri, "statementRange", async (uri: vscode.Uri) => {
return getStatementRange(
uri,
adjustedPosition(vdoc.language, position),
vdoc.language
);
});
} else {
return undefined;
}
};
}
async function getStatementRange(
uri: vscode.Uri,
position: vscode.Position,
language: EmbeddedLanguage
) {
const result = await vscode.commands.executeCommand<hooks.StatementRange>(
"vscode.executeStatementRangeProvider",
uri,
position
);
return { range: unadjustedRange(language, result.range), code: result.code };
}
class EmbeddedHelpTopicProvider implements HostHelpTopicProvider {
private readonly _engine: MarkdownEngine;
constructor(
readonly engine: MarkdownEngine,
) {
this._engine = engine;
}
async provideHelpTopic(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken): Promise<string | undefined> {
const vdoc = await virtualDoc(document, position, this._engine);
if (vdoc) {
return await withVirtualDocUri(vdoc, document.uri, "helpTopic", async (uri: vscode.Uri) => {
return await vscode.commands.executeCommand<string>(
"positron.executeHelpTopicProvider",
uri,
adjustedPosition(vdoc.language, position),
vdoc.language
);
});
} else {
return undefined;
}
};
}