-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathVscodeTextEditor.ts
More file actions
270 lines (222 loc) · 7.97 KB
/
Copy pathVscodeTextEditor.ts
File metadata and controls
270 lines (222 loc) · 7.97 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
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as vscode from "vscode";
import type {
Edit,
EditableTextEditor,
GeneralizedRange,
OpenLinkOptions,
Range,
RevealLineAt,
Selection,
SetSelectionsOpts,
TextDocument,
TextEditor,
TextEditorOptions,
} from "@cursorless/lib-common";
import { sleep, uniqWithHash } from "@cursorless/lib-common";
import {
fromVscodeRange,
fromVscodeSelection,
toVscodeRange,
toVscodeSelection,
} from "@cursorless/lib-vscode-common";
import { isDiffEditorOriginal } from "./isDiffEditorOriginal";
import { vscodeEdit } from "./VscodeEdit";
import { vscodeFocusEditor } from "./VscodeFocusEditor";
import { vscodeFold, vscodeUnfold } from "./VscodeFold";
import type { VscodeIDE } from "./VscodeIDE";
import { vscodeInsertSnippet } from "./VscodeInsertSnippets";
import { vscodeOpenLink } from "./VscodeOpenLink";
import { vscodeRevealLine } from "./VscodeRevealLine";
import { VscodeTextDocument } from "./VscodeTextDocument";
import { vscodeToggleBreakpoint } from "./VscodeToggleBreakpoint";
export class VscodeTextEditor implements EditableTextEditor {
readonly document: TextDocument;
constructor(
public readonly id: string,
private ide: VscodeIDE,
private editor: vscode.TextEditor,
) {
this.document = new VscodeTextDocument(editor.document);
}
get vscodeEditor(): vscode.TextEditor {
return this.editor;
}
get selections(): Selection[] {
return this.editor.selections.map(fromVscodeSelection);
}
async setSelections(
rawSelections: Selection[],
{
focusEditor = false,
revealRange = true,
highlightWord = false,
}: SetSelectionsOpts = {},
): Promise<void> {
const selections = uniqWithHash(
rawSelections,
(a, b) => a.isEqual(b),
(s) => s.concise(),
).map(toVscodeSelection);
if (!focusEditor) {
this.editor.selections = selections;
return;
}
if (this.isDiffEditorOriginal || this.isSearchEditor) {
// NB: With a git diff editor we focus the editor BEFORE setting the
// selections because otherwise the selections will be clobbered when we
// issue the command to switch sides in the diff editor.
// The search editor has the same problem where focus is moved to the
// input field and the selection is clobbered.
await vscodeFocusEditor(this);
this.editor.selections = selections;
}
// Normal text editor
else {
// NB: With a normal text editor we focus the editor AFTER setting the
// selections because otherwise you see an intermediate state where the
// old selection persists
this.editor.selections = selections;
await vscodeFocusEditor(this);
}
if (revealRange) {
await this.revealRange(this.selections[0]);
}
if (highlightWord) {
vscode.commands.executeCommand("editor.action.wordHighlight.trigger");
}
}
get visibleRanges(): Range[] {
return this.editor.visibleRanges.map(fromVscodeRange);
}
get options(): TextEditorOptions {
return this.editor.options;
}
set options(options: TextEditorOptions) {
this.editor.options = options;
}
get isActive(): boolean {
return this.editor === vscode.window.activeTextEditor;
}
/** Returns true if this is the original/left hand side of a git diff editor */
get isDiffEditorOriginal(): boolean {
return isDiffEditorOriginal(this.editor);
}
/** Returns true if this is a search editor */
get isSearchEditor(): boolean {
return this.document.uri.scheme === "search-editor";
}
public isEqual(other: TextEditor): boolean {
return this.id === other.id;
}
public revealRange(range: Range): Promise<void> {
this.editor.revealRange(toVscodeRange(range));
return Promise.resolve();
}
public revealLine(lineNumber: number, at: RevealLineAt): Promise<void> {
return vscodeRevealLine(this, lineNumber, at);
}
public edit(edits: Edit[]): Promise<boolean> {
return vscodeEdit(this.editor, edits);
}
public focus(): Promise<void> {
return vscodeFocusEditor(this);
}
public async editNewNotebookCellAbove(): Promise<void> {
await vscode.commands.executeCommand("notebook.cell.insertCodeCellAbove");
}
public async editNewNotebookCellBelow(): Promise<void> {
await vscode.commands.executeCommand("notebook.cell.insertCodeCellBelow");
}
public openLink(
range: Range,
options: OpenLinkOptions = { openAside: false },
): Promise<void> {
return vscodeOpenLink(this, range, options);
}
public fold(ranges?: Range[]): Promise<void> {
return vscodeFold(this.ide, this, ranges);
}
public unfold(ranges?: Range[]): Promise<void> {
return vscodeUnfold(this.ide, this, ranges);
}
public toggleBreakpoint(ranges?: GeneralizedRange[]): Promise<void> {
return vscodeToggleBreakpoint(this, ranges);
}
public async toggleLineComment(_ranges?: Range[]): Promise<void> {
await vscode.commands.executeCommand("editor.action.commentLine");
}
public async clipboardCopy(_ranges?: Range[]): Promise<void> {
await vscode.commands.executeCommand("editor.action.clipboardCopyAction");
}
public async clipboardPaste(): Promise<void> {
// We add these sleeps here to workaround a bug in VSCode. See #1521
await sleep(100);
await vscode.commands.executeCommand("editor.action.clipboardPasteAction");
await sleep(100);
}
public async indentLine(_ranges?: Range[]): Promise<void> {
await vscode.commands.executeCommand("editor.action.indentLines");
}
public async outdentLine(_ranges?: Range[]): Promise<void> {
await vscode.commands.executeCommand("editor.action.outdentLines");
}
public async insertLineAfter(_ranges?: Range[]): Promise<void> {
await vscode.commands.executeCommand("editor.action.insertLineAfter");
}
public insertSnippet(snippet: string, ranges?: Range[]): Promise<void> {
return vscodeInsertSnippet(this, snippet, ranges);
}
public async rename(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("editor.action.rename");
}
public async showReferences(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("references-view.find");
}
public async quickFix(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("editor.action.quickFix");
await sleep(100);
}
public async revealDefinition(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("editor.action.revealDefinition");
}
public async revealTypeDefinition(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("editor.action.goToTypeDefinition");
}
public async showHover(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("editor.action.showHover");
}
public async showDebugHover(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("editor.debug.action.showDebugHover");
}
public async extractVariable(_range?: Range): Promise<void> {
if (this.document.languageId === "python") {
await vscode.commands.executeCommand("editor.action.codeAction", {
kind: "refactor.extract.variable",
});
} else {
await vscode.commands.executeCommand("editor.action.codeAction", {
kind: "refactor.extract.constant",
preferred: true,
});
}
await sleep(250);
}
public async gitAccept(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("merge-conflict.accept.selection");
}
public async gitRevert(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("git.revertSelectedRanges");
}
public async gitStageFile(): Promise<void> {
await vscode.commands.executeCommand("git.stage");
}
public async gitUnstageFile(): Promise<void> {
await vscode.commands.executeCommand("git.unstage");
}
public async gitStageRange(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("git.stageSelectedRanges");
}
public async gitUnstageRange(_range?: Range): Promise<void> {
await vscode.commands.executeCommand("git.unstageSelectedRanges");
}
}