-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
49 lines (41 loc) · 1.56 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
import { language, Html1File } from './language';
import createEmmetService from 'volar-service-emmet';
import createHtmlService from 'volar-service-html';
import createCssService from 'volar-service-css';
import { createConnection, startLanguageServer, LanguageServerPlugin, Diagnostic, Service } from '@volar/language-server/node';
const plugin: LanguageServerPlugin = (): ReturnType<LanguageServerPlugin> => ({
extraFileExtensions: [{ extension: 'html1', isMixedContent: true, scriptKind: 7 }],
resolveConfig(config) {
// languages
config.languages ??= {};
config.languages.html1 ??= language;
// services
config.services ??= {};
config.services.html ??= createHtmlService();
config.services.css ??= createCssService();
config.services.emmet ??= createEmmetService();
config.services.html1 ??= (context): ReturnType<Service> => ({
provideDiagnostics(document) {
const [file] = context!.documents.getVirtualFileByUri(document.uri);
if (!(file instanceof Html1File)) return;
const styleNodes = file.htmlDocument.roots.filter(root => root.tag === 'style');
if (styleNodes.length <= 1) return;
const errors: Diagnostic[] = [];
for (let i = 1; i < styleNodes.length; i++) {
errors.push({
severity: 2,
range: {
start: file.document.positionAt(styleNodes[i].start),
end: file.document.positionAt(styleNodes[i].end),
},
source: 'html1',
message: 'Only one style tag is allowed.',
});
}
return errors;
},
});
return config;
},
});
startLanguageServer(createConnection(), plugin);