/
autoInsertion.ts
118 lines (104 loc) · 3.56 KB
/
autoInsertion.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
import * as vscode from 'vscode';
import type { BaseLanguageClient } from 'vscode-languageclient';
import { AutoInsertRequest } from '@volar/language-server';
export async function register(
context: vscode.ExtensionContext,
clients: BaseLanguageClient[],
active: (document: vscode.TextDocument) => boolean,
) {
let isEnabled = false;
let timeout: NodeJS.Timeout | undefined;
updateEnabledState();
vscode.workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, context.subscriptions);
vscode.window.onDidChangeActiveTextEditor(updateEnabledState, null, context.subscriptions);
function updateEnabledState() {
isEnabled = false;
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
let document = editor.document;
if (!active(document)) {
return;
}
isEnabled = true;
}
function onDidChangeTextDocument({ document, contentChanges, reason }: vscode.TextDocumentChangeEvent) {
if (
!isEnabled
|| contentChanges.length !== 1
|| !contentChanges[0].text // delete
|| reason === vscode.TextDocumentChangeReason.Undo
|| reason === vscode.TextDocumentChangeReason.Redo
) {
return;
}
const activeDocument = vscode.window.activeTextEditor?.document;
if (document !== activeDocument) {
return;
}
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
const lastChange = contentChanges[contentChanges.length - 1];
doAutoInsert(document, lastChange, async (document, position, lastChange, isCancel) => {
for (const client of clients) {
const params = {
...client.code2ProtocolConverter.asTextDocumentPositionParams(document, position),
options: {
lastChange: {
...lastChange,
range: client.code2ProtocolConverter.asRange(lastChange.range),
},
},
};
if (isCancel()) return;
const result = await client.sendRequest(AutoInsertRequest.type, params);
if (result !== undefined && result !== null) {
if (typeof result === 'string') {
return result;
}
else {
return client.protocol2CodeConverter.asTextEdit(result);
}
}
}
});
}
function doAutoInsert(
document: vscode.TextDocument,
lastChange: vscode.TextDocumentContentChangeEvent,
provider: (document: vscode.TextDocument, position: vscode.Position, lastChange: vscode.TextDocumentContentChangeEvent, isCancel: () => boolean) => Thenable<string | vscode.TextEdit | null | undefined>,
) {
const version = document.version;
timeout = setTimeout(() => {
const position = vscode.window.activeTextEditor?.selections.length === 1 && vscode.window.activeTextEditor.selections[0].active;
if (position) {
provider(document, position, lastChange, () => vscode.window.activeTextEditor?.document.version !== version).then(text => {
if (text && isEnabled) {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
const activeDocument = activeEditor.document;
if (document === activeDocument && activeDocument.version === version) {
if (typeof text === 'string') {
const selections = activeEditor.selections;
if (selections.length && selections.some(s => s.active.isEqual(position))) {
activeEditor.insertSnippet(new vscode.SnippetString(text), selections.map(s => s.active));
}
else {
activeEditor.insertSnippet(new vscode.SnippetString(text), position);
}
}
else {
activeEditor.insertSnippet(new vscode.SnippetString(text.newText), text.range);
}
}
}
}
});
}
timeout = undefined;
}, 100);
}
}