volar-service-json 0.0.30 → 0.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +16 -2
- package/index.js +71 -31
- package/package.json +3 -3
package/index.d.ts
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
|
-
import type { ServicePlugin } from '@volar/language-service';
|
|
1
|
+
import type { ServicePlugin, DocumentSelector, ServiceContext, Disposable, Result, FormattingOptions } from '@volar/language-service';
|
|
2
2
|
import * as json from 'vscode-json-languageservice';
|
|
3
3
|
import type { TextDocument } from 'vscode-languageserver-textdocument';
|
|
4
4
|
export interface Provide {
|
|
5
5
|
'json/jsonDocument': (document: TextDocument) => json.JSONDocument | undefined;
|
|
6
6
|
'json/languageService': () => json.LanguageService;
|
|
7
7
|
}
|
|
8
|
-
export
|
|
8
|
+
export interface JSONSchemaSettings {
|
|
9
|
+
fileMatch?: string[];
|
|
10
|
+
url?: string;
|
|
11
|
+
schema?: json.JSONSchema;
|
|
12
|
+
folderUri?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function create({ documentSelector, getWorkspaceContextService, isFormattingEnabled, getFormattingOptions, getLanguageSettings, getDocumentLanguageSettings, onDidChangeLanguageSettings, }?: {
|
|
15
|
+
documentSelector?: DocumentSelector;
|
|
16
|
+
getWorkspaceContextService?(context: ServiceContext): json.WorkspaceContextService;
|
|
17
|
+
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
|
|
18
|
+
getFormattingOptions?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<json.FormattingOptions>;
|
|
19
|
+
getLanguageSettings?(context: ServiceContext): Result<json.LanguageSettings>;
|
|
20
|
+
getDocumentLanguageSettings?(document: TextDocument, context: ServiceContext): Result<json.DocumentLanguageSettings | undefined>;
|
|
21
|
+
onDidChangeLanguageSettings?(listener: () => void, context: ServiceContext): Disposable;
|
|
22
|
+
}): ServicePlugin;
|
|
9
23
|
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -3,36 +3,65 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.create = void 0;
|
|
4
4
|
const json = require("vscode-json-languageservice");
|
|
5
5
|
const vscode_uri_1 = require("vscode-uri");
|
|
6
|
-
function create(
|
|
6
|
+
function create({ documentSelector = ['json', 'jsonc'], getWorkspaceContextService = () => {
|
|
7
|
+
return {
|
|
8
|
+
resolveRelativePath(relativePath, resource) {
|
|
9
|
+
const base = resource.substring(0, resource.lastIndexOf('/') + 1);
|
|
10
|
+
return vscode_uri_1.Utils.resolvePath(vscode_uri_1.URI.parse(base), relativePath).toString();
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}, isFormattingEnabled = async (_document, context) => {
|
|
14
|
+
return await context.env.getConfiguration?.('json.format.enable') ?? true;
|
|
15
|
+
}, getFormattingOptions = async (_document, options, context) => {
|
|
16
|
+
return {
|
|
17
|
+
...options,
|
|
18
|
+
...await context.env.getConfiguration?.('json.format'),
|
|
19
|
+
};
|
|
20
|
+
}, getLanguageSettings = async (context) => {
|
|
21
|
+
const languageSettings = {};
|
|
22
|
+
languageSettings.validate = await context.env.getConfiguration?.('json.validate') ?? true;
|
|
23
|
+
languageSettings.schemas ??= [];
|
|
24
|
+
const schemas = await context.env.getConfiguration?.('json.schemas') ?? [];
|
|
25
|
+
for (let i = 0; i < schemas.length; i++) {
|
|
26
|
+
const schema = schemas[i];
|
|
27
|
+
let uri = schema.url;
|
|
28
|
+
if (!uri && schema.schema) {
|
|
29
|
+
uri = schema.schema.id || `vscode://schemas/custom/${i}`;
|
|
30
|
+
}
|
|
31
|
+
if (uri) {
|
|
32
|
+
languageSettings.schemas.push({ uri, fileMatch: schema.fileMatch, schema: schema.schema, folderUri: schema.folderUri });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return languageSettings;
|
|
36
|
+
}, getDocumentLanguageSettings = document => {
|
|
37
|
+
return document.languageId === 'jsonc'
|
|
38
|
+
? { comments: 'ignore', trailingCommas: 'warning' }
|
|
39
|
+
: { comments: 'error', trailingCommas: 'error' };
|
|
40
|
+
}, onDidChangeLanguageSettings = (listener, context) => {
|
|
41
|
+
const disposable = context.env.onDidChangeConfiguration?.(listener);
|
|
42
|
+
return {
|
|
43
|
+
dispose() {
|
|
44
|
+
disposable?.dispose();
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}, } = {}) {
|
|
7
48
|
return {
|
|
8
49
|
name: 'json',
|
|
9
50
|
// https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150
|
|
10
51
|
triggerCharacters: ['"', ':'],
|
|
11
52
|
create(context) {
|
|
12
53
|
const jsonDocuments = new WeakMap();
|
|
13
|
-
const workspaceContext = {
|
|
14
|
-
resolveRelativePath: (ref, base) => {
|
|
15
|
-
if (ref.match(/^\w[\w\d+.-]*:/)) {
|
|
16
|
-
// starts with a schema
|
|
17
|
-
return ref;
|
|
18
|
-
}
|
|
19
|
-
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
|
|
20
|
-
return base + ref;
|
|
21
|
-
}
|
|
22
|
-
const baseUri = vscode_uri_1.URI.parse(base);
|
|
23
|
-
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : vscode_uri_1.Utils.dirname(baseUri);
|
|
24
|
-
return vscode_uri_1.Utils.resolvePath(baseUriDir, ref).toString(true);
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
54
|
const jsonLs = json.getLanguageService({
|
|
28
55
|
schemaRequestService: async (uri) => await context.env.fs?.readFile(uri) ?? '',
|
|
29
|
-
workspaceContext,
|
|
56
|
+
workspaceContext: getWorkspaceContextService(context),
|
|
30
57
|
clientCapabilities: context.env.clientCapabilities,
|
|
31
58
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
59
|
+
const disposable = onDidChangeLanguageSettings(() => initializing = undefined, context);
|
|
60
|
+
let initializing;
|
|
35
61
|
return {
|
|
62
|
+
dispose() {
|
|
63
|
+
disposable.dispose();
|
|
64
|
+
},
|
|
36
65
|
provide: {
|
|
37
66
|
'json/jsonDocument': getJsonDocument,
|
|
38
67
|
'json/languageService': () => jsonLs,
|
|
@@ -52,8 +81,8 @@ function create(settings) {
|
|
|
52
81
|
},
|
|
53
82
|
provideDiagnostics(document) {
|
|
54
83
|
return worker(document, async (jsonDocument) => {
|
|
55
|
-
const
|
|
56
|
-
return await jsonLs.doValidation(document, jsonDocument,
|
|
84
|
+
const settings = await getDocumentLanguageSettings(document, context);
|
|
85
|
+
return await jsonLs.doValidation(document, jsonDocument, settings);
|
|
57
86
|
});
|
|
58
87
|
},
|
|
59
88
|
provideHover(document, position) {
|
|
@@ -83,7 +112,7 @@ function create(settings) {
|
|
|
83
112
|
},
|
|
84
113
|
provideFoldingRanges(document) {
|
|
85
114
|
return worker(document, async () => {
|
|
86
|
-
return await jsonLs.getFoldingRanges(document);
|
|
115
|
+
return await jsonLs.getFoldingRanges(document, context.env.clientCapabilities?.textDocument?.foldingRange);
|
|
87
116
|
});
|
|
88
117
|
},
|
|
89
118
|
provideSelectionRanges(document, positions) {
|
|
@@ -93,26 +122,29 @@ function create(settings) {
|
|
|
93
122
|
},
|
|
94
123
|
provideDocumentFormattingEdits(document, range, options) {
|
|
95
124
|
return worker(document, async () => {
|
|
96
|
-
|
|
97
|
-
if (!(options_2?.enable ?? true)) {
|
|
125
|
+
if (!await isFormattingEnabled(document, context)) {
|
|
98
126
|
return;
|
|
99
127
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
...options,
|
|
103
|
-
});
|
|
128
|
+
const formatOptions = await getFormattingOptions(document, options, context);
|
|
129
|
+
return jsonLs.format(document, range, formatOptions);
|
|
104
130
|
});
|
|
105
131
|
},
|
|
106
132
|
};
|
|
107
|
-
function worker(document, callback) {
|
|
133
|
+
async function worker(document, callback) {
|
|
108
134
|
const jsonDocument = getJsonDocument(document);
|
|
109
135
|
if (!jsonDocument)
|
|
110
136
|
return;
|
|
111
|
-
|
|
137
|
+
await (initializing ??= initialize());
|
|
138
|
+
return await callback(jsonDocument);
|
|
139
|
+
}
|
|
140
|
+
async function initialize() {
|
|
141
|
+
const settings = await getLanguageSettings(context);
|
|
142
|
+
jsonLs.configure(settings);
|
|
112
143
|
}
|
|
113
144
|
function getJsonDocument(textDocument) {
|
|
114
|
-
if (
|
|
145
|
+
if (!matchDocument(documentSelector, textDocument)) {
|
|
115
146
|
return;
|
|
147
|
+
}
|
|
116
148
|
const cache = jsonDocuments.get(textDocument);
|
|
117
149
|
if (cache) {
|
|
118
150
|
const [cacheVersion, cacheDoc] = cache;
|
|
@@ -128,4 +160,12 @@ function create(settings) {
|
|
|
128
160
|
};
|
|
129
161
|
}
|
|
130
162
|
exports.create = create;
|
|
163
|
+
function matchDocument(selector, document) {
|
|
164
|
+
for (const sel of selector) {
|
|
165
|
+
if (sel === document.languageId || (typeof sel === 'object' && sel.language === document.languageId)) {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
131
171
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "volar-service-json",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.32",
|
|
4
4
|
"description": "Integrate vscode-json-languageservice into Volar",
|
|
5
5
|
"homepage": "https://github.com/volarjs/services/tree/master/packages/json",
|
|
6
6
|
"bugs": "https://github.com/volarjs/services/issues",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"vscode-languageserver-textdocument": "^1.0.11"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@volar/language-service": "~2.0
|
|
34
|
+
"@volar/language-service": "~2.1.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
37
37
|
"@volar/language-service": {
|
|
38
38
|
"optional": true
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "717049e7dcd5c30f451f6db8eb71eaba43f74c83"
|
|
42
42
|
}
|