/
index.ts
165 lines (129 loc) · 4.27 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
import type { LanguageServicePlugin, LanguageServicePluginContext } from '@volar/language-service';
import * as json from 'vscode-json-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
export default function (settings?: json.LanguageSettings): LanguageServicePlugin {
const jsonDocuments = new WeakMap<TextDocument, [number, json.JSONDocument]>();
let context: LanguageServicePluginContext;
let jsonLs: json.LanguageService;
return {
setup(_context) {
context = _context;
jsonLs = json.getLanguageService({ schemaRequestService: _context.env.schemaRequestService });
if (settings) {
jsonLs.configure(settings);
}
},
complete: {
// https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150
triggerCharacters: ['"', ':'],
on(document, position, context) {
return worker(document, async (jsonDocument) => {
return await jsonLs.doComplete(document, position, jsonDocument);
});
},
async resolve(item) {
return await jsonLs.doResolve(item);
},
},
definition: {
on(document, position) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findDefinition(document, position, jsonDocument);
});
},
},
validation: {
onSyntactic(document) {
return worker(document, async (jsonDocument) => {
const documentLanguageSettings = undefined; // await getSettings(); // TODO
return await jsonLs.doValidation(
document,
jsonDocument,
documentLanguageSettings,
undefined, // TODO
) as vscode.Diagnostic[];
});
},
},
doHover(document, position) {
return worker(document, async (jsonDocument) => {
return await jsonLs.doHover(document, position, jsonDocument);
});
},
findDocumentLinks(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findLinks(document, jsonDocument);
});
},
findDocumentSymbols(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findDocumentSymbols(document, jsonDocument);
});
},
findDocumentColors(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findDocumentColors(document, jsonDocument);
});
},
getColorPresentations(document, color, range) {
return worker(document, async (jsonDocument) => {
return await jsonLs.getColorPresentations(document, jsonDocument, color, range);
});
},
getFoldingRanges(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.getFoldingRanges(document);
});
},
getSelectionRanges(document, positions) {
return worker(document, async (jsonDocument) => {
return await jsonLs.getSelectionRanges(document, positions, jsonDocument);
});
},
format(document, range, options) {
return worker(document, async (jsonDocument) => {
const options_2 = await context.env.configurationHost?.getConfiguration<json.FormattingOptions & { enable: boolean; }>('json.format');
if (options_2?.enable === false) {
return;
}
const edits = jsonLs.format(document, range, {
...options_2,
...options,
insertFinalNewline: true,
});
if (!edits.length) {
return edits;
}
const newText = TextDocument.applyEdits(document, edits);
return [{
newText: '\n' + newText.trim() + '\n',
range: {
start: document.positionAt(0),
end: document.positionAt(document.getText().length),
},
}];
});
},
};
function worker<T>(document: TextDocument, callback: (jsonDocument: json.JSONDocument) => T) {
const jsonDocument = getJsonDocument(document);
if (!jsonDocument)
return;
return callback(jsonDocument);
}
function getJsonDocument(textDocument: TextDocument) {
if (textDocument.languageId !== 'json' && textDocument.languageId !== 'jsonc')
return;
const cache = jsonDocuments.get(textDocument);
if (cache) {
const [cacheVersion, cacheDoc] = cache;
if (cacheVersion === textDocument.version) {
return cacheDoc;
}
}
const doc = jsonLs.parseJSONDocument(textDocument);
jsonDocuments.set(textDocument, [textDocument.version, doc]);
return doc;
}
}