Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/html-language-features/client/src/autoInsertion.ts
3320 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { window, workspace, Disposable, TextDocument, Position, SnippetString, TextDocumentChangeEvent, TextDocumentChangeReason, TextDocumentContentChangeEvent } from 'vscode';
7
import { Runtime } from './htmlClient';
8
import { LanguageParticipants } from './languageParticipants';
9
10
export function activateAutoInsertion(provider: (kind: 'autoQuote' | 'autoClose', document: TextDocument, position: Position) => Thenable<string>, languageParticipants: LanguageParticipants, runtime: Runtime): Disposable {
11
const disposables: Disposable[] = [];
12
workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, disposables);
13
14
let anyIsEnabled = false;
15
const isEnabled = {
16
'autoQuote': false,
17
'autoClose': false
18
};
19
updateEnabledState();
20
window.onDidChangeActiveTextEditor(updateEnabledState, null, disposables);
21
22
let timeout: Disposable | undefined = undefined;
23
24
disposables.push({
25
dispose: () => {
26
timeout?.dispose();
27
}
28
});
29
30
function updateEnabledState() {
31
anyIsEnabled = false;
32
const editor = window.activeTextEditor;
33
if (!editor) {
34
return;
35
}
36
const document = editor.document;
37
if (!languageParticipants.useAutoInsert(document.languageId)) {
38
return;
39
}
40
const configurations = workspace.getConfiguration(undefined, document.uri);
41
isEnabled['autoQuote'] = configurations.get<boolean>('html.autoCreateQuotes') ?? false;
42
isEnabled['autoClose'] = configurations.get<boolean>('html.autoClosingTags') ?? false;
43
anyIsEnabled = isEnabled['autoQuote'] || isEnabled['autoClose'];
44
}
45
46
function onDidChangeTextDocument({ document, contentChanges, reason }: TextDocumentChangeEvent) {
47
if (!anyIsEnabled || contentChanges.length === 0 || reason === TextDocumentChangeReason.Undo || reason === TextDocumentChangeReason.Redo) {
48
return;
49
}
50
const activeDocument = window.activeTextEditor && window.activeTextEditor.document;
51
if (document !== activeDocument) {
52
return;
53
}
54
if (timeout) {
55
timeout.dispose();
56
}
57
58
const lastChange = contentChanges[contentChanges.length - 1];
59
if (lastChange.rangeLength === 0 && isSingleLine(lastChange.text)) {
60
const lastCharacter = lastChange.text[lastChange.text.length - 1];
61
if (isEnabled['autoQuote'] && lastCharacter === '=') {
62
doAutoInsert('autoQuote', document, lastChange);
63
} else if (isEnabled['autoClose'] && (lastCharacter === '>' || lastCharacter === '/')) {
64
doAutoInsert('autoClose', document, lastChange);
65
}
66
}
67
}
68
69
function isSingleLine(text: string): boolean {
70
return !/\n/.test(text);
71
}
72
73
function doAutoInsert(kind: 'autoQuote' | 'autoClose', document: TextDocument, lastChange: TextDocumentContentChangeEvent) {
74
const rangeStart = lastChange.range.start;
75
const version = document.version;
76
timeout = runtime.timer.setTimeout(() => {
77
const position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length);
78
provider(kind, document, position).then(text => {
79
if (text && isEnabled[kind]) {
80
const activeEditor = window.activeTextEditor;
81
if (activeEditor) {
82
const activeDocument = activeEditor.document;
83
if (document === activeDocument && activeDocument.version === version) {
84
const selections = activeEditor.selections;
85
if (selections.length && selections.some(s => s.active.isEqual(position))) {
86
activeEditor.insertSnippet(new SnippetString(text), selections.map(s => s.active));
87
} else {
88
activeEditor.insertSnippet(new SnippetString(text), position);
89
}
90
}
91
}
92
}
93
});
94
timeout = undefined;
95
}, 100);
96
}
97
return Disposable.from(...disposables);
98
}
99
100