-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathVscodeIDE.ts
More file actions
257 lines (226 loc) · 7.88 KB
/
Copy pathVscodeIDE.ts
File metadata and controls
257 lines (226 loc) · 7.88 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import type {
Disposable,
EditableTextEditor,
FlashDescriptor,
GeneralizedRange,
HighlightId,
IDE,
InputBoxOptions,
NotebookEditor,
OpenUntitledTextDocumentOptions,
QuickPickOptions,
RunMode,
TextDocumentChangeEvent,
TextEditor,
} from "@cursorless/common";
import { OutdatedExtensionError } from "@cursorless/common";
import {
fromVscodeRange,
fromVscodeSelection,
} from "@cursorless/vscode-common";
import { pull } from "lodash-es";
import { v4 as uuid } from "uuid";
import type { ExtensionContext, WorkspaceFolder } from "vscode";
import * as vscode from "vscode";
import { window, workspace } from "vscode";
import { VscodeCapabilities } from "./VscodeCapabilities";
import VscodeClipboard from "./VscodeClipboard";
import VscodeConfiguration from "./VscodeConfiguration";
import { forwardEvent, vscodeOnDidChangeTextDocument } from "./VscodeEvents";
import VscodeFlashHandler from "./VscodeFlashHandler";
import VscodeHighlights, { HighlightStyle } from "./VscodeHighlights";
import { VscodeNotebookEditorImpl } from "./VscodeIdeNotebook";
import VscodeKeyValueStore from "./VscodeKeyValueStore";
import VscodeMessages from "./VscodeMessages";
import { vscodeRunMode } from "./VscodeRunMode";
import { VscodeTextDocumentImpl } from "./VscodeTextDocumentImpl";
import { VscodeTextEditorImpl } from "./VscodeTextEditorImpl";
import { vscodeShowQuickPick } from "./vscodeShowQuickPick";
export class VscodeIDE implements IDE {
readonly configuration: VscodeConfiguration;
readonly keyValueStore: VscodeKeyValueStore;
readonly messages: VscodeMessages;
readonly clipboard: VscodeClipboard;
readonly capabilities: VscodeCapabilities;
private flashHandler: VscodeFlashHandler;
private highlights: VscodeHighlights;
private editorMap;
constructor(private extensionContext: ExtensionContext) {
this.configuration = new VscodeConfiguration(this);
this.keyValueStore = new VscodeKeyValueStore(extensionContext);
this.messages = new VscodeMessages();
this.clipboard = new VscodeClipboard();
this.highlights = new VscodeHighlights(extensionContext);
this.flashHandler = new VscodeFlashHandler(this, this.highlights);
this.capabilities = new VscodeCapabilities();
this.editorMap = new WeakMap<vscode.TextEditor, VscodeTextEditorImpl>();
}
async showQuickPick(
items: readonly string[],
options?: QuickPickOptions,
): Promise<string | undefined> {
return await vscodeShowQuickPick(items, options);
}
setHighlightRanges(
highlightId: HighlightId | undefined,
editor: TextEditor,
ranges: GeneralizedRange[],
): Promise<void> {
const vscodeHighlightId =
highlightId == null
? HighlightStyle.highlight0
: HighlightStyle[highlightId as keyof typeof HighlightStyle];
return this.highlights.setHighlightRanges(
vscodeHighlightId,
editor as VscodeTextEditorImpl,
ranges,
);
}
flashRanges(flashDescriptors: FlashDescriptor[]): Promise<void> {
return this.flashHandler.flashRanges(flashDescriptors);
}
get assetsRoot(): string {
return this.extensionContext.extensionPath;
}
get cursorlessVersion(): string {
return this.extensionContext.extension.packageJSON.version;
}
get runMode(): RunMode {
return vscodeRunMode(this.extensionContext);
}
get workspaceFolders(): readonly WorkspaceFolder[] | undefined {
return workspace.workspaceFolders;
}
get activeTextEditor(): VscodeTextEditorImpl | undefined {
return this.getActiveTextEditor();
}
get activeEditableTextEditor(): VscodeTextEditorImpl | undefined {
return this.getActiveTextEditor();
}
private getActiveTextEditor() {
return window.activeTextEditor != null &&
isValidEditor(window.activeTextEditor)
? this.fromVscodeEditor(window.activeTextEditor)
: undefined;
}
get visibleTextEditors(): VscodeTextEditorImpl[] {
return window.visibleTextEditors
.filter(isValidEditor)
.map((e) => this.fromVscodeEditor(e));
}
get visibleNotebookEditors(): NotebookEditor[] {
return vscode.window.visibleNotebookEditors.map(
(editor) => new VscodeNotebookEditorImpl(editor),
);
}
public getEditableTextEditor(editor: TextEditor): EditableTextEditor {
return editor as EditableTextEditor;
}
public async findInDocument(
query: string,
editor?: TextEditor,
): Promise<void> {
if (editor != null && !editor.isActive) {
await this.getEditableTextEditor(editor).focus();
}
await vscode.commands.executeCommand("editor.actions.findWithArgs", {
searchString: query,
});
}
public async findInWorkspace(query: string): Promise<void> {
await vscode.commands.executeCommand("workbench.action.findInFiles", {
query,
});
}
public async openTextDocument(path: string): Promise<TextEditor> {
const textDocument = await workspace.openTextDocument(path);
return this.fromVscodeEditor(await window.showTextDocument(textDocument));
}
public async openUntitledTextDocument(
options?: OpenUntitledTextDocumentOptions,
): Promise<TextEditor> {
const textDocument = await workspace.openTextDocument(options);
return this.fromVscodeEditor(await window.showTextDocument(textDocument));
}
public async showInputBox(
options?: InputBoxOptions,
): Promise<string | undefined> {
return await vscode.window.showInputBox(options);
}
public async executeCommand<T>(
command: string,
...args: any[]
): Promise<T | undefined> {
return await vscode.commands.executeCommand(command, ...args);
}
public onDidChangeTextDocument(
listener: (event: TextDocumentChangeEvent) => void,
): Disposable {
return vscodeOnDidChangeTextDocument(listener);
}
onDidOpenTextDocument = forwardEvent(
workspace.onDidOpenTextDocument,
(document) => new VscodeTextDocumentImpl(document),
);
onDidCloseTextDocument = forwardEvent(
workspace.onDidCloseTextDocument,
(document) => new VscodeTextDocumentImpl(document),
);
onDidChangeActiveTextEditor = forwardEvent(
window.onDidChangeActiveTextEditor,
(editor) => (editor == null ? editor : this.fromVscodeEditor(editor)),
);
onDidChangeVisibleTextEditors = forwardEvent(
window.onDidChangeVisibleTextEditors,
(editors) => editors.map((editor) => this.fromVscodeEditor(editor)),
);
onDidChangeTextEditorSelection = forwardEvent(
window.onDidChangeTextEditorSelection,
({ textEditor, selections }) => ({
textEditor: this.fromVscodeEditor(textEditor),
selections: selections.map(fromVscodeSelection),
}),
);
onDidChangeTextEditorVisibleRanges = forwardEvent(
window.onDidChangeTextEditorVisibleRanges,
({ textEditor, visibleRanges }) => ({
textEditor: this.fromVscodeEditor(textEditor),
visibleRanges: visibleRanges.map(fromVscodeRange),
}),
);
public fromVscodeEditor(editor: vscode.TextEditor): VscodeTextEditorImpl {
if (!this.editorMap.has(editor)) {
this.editorMap.set(
editor,
new VscodeTextEditorImpl(uuid(), this, editor),
);
}
return this.editorMap.get(editor)!;
}
handleCommandError(err: Error) {
if (err instanceof OutdatedExtensionError) {
void this.showUpdateExtensionErrorMessage(err);
} else {
void vscode.window.showErrorMessage(err.message);
}
}
private async showUpdateExtensionErrorMessage(err: OutdatedExtensionError) {
const item = await vscode.window.showErrorMessage(
err.message,
"Check for updates",
);
if (item == null) {
return;
}
await vscode.commands.executeCommand(
"workbench.extensions.action.checkForUpdates",
);
}
disposeOnExit(...disposables: Disposable[]): () => void {
this.extensionContext.subscriptions.push(...disposables);
return () => pull(this.extensionContext.subscriptions, ...disposables);
}
}
function isValidEditor(editor: vscode.TextEditor): boolean {
return editor.document.uri.scheme !== "output";
}