-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.ts
221 lines (192 loc) · 7.95 KB
/
extension.ts
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
import * as vscode from 'vscode';
import { FlowrInternalSession } from './flowr/internal-session';
import { FlowrServerSession } from './flowr/server-session';
import { Settings } from './settings';
import { registerSliceCommands } from './slice';
import { registerDiagramCommands } from './diagram';
import type { FlowrSession } from './flowr/utils';
import { selectionSlicer } from './selection-slicer';
import { positionSlicers } from './position-slicer';
import { flowrVersion } from '@eagleoutice/flowr/util/version';
import { registerDependencyView } from './flowr/views/dependency-view';
import { VariableResolve , defaultConfigOptions, setConfig } from '@eagleoutice/flowr/config';
import type { BuiltInDefinitions } from '@eagleoutice/flowr/dataflow/environments/built-in-config';
import { deepMergeObject } from '@eagleoutice/flowr/util/objects';
import { registerInlineHints } from './flowr/views/inline-values';
export const MINIMUM_R_MAJOR = 3;
export const BEST_R_MAJOR = 4;
let extensionContext: vscode.ExtensionContext;
let outputChannel: vscode.OutputChannel;
let statusBarItem: vscode.StatusBarItem;
let flowrSession: FlowrSession | undefined;
export async function activate(context: vscode.ExtensionContext) {
extensionContext = context;
outputChannel = vscode.window.createOutputChannel('flowR');
outputChannel.appendLine(`flowR extension activated (ships with flowR v${flowrVersion().toString()})`);
registerDiagramCommands(context, outputChannel);
registerSliceCommands(context, outputChannel);
updateFlowrConfig();
vscode.workspace.onDidChangeConfiguration(updateFlowrConfig);
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.session.internal', async() => {
await establishInternalSession();
return flowrSession;
}));
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.session.connect', async() => {
await establishServerSession();
return flowrSession;
}));
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.session.disconnect', () => {
if(flowrSession instanceof FlowrServerSession) {
destroySession();
}
}));
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.repl', async() => {
try {
const repl = await import('./flowr/terminals/flowr-repl');
repl.showRepl(context, await getFlowrSession());
} catch(e){
vscode.window.showErrorMessage('Failed to start flowR REPL');
console.error(e);
}
}));
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.settings.open', async() => {
await vscode.commands.executeCommand('workbench.action.openSettings', Settings.Category);
}));
context.subscriptions.push(
vscode.commands.registerCommand('vscode-flowr.report', () => {
void vscode.env.openExternal(vscode.Uri.parse('https://github.com/flowr-analysis/flowr/issues/new/choose'));
})
);
statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
context.subscriptions.push(statusBarItem);
updateStatusBar();
context.subscriptions.push(new vscode.Disposable(() => destroySession()));
const { dispose: disposeDep, update: updateDependencyView } = registerDependencyView(outputChannel);
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.dependencyView.update', () => {
updateDependencyView();
}));
context.subscriptions.push(registerInlineHints(outputChannel))
context.subscriptions.push(new vscode.Disposable(() => disposeDep()));
process.on('SIGINT', () => destroySession());
if(getConfig().get<boolean>(Settings.ServerAutoConnect)) {
await establishServerSession();
}
}
export function getConfig(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration(Settings.Category);
}
export function isVerbose(): boolean {
return getConfig().get<boolean>(Settings.VerboseLog, false);
}
export async function establishInternalSession() {
destroySession();
flowrSession = new FlowrInternalSession(outputChannel);
await flowrSession.initialize();
return flowrSession;
}
export async function getFlowrSession() {
if(flowrSession) {
return flowrSession;
}
// initialize a default session if none is active
// on the web, we always want to use the tree-sitter backend since we can't run R
return await establishInternalSession();
}
export async function establishServerSession() {
destroySession();
flowrSession = new FlowrServerSession(outputChannel);
await flowrSession.initialize();
return flowrSession;
}
export function destroySession() {
flowrSession?.destroy();
flowrSession = undefined;
}
export function updateStatusBar() {
const text: string[] = [];
const tooltip: string[] = [];
if(flowrSession instanceof FlowrServerSession) {
text.push(`$(cloud) flowR ${flowrSession.state}`);
if(flowrSession.state === 'connected') {
tooltip.push(`R version ${flowrSession.rVersion} \nflowR version ${flowrSession.flowrVersion}`);
}
if(flowrSession.working){
text.push('$(loading~spin) Analyzing');
}
} else if(flowrSession instanceof FlowrInternalSession) {
text.push(`$(console) flowR ${flowrSession.state}`);
if(flowrSession.state === 'active') {
tooltip.push(`R version ${flowrSession.rVersion} \nflowR version ${flowrVersion().toString()} \nEngine ${flowrSession.parser?.name}`);
}
if(flowrSession.working){
text.push('$(loading~spin) Analyzing');
}
}
const slicingTypes: string[] = [];
const slicingFiles: string[] = [];
if(selectionSlicer?.changeListeners.length) {
slicingTypes.push('cursor');
}
if(positionSlicers.size) {
const pos = [...positionSlicers].reduce((i, [,s]) => i + s.offsets.length, 0);
if(pos > 0) {
slicingTypes.push(`${pos} position${pos === 1 ? '' : 's'}`);
for(const [doc,slicer] of positionSlicers) {
slicingFiles.push(`${vscode.workspace.asRelativePath(doc.fileName)} (${slicer.offsets.length} position${slicer.offsets.length === 1 ? '' : 's'})`);
}
}
}
if(slicingTypes.length) {
text.push(`$(lightbulb) Slicing ${slicingTypes.join(', ')}`);
if(slicingFiles.length) {
tooltip.push(`Slicing in\n${slicingFiles.map(f => `- ${f}`).join('\n')}`);
}
}
if(text.length) {
statusBarItem.show();
statusBarItem.text = text.join(' ');
statusBarItem.tooltip = tooltip.length ? tooltip.reduce((m, s) => m.appendMarkdown('\n\n').appendMarkdown(s), new vscode.MarkdownString()) : undefined;
} else {
statusBarItem.hide();
}
}
export function isWeb() {
// apparently there is no official way to test this from the vscode api other
// than in the command availability context stuff, which is not what we want
// this is dirty but it should work since the WebSocket is unavailable in node
return typeof WebSocket !== 'undefined';
}
export function getWasmRootPath(): string {
if(!isWeb()) {
return `${__dirname}/flowr/tree-sitter`;
} else {
const uri = vscode.Uri.joinPath(extensionContext.extensionUri, '/dist/web');
// in the fake browser version of vscode, it needs to be a special scheme, so we do this check
return uri.scheme !== 'file' ? uri.toString() : `vscode-file://vscode-app/${uri.fsPath}`;
}
}
function updateFlowrConfig() {
const config = getConfig();
const wasmRoot = getWasmRootPath();
// we don't want to *amend* here since updates to our extension config shouldn't add additional entries while keeping old ones (definitions etc.)
setConfig(deepMergeObject(defaultConfigOptions, {
ignoreSourceCalls: config.get<boolean>(Settings.IgnoreSourceCalls, false),
solver: {
variables: config.get<VariableResolve>(Settings.SolverVariableHandling, VariableResolve.Alias),
pointerTracking: config.get<boolean>(Settings.SolverPointerTracking, true)
},
semantics: {
environment: {
overwriteBuiltIns: {
loadDefaults: config.get<boolean>(Settings.BuiltInsLoadDefaults, true),
definitions: config.get<BuiltInDefinitions>(Settings.BuiltInsDefinitions, [])
}
}
},
engines: [{
type: FlowrInternalSession.getEngineToUse(),
wasmPath: `${wasmRoot}/tree-sitter-r.wasm`,
treeSitterWasmPath: `${wasmRoot}/tree-sitter.wasm`
}]
}));
}