/
index.ts
278 lines (208 loc) · 7.67 KB
/
index.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
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
271
272
273
274
275
276
277
278
import type { LanguageServicePlugin, LanguageServicePluginContext } from '@volar/language-service';
import * as html from 'vscode-html-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as path from 'path';
export default function (options: {
validLang?: string,
disableCustomData?: boolean,
} = {}): LanguageServicePlugin & {
getHtmlLs: () => html.LanguageService,
updateCustomData(extraData: html.IHTMLDataProvider[]): void,
} {
const htmlDocuments = new WeakMap<TextDocument, [number, html.HTMLDocument]>();
let inited = false;
let customData: html.IHTMLDataProvider[] = [];
let extraData: html.IHTMLDataProvider[] = [];
let htmlLs: html.LanguageService;
let context: LanguageServicePluginContext;
return {
getHtmlLs: () => htmlLs,
updateCustomData,
setup(_context) {
htmlLs = html.getLanguageService({ fileSystemProvider: _context.env.fileSystemProvider });
context = _context;
},
complete: {
// https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/html-language-features/server/src/htmlServer.ts#L183
triggerCharacters: ['.', ':', '<', '"', '=', '/'],
async on(document, position) {
return worker(document, async (htmlDocument) => {
const configs = await context.env.configurationHost?.getConfiguration<html.CompletionConfiguration>('html.completion');
if (context.env.documentContext) {
return htmlLs.doComplete2(document, position, htmlDocument, context.env.documentContext, configs);
}
else {
return htmlLs.doComplete(document, position, htmlDocument, configs);
}
});
},
},
rename: {
on(document, position, newName) {
return worker(document, (htmlDocument) => {
return htmlLs.doRename(document, position, newName, htmlDocument);
});
},
},
async doHover(document, position) {
return worker(document, async (htmlDocument) => {
const hoverSettings = await context.env.configurationHost?.getConfiguration<html.HoverSettings>('html.hover');
return htmlLs.doHover(document, position, htmlDocument, hoverSettings);
});
},
findDocumentHighlights(document, position) {
return worker(document, (htmlDocument) => {
return htmlLs.findDocumentHighlights(document, position, htmlDocument);
});
},
findDocumentLinks(document) {
return worker(document, (htmlDocument) => {
if (!context.env.documentContext)
return;
return htmlLs.findDocumentLinks(document, context.env.documentContext);
});
},
findDocumentSymbols(document) {
return worker(document, (htmlDocument) => {
return htmlLs.findDocumentSymbols(document, htmlDocument);
});
},
getFoldingRanges(document) {
return worker(document, (htmlDocument) => {
return htmlLs.getFoldingRanges(document);
});
},
getSelectionRanges(document, positions) {
return worker(document, (htmlDocument) => {
return htmlLs.getSelectionRanges(document, positions);
});
},
async format(document, formatRange, options) {
return worker(document, async (htmlDocument) => {
const options_2 = await context.env.configurationHost?.getConfiguration<html.HTMLFormatConfiguration & { enable: boolean; }>('html.format');
if (options_2?.enable === false) {
return;
}
{ // https://github.com/microsoft/vscode/blob/dce493cb6e36346ef2714e82c42ce14fc461b15c/extensions/html-language-features/server/src/modes/formatting.ts#L13-L23
const endPos = formatRange.end;
let endOffset = document.offsetAt(endPos);
const content = document.getText();
if (endPos.character === 0 && endPos.line > 0 && endOffset !== content.length) {
// if selection ends after a new line, exclude that new line
const prevLineStart = document.offsetAt(vscode.Position.create(endPos.line - 1, 0));
while (isEOL(content, endOffset - 1) && endOffset > prevLineStart) {
endOffset--;
}
formatRange = vscode.Range.create(formatRange.start, document.positionAt(endOffset));
}
}
const edits = htmlLs.format(document, formatRange, {
...options_2,
...options,
});
const newText = TextDocument.applyEdits(document, edits);
return [{
newText: '\n' + newText.trim() + '\n',
range: {
start: document.positionAt(0),
end: document.positionAt(document.getText().length),
},
}];
});
},
findLinkedEditingRanges(document, position) {
return worker(document, (htmlDocument) => {
const ranges = htmlLs.findLinkedEditingRanges(document, position, htmlDocument);
if (!ranges)
return;
return { ranges };
});
},
async doAutoInsert(document, position, insertContext) {
return worker(document, async (htmlDocument) => {
const lastCharacter = insertContext.lastChange.text[insertContext.lastChange.text.length - 1];
if (insertContext.lastChange.rangeLength === 0 && lastCharacter === '=') {
const enabled = (await context.env.configurationHost?.getConfiguration<boolean>('html.autoCreateQuotes')) ?? true;
if (enabled) {
const text = htmlLs.doQuoteComplete(document, position, htmlDocument, await context.env.configurationHost?.getConfiguration<html.CompletionConfiguration>('html.completion'));
if (text) {
return text;
}
}
}
if (insertContext.lastChange.rangeLength === 0 && (lastCharacter === '>' || lastCharacter === '/')) {
const enabled = (await context.env.configurationHost?.getConfiguration<boolean>('html.autoClosingTags')) ?? true;
if (enabled) {
const text = htmlLs.doTagComplete(document, position, htmlDocument);
if (text) {
return text;
}
}
}
});
},
};
async function initCustomData() {
if (!inited && !options.disableCustomData) {
inited = true;
context.env.configurationHost?.onDidChangeConfiguration(async () => {
customData = await getCustomData();
htmlLs.setDataProviders(true, [...customData, ...extraData]);
});
customData = await getCustomData();
htmlLs.setDataProviders(true, [...customData, ...extraData]);
}
}
function updateCustomData(data: html.IHTMLDataProvider[]) {
extraData = data;
htmlLs.setDataProviders(true, [...customData, ...extraData]);
}
async function getCustomData() {
const configHost = context.env.configurationHost;
if (configHost) {
const customData: string[] = await configHost.getConfiguration('html.customData') ?? [];
const newData: html.IHTMLDataProvider[] = [];
for (const customDataPath of customData) {
try {
const jsonPath = path.resolve(customDataPath);
newData.push(html.newHTMLDataProvider(customDataPath, require(jsonPath)));
}
catch (error) {
console.error(error);
}
}
return newData;
}
return [];
}
async function worker<T>(document: TextDocument, callback: (htmlDocument: html.HTMLDocument) => T) {
const htmlDocument = getHtmlDocument(document);
if (!htmlDocument)
return;
await initCustomData();
return callback(htmlDocument);
}
function getHtmlDocument(document: TextDocument) {
if (document.languageId !== (options.validLang ?? 'html'))
return;
const cache = htmlDocuments.get(document);
if (cache) {
const [cacheVersion, cacheDoc] = cache;
if (cacheVersion === document.version) {
return cacheDoc;
}
}
const doc = htmlLs.parseHTMLDocument(document);
htmlDocuments.set(document, [document.version, doc]);
return doc;
}
}
function isEOL(content: string, offset: number) {
return isNewlineCharacter(content.charCodeAt(offset));
}
const CR = '\r'.charCodeAt(0);
const NL = '\n'.charCodeAt(0);
function isNewlineCharacter(charCode: number) {
return charCode === CR || charCode === NL;
}