-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
174 lines (151 loc) · 5.05 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
import type { ServicePlugin } from "@volar/language-service";
import {
type JSONDocument,
type TextDocument,
getLanguageService,
} from "vscode-json-languageservice";
import customBlockSchema from "./schema.json";
import { createGenerator, type Config } from "ts-json-schema-generator";
type PluginConfig = Pick<Config, "path" | "tsconfig">;
// Modify of https://github.com/volarjs/services/blob/master/packages/json/src/index.ts
export default (config: PluginConfig = {}): ServicePlugin => ({
// https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150
name: "route",
triggerCharacters: ['"', ":"],
create(context) {
const jsonDocuments = new WeakMap<TextDocument, [number, JSONDocument]>();
const jsonLs = getLanguageService({});
try {
const routeMetaSchema = createGenerator({
skipTypeCheck: true,
type: "RouteMeta",
...config,
}).createSchema("RouteMeta");
customBlockSchema.definitions = {
...customBlockSchema.definitions,
...routeMetaSchema.definitions,
};
} catch (e) {
console.log("[Error] volar-plugin-vue-router:");
console.log(e);
}
jsonLs.configure({
allowComments: false,
schemas: [
{
fileMatch: ["*.customBlock_route_*.json"],
uri: "foo://route-custom-block.schema.json",
schema: customBlockSchema,
},
],
});
return {
provide: {
"route/jsonDocument": getJsonDocument,
"route/languageService": () => jsonLs,
},
provideCompletionItems(document, position) {
return worker(document, async (jsonDocument) => {
return await jsonLs.doComplete(document, position, jsonDocument);
});
},
resolveCompletionItem(item) {
return jsonLs.doResolve(item);
},
provideDefinition(document, position) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findDefinition(document, position, jsonDocument);
});
},
provideDiagnostics(document) {
return worker(document, async (jsonDocument) => {
const documentLanguageSettings = undefined; // await getSettings(); // TODO
return await jsonLs.doValidation(
document,
jsonDocument,
documentLanguageSettings
);
});
},
provideHover(document, position) {
return worker(document, async (jsonDocument) => {
return await jsonLs.doHover(document, position, jsonDocument);
});
},
provideDocumentLinks(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findLinks(document, jsonDocument);
});
},
provideDocumentSymbols(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findDocumentSymbols2(document, jsonDocument);
});
},
provideDocumentColors(document) {
return worker(document, async (jsonDocument) => {
return await jsonLs.findDocumentColors(document, jsonDocument);
});
},
provideColorPresentations(document, color, range) {
return worker(document, async (jsonDocument) => {
return await jsonLs.getColorPresentations(
document,
jsonDocument,
color,
range
);
});
},
provideFoldingRanges(document) {
return worker(document, async () => {
return await jsonLs.getFoldingRanges(document);
});
},
provideSelectionRanges(document, positions) {
return worker(document, async (jsonDocument) => {
return await jsonLs.getSelectionRanges(
document,
positions,
jsonDocument
);
});
},
// provideDocumentFormattingEdits(document, range, options) {
// return worker(document, async () => {
// const options_2 = await context.env.getConfiguration?.<
// FormattingOptions & { enable: boolean }
// >("json.format");
// if (!(options_2?.enable ?? true)) {
// return;
// }
// });
// },
};
function worker<T>(
document: TextDocument,
callback: (jsonDocument: 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;
}
},
});