volar-service-json 0.0.30 → 0.0.31

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.
Files changed (3) hide show
  1. package/index.d.ts +16 -2
  2. package/index.js +68 -28
  3. 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 } 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 declare function create(settings?: json.LanguageSettings): ServicePlugin;
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, context: ServiceContext): Result<json.FormattingOptions | undefined>;
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,62 @@ 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(settings) {
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, context) => {
16
+ return await context.env.getConfiguration?.('json.format');
17
+ }, getLanguageSettings = async (context) => {
18
+ const languageSettings = {};
19
+ languageSettings.validate = await context.env.getConfiguration?.('json.validate') ?? true;
20
+ languageSettings.schemas ??= [];
21
+ const schemas = await context.env.getConfiguration?.('json.schemas') ?? [];
22
+ for (let i = 0; i < schemas.length; i++) {
23
+ const schema = schemas[i];
24
+ let uri = schema.url;
25
+ if (!uri && schema.schema) {
26
+ uri = schema.schema.id || `vscode://schemas/custom/${i}`;
27
+ }
28
+ if (uri) {
29
+ languageSettings.schemas.push({ uri, fileMatch: schema.fileMatch, schema: schema.schema, folderUri: schema.folderUri });
30
+ }
31
+ }
32
+ return languageSettings;
33
+ }, getDocumentLanguageSettings = document => {
34
+ return document.languageId === 'jsonc'
35
+ ? { comments: 'ignore', trailingCommas: 'warning' }
36
+ : { comments: 'error', trailingCommas: 'error' };
37
+ }, onDidChangeLanguageSettings = (listener, context) => {
38
+ const disposable = context.env.onDidChangeConfiguration?.(listener);
39
+ return {
40
+ dispose() {
41
+ disposable?.dispose();
42
+ },
43
+ };
44
+ }, } = {}) {
7
45
  return {
8
46
  name: 'json',
9
47
  // https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150
10
48
  triggerCharacters: ['"', ':'],
11
49
  create(context) {
12
50
  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
51
  const jsonLs = json.getLanguageService({
28
52
  schemaRequestService: async (uri) => await context.env.fs?.readFile(uri) ?? '',
29
- workspaceContext,
53
+ workspaceContext: getWorkspaceContextService(context),
30
54
  clientCapabilities: context.env.clientCapabilities,
31
55
  });
32
- if (settings) {
33
- jsonLs.configure(settings);
34
- }
56
+ const disposable = onDidChangeLanguageSettings(() => initializing = undefined, context);
57
+ let initializing;
35
58
  return {
59
+ dispose() {
60
+ disposable.dispose();
61
+ },
36
62
  provide: {
37
63
  'json/jsonDocument': getJsonDocument,
38
64
  'json/languageService': () => jsonLs,
@@ -52,8 +78,8 @@ function create(settings) {
52
78
  },
53
79
  provideDiagnostics(document) {
54
80
  return worker(document, async (jsonDocument) => {
55
- const documentLanguageSettings = undefined; // await getSettings(); // TODO
56
- return await jsonLs.doValidation(document, jsonDocument, documentLanguageSettings, undefined);
81
+ const settings = await getDocumentLanguageSettings(document, context);
82
+ return await jsonLs.doValidation(document, jsonDocument, settings);
57
83
  });
58
84
  },
59
85
  provideHover(document, position) {
@@ -83,7 +109,7 @@ function create(settings) {
83
109
  },
84
110
  provideFoldingRanges(document) {
85
111
  return worker(document, async () => {
86
- return await jsonLs.getFoldingRanges(document);
112
+ return await jsonLs.getFoldingRanges(document, context.env.clientCapabilities?.textDocument?.foldingRange);
87
113
  });
88
114
  },
89
115
  provideSelectionRanges(document, positions) {
@@ -93,26 +119,32 @@ function create(settings) {
93
119
  },
94
120
  provideDocumentFormattingEdits(document, range, options) {
95
121
  return worker(document, async () => {
96
- const options_2 = await context.env.getConfiguration?.('json.format');
97
- if (!(options_2?.enable ?? true)) {
122
+ if (!await isFormattingEnabled(document, context)) {
98
123
  return;
99
124
  }
125
+ const formatOptions = await getFormattingOptions(document, context);
100
126
  return jsonLs.format(document, range, {
101
- ...options_2,
102
127
  ...options,
128
+ ...formatOptions,
103
129
  });
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
- return callback(jsonDocument);
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 (textDocument.languageId !== 'json' && textDocument.languageId !== 'jsonc')
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.30",
3
+ "version": "0.0.31",
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.1"
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": "30c3cc3c76e90f75f14fe0c2fa4fd33b7ff06507"
41
+ "gitHead": "f7005aef724767786ee9fe943fa976231cc79bf1"
42
42
  }