forked from microsoft/vscode-json-languageservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.ts
61 lines (48 loc) · 1.92 KB
/
sample.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
import { getLanguageService, JSONSchema, SchemaRequestService, TextDocument, MatchingSchema } from '../jsonLanguageService';
async function main() {
const jsonContentUri = 'foo://server/example.data.json';
const jsonContent =
`{
"name": 12
"country": "Ireland"
}`;
const jsonSchemaUri = "foo://server/data.schema.json";
const jsonSchema = {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"country": {
"type": "string",
"enum": ["Ireland", "Iceland"]
}
}
};
const textDocument = TextDocument.create(jsonContentUri, 'json', 1, jsonContent);
const jsonLanguageService = getLanguageService({
schemaRequestService: (uri) => {
if (uri === jsonSchemaUri) {
return Promise.resolve(JSON.stringify(jsonSchema));
}
return Promise.reject(`Unabled to load schema at ${uri}`);
}
});
// associate `*.data.json` with the `foo://server/data.schema.json` schema
jsonLanguageService.configure({ allowComments: false, schemas: [{ fileMatch: ["*.data.json"], uri: jsonSchemaUri }] });
const jsonDocument = jsonLanguageService.parseJSONDocument(textDocument);
const diagnostics = await jsonLanguageService.doValidation(textDocument, jsonDocument);
console.log('Validation results:', diagnostics.map(d => `[line ${d.range.start.line}] ${d.message}`));
/*
* > Validation results: [
* > '[line 1] Incorrect type. Expected "string".',
* > '[line 2] Expected comma'
* > ]
*/
const competionResult = await jsonLanguageService.doComplete(textDocument, { line: 2, character: 18 }, jsonDocument);
console.log('Completion proposals:', competionResult?.items.map(i => `${i.label}`));
/*
* Completion proposals: [ '"Ireland"', '"Iceland"' ]
*/
}
main();