@volar/language-service 2.0.0 → 2.0.2

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/lib/documents.js CHANGED
@@ -137,6 +137,7 @@ function createDocumentProvider(files) {
137
137
  return mirrorMap2DocMirrorMap.get(map);
138
138
  }
139
139
  },
140
+ // TODO: rename to getVirtualCodeByDocumentUri
140
141
  getVirtualCodeByUri(uri) {
141
142
  if (uri.includes('?virtualCodeId=')) {
142
143
  const sourceFileUri = uri.split('?virtualCodeId=')[0];
@@ -145,6 +146,7 @@ function createDocumentProvider(files) {
145
146
  }
146
147
  return [undefined, undefined];
147
148
  },
149
+ // TODO: rename to getDocumentUriByVirtualCode
148
150
  getVirtualCodeUri(sourceFileUri, virtualCodeId) {
149
151
  return sourceFileUri + `?virtualCodeId=${virtualCodeId}`;
150
152
  },
@@ -6,15 +6,22 @@ const cancellation_1 = require("../utils/cancellation");
6
6
  const transform_1 = require("../utils/transform");
7
7
  const featureWorkers_1 = require("../utils/featureWorkers");
8
8
  function register(context) {
9
- let cache;
9
+ let lastResult;
10
10
  return async (uri, position, completionContext = { triggerKind: 1, }, token = cancellation_1.NoneCancellationToken) => {
11
11
  const sourceFile = context.language.files.get(uri);
12
+ if (!sourceFile) {
13
+ return {
14
+ isIncomplete: false,
15
+ items: [],
16
+ };
17
+ }
12
18
  if (completionContext?.triggerKind === 3
13
- && cache?.uri === uri) {
14
- for (const cacheData of cache.data) {
15
- if (!cacheData.list.isIncomplete) {
19
+ && lastResult?.uri === uri) {
20
+ for (const cacheData of lastResult.results) {
21
+ if (!cacheData.list?.isIncomplete) {
16
22
  continue;
17
23
  }
24
+ const serviceIndex = context.services.findIndex(service => service[1] === cacheData.service);
18
25
  if (cacheData.virtualDocumentUri) {
19
26
  const [virtualCode] = context.documents.getVirtualCodeByUri(cacheData.virtualDocumentUri);
20
27
  if (!virtualCode) {
@@ -25,35 +32,36 @@ function register(context) {
25
32
  if (!cacheData.service.provideCompletionItems) {
26
33
  continue;
27
34
  }
28
- const embeddedCompletionList = await cacheData.service.provideCompletionItems(map.virtualFileDocument, mapped, completionContext, token);
29
- if (!embeddedCompletionList) {
30
- cacheData.list.isIncomplete = false;
35
+ cacheData.list = await cacheData.service.provideCompletionItems(map.virtualFileDocument, mapped, completionContext, token);
36
+ if (!cacheData.list) {
31
37
  continue;
32
38
  }
33
- cacheData.list = (0, transform_1.transformCompletionList)(embeddedCompletionList, range => map.getSourceRange(range), map.virtualFileDocument, (newItem, oldItem) => newItem.data = {
34
- uri,
35
- original: {
36
- additionalTextEdits: oldItem.additionalTextEdits,
37
- textEdit: oldItem.textEdit,
38
- data: oldItem.data,
39
- },
40
- serviceIndex: cacheData.serviceIndex,
41
- virtualDocumentUri: map.virtualFileDocument.uri,
42
- });
39
+ for (const item of cacheData.list.items) {
40
+ item.data = {
41
+ uri,
42
+ original: {
43
+ additionalTextEdits: item.additionalTextEdits,
44
+ textEdit: item.textEdit,
45
+ data: item.data,
46
+ },
47
+ serviceIndex,
48
+ virtualDocumentUri: map.virtualFileDocument.uri,
49
+ };
50
+ }
51
+ cacheData.list = (0, transform_1.transformCompletionList)(cacheData.list, range => map.getSourceRange(range), map.virtualFileDocument);
43
52
  }
44
53
  }
45
54
  }
46
- else if (sourceFile) {
55
+ else {
47
56
  if (!cacheData.service.provideCompletionItems) {
48
57
  continue;
49
58
  }
50
59
  const document = context.documents.get(uri, sourceFile.languageId, sourceFile.snapshot);
51
- const completionList = await cacheData.service.provideCompletionItems(document, position, completionContext, token);
52
- if (!completionList) {
53
- cacheData.list.isIncomplete = false;
60
+ cacheData.list = await cacheData.service.provideCompletionItems(document, position, completionContext, token);
61
+ if (!cacheData.list) {
54
62
  continue;
55
63
  }
56
- completionList.items.forEach(item => {
64
+ for (const item of cacheData.list.items) {
57
65
  item.data = {
58
66
  uri,
59
67
  original: {
@@ -61,90 +69,25 @@ function register(context) {
61
69
  textEdit: item.textEdit,
62
70
  data: item.data,
63
71
  },
64
- serviceIndex: cacheData.serviceIndex,
72
+ serviceIndex,
65
73
  virtualDocumentUri: undefined,
66
74
  };
67
- });
75
+ }
68
76
  }
69
77
  }
70
78
  }
71
79
  else {
72
- const rootVirtualFile = context.language.files.get(uri)?.generated?.code;
73
- cache = {
80
+ lastResult = {
74
81
  uri,
75
- data: [],
76
- mainCompletion: undefined,
82
+ results: [],
77
83
  };
78
84
  // monky fix https://github.com/johnsoncodehk/volar/issues/1358
79
85
  let isFirstMapping = true;
80
- if (rootVirtualFile) {
81
- await (0, featureWorkers_1.visitEmbedded)(context, rootVirtualFile, async (_, map) => {
82
- const services = [...context.services]
83
- .filter(service => !context.disabledServicePlugins.has(service[1]))
84
- .sort((a, b) => sortServices(a[1], b[1]));
85
- let _data;
86
- for (const mapped of map.getGeneratedPositions(position, data => {
87
- _data = data;
88
- return (0, language_core_1.isCompletionEnabled)(data);
89
- })) {
90
- for (const service of services) {
91
- if (token.isCancellationRequested) {
92
- break;
93
- }
94
- if (!service[1].provideCompletionItems) {
95
- continue;
96
- }
97
- if (service[1].isAdditionalCompletion && !isFirstMapping) {
98
- continue;
99
- }
100
- if (completionContext?.triggerCharacter && !service[0].triggerCharacters?.includes(completionContext.triggerCharacter)) {
101
- continue;
102
- }
103
- const isAdditional = _data && typeof _data.completion === 'object' && _data.completion.isAdditional || service[1].isAdditionalCompletion;
104
- if (cache.mainCompletion && (!isAdditional || cache?.mainCompletion.documentUri !== map.virtualFileDocument.uri)) {
105
- continue;
106
- }
107
- // avoid duplicate items with .vue and .vue.html
108
- if (service[1].isAdditionalCompletion && cache?.data.some(data => data.service === service[1])) {
109
- continue;
110
- }
111
- const embeddedCompletionList = await service[1].provideCompletionItems(map.virtualFileDocument, mapped, completionContext, token);
112
- if (!embeddedCompletionList || !embeddedCompletionList.items.length) {
113
- continue;
114
- }
115
- if (typeof _data?.completion === 'object' && _data.completion.onlyImport) {
116
- embeddedCompletionList.items = embeddedCompletionList.items.filter(item => !!item.labelDetails);
117
- }
118
- if (!isAdditional) {
119
- cache.mainCompletion = { documentUri: map.virtualFileDocument.uri };
120
- }
121
- const completionList = (0, transform_1.transformCompletionList)(embeddedCompletionList, range => map.getSourceRange(range, language_core_1.isCompletionEnabled), map.virtualFileDocument, (newItem, oldItem) => newItem.data = {
122
- uri,
123
- original: {
124
- additionalTextEdits: oldItem.additionalTextEdits,
125
- textEdit: oldItem.textEdit,
126
- data: oldItem.data,
127
- },
128
- serviceIndex: context.services.indexOf(service),
129
- virtualDocumentUri: map.virtualFileDocument.uri,
130
- });
131
- cache.data.push({
132
- virtualDocumentUri: map.virtualFileDocument.uri,
133
- service: service[1],
134
- serviceIndex: context.services.indexOf(service),
135
- list: completionList,
136
- });
137
- }
138
- isFirstMapping = false;
139
- }
140
- return true;
141
- });
142
- }
143
- if (sourceFile) {
144
- const document = context.documents.get(uri, sourceFile.languageId, sourceFile.snapshot);
145
- const services = [...context.services]
146
- .filter(service => !context.disabledServicePlugins.has(service[1]))
147
- .sort((a, b) => sortServices(a[1], b[1]));
86
+ let mainCompletionUri;
87
+ const services = [...context.services]
88
+ .filter(service => !context.disabledServicePlugins.has(service[1]))
89
+ .sort((a, b) => sortServices(a[1], b[1]));
90
+ const worker = async (document, position, map, codeInfo) => {
148
91
  for (const service of services) {
149
92
  if (token.isCancellationRequested) {
150
93
  break;
@@ -158,21 +101,26 @@ function register(context) {
158
101
  if (completionContext?.triggerCharacter && !service[0].triggerCharacters?.includes(completionContext.triggerCharacter)) {
159
102
  continue;
160
103
  }
161
- if (cache.mainCompletion && (!service[1].isAdditionalCompletion || cache.mainCompletion.documentUri !== document.uri)) {
104
+ const isAdditional = (codeInfo && typeof codeInfo.completion === 'object' && codeInfo.completion.isAdditional) || service[1].isAdditionalCompletion;
105
+ if (mainCompletionUri && (!isAdditional || mainCompletionUri !== document.uri)) {
162
106
  continue;
163
107
  }
164
108
  // avoid duplicate items with .vue and .vue.html
165
- if (service[1].isAdditionalCompletion && cache?.data.some(data => data.service === service[1])) {
109
+ if (service[1].isAdditionalCompletion && lastResult?.results.some(data => data.service === service[1])) {
166
110
  continue;
167
111
  }
168
- const completionList = await service[1].provideCompletionItems(document, position, completionContext, token);
112
+ let completionList = await service[1].provideCompletionItems(document, position, completionContext, token);
169
113
  if (!completionList || !completionList.items.length) {
170
114
  continue;
171
115
  }
172
- if (!service[1].isAdditionalCompletion) {
173
- cache.mainCompletion = { documentUri: document.uri };
116
+ if (typeof codeInfo?.completion === 'object' && codeInfo.completion.onlyImport) {
117
+ completionList.items = completionList.items.filter(item => !!item.labelDetails);
118
+ }
119
+ if (!isAdditional) {
120
+ mainCompletionUri = document.uri;
174
121
  }
175
- completionList.items.forEach(item => {
122
+ const serviceIndex = context.services.indexOf(service);
123
+ for (const item of completionList.items) {
176
124
  item.data = {
177
125
  uri,
178
126
  original: {
@@ -180,28 +128,46 @@ function register(context) {
180
128
  textEdit: item.textEdit,
181
129
  data: item.data,
182
130
  },
183
- serviceIndex: context.services.indexOf(service),
184
- virtualDocumentUri: undefined,
131
+ serviceIndex,
132
+ virtualDocumentUri: map ? document.uri : undefined,
185
133
  };
186
- });
187
- cache.data.push({
188
- virtualDocumentUri: undefined,
134
+ }
135
+ if (map) {
136
+ completionList = (0, transform_1.transformCompletionList)(completionList, range => map.getSourceRange(range, language_core_1.isCompletionEnabled), document);
137
+ }
138
+ lastResult?.results.push({
139
+ virtualDocumentUri: map ? document.uri : undefined,
189
140
  service: service[1],
190
- serviceIndex: context.services.indexOf(service),
191
141
  list: completionList,
192
142
  });
193
143
  }
144
+ isFirstMapping = false;
145
+ };
146
+ if (sourceFile.generated) {
147
+ for (const map of (0, featureWorkers_1.eachEmbeddedDocument)(context, sourceFile.generated.code)) {
148
+ let _data;
149
+ for (const mappedPosition of map.getGeneratedPositions(position, data => {
150
+ _data = data;
151
+ return (0, language_core_1.isCompletionEnabled)(data);
152
+ })) {
153
+ await worker(map.virtualFileDocument, mappedPosition, map, _data);
154
+ }
155
+ }
156
+ }
157
+ else {
158
+ const document = context.documents.get(uri, sourceFile.languageId, sourceFile.snapshot);
159
+ await worker(document, position);
194
160
  }
195
161
  }
196
- return combineCompletionList(cache.data.map(cacheData => cacheData.list));
162
+ return combineCompletionList(lastResult.results.map(cacheData => cacheData.list));
197
163
  function sortServices(a, b) {
198
164
  return (b.isAdditionalCompletion ? -1 : 1) - (a.isAdditionalCompletion ? -1 : 1);
199
165
  }
200
166
  function combineCompletionList(lists) {
201
167
  return {
202
- isIncomplete: lists.some(list => list.isIncomplete),
203
- itemDefaults: lists.find(list => list.itemDefaults)?.itemDefaults,
204
- items: lists.map(list => list.items).flat(),
168
+ isIncomplete: lists.some(list => list?.isIncomplete),
169
+ itemDefaults: lists.find(list => list?.itemDefaults)?.itemDefaults,
170
+ items: lists.map(list => list?.items ?? []).flat(),
205
171
  };
206
172
  }
207
173
  };
@@ -5,5 +5,5 @@ import type { ServiceContext, ServicePluginInstance, ServicePlugin } from '../ty
5
5
  export declare function documentFeatureWorker<T>(context: ServiceContext, uri: string, valid: (map: SourceMapWithDocuments<CodeInformation>) => boolean, worker: (service: [ServicePlugin, ServicePluginInstance], document: TextDocument) => Thenable<T | null | undefined> | T | null | undefined, transformResult: (result: T, map?: SourceMapWithDocuments<CodeInformation>) => T | undefined, combineResult?: (results: T[]) => T): Promise<T | undefined>;
6
6
  export declare function languageFeatureWorker<T, K>(context: ServiceContext, uri: string, getReadDocParams: () => K, eachVirtualDocParams: (map: SourceMapWithDocuments<CodeInformation>) => Generator<K>, worker: (service: [ServicePlugin, ServicePluginInstance], document: TextDocument, params: K, map?: SourceMapWithDocuments<CodeInformation>) => Thenable<T | null | undefined> | T | null | undefined, transformResult: (result: T, map?: SourceMapWithDocuments<CodeInformation>) => T | undefined, combineResult?: (results: T[]) => T): Promise<T | undefined>;
7
7
  export declare function safeCall<T>(cb: () => Thenable<T> | T, errorMsg?: string): Promise<T | undefined>;
8
- export declare function visitEmbedded(context: ServiceContext, current: VirtualCode, cb: (file: VirtualCode, sourceMap: SourceMapWithDocuments<CodeInformation>) => Promise<boolean>, rootFile?: VirtualCode<string>): Promise<boolean>;
8
+ export declare function eachEmbeddedDocument(context: ServiceContext, current: VirtualCode, rootCode?: VirtualCode<string>): Generator<SourceMapWithDocuments<CodeInformation>>;
9
9
  export declare function getEmbeddedFilesByLevel(context: ServiceContext, sourceFileUri: string, rootFile: VirtualCode, level: number): VirtualCode<string>[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEmbeddedFilesByLevel = exports.visitEmbedded = exports.safeCall = exports.languageFeatureWorker = exports.documentFeatureWorker = void 0;
3
+ exports.getEmbeddedFilesByLevel = exports.eachEmbeddedDocument = exports.safeCall = exports.languageFeatureWorker = exports.documentFeatureWorker = void 0;
4
4
  async function documentFeatureWorker(context, uri, valid, worker, transformResult, combineResult) {
5
5
  return languageFeatureWorker(context, uri, () => void 0, function* (map) {
6
6
  if (valid(map)) {
@@ -16,7 +16,7 @@ async function languageFeatureWorker(context, uri, getReadDocParams, eachVirtual
16
16
  }
17
17
  let results = [];
18
18
  if (sourceFile.generated) {
19
- await visitEmbedded(context, sourceFile.generated.code, async (_file, map) => {
19
+ for (const map of eachEmbeddedDocument(context, sourceFile.generated.code)) {
20
20
  for (const mappedArg of eachVirtualDocParams(map)) {
21
21
  for (const [serviceId, service] of Object.entries(context.services)) {
22
22
  if (context.disabledServicePlugins.has(service[1])) {
@@ -32,12 +32,11 @@ async function languageFeatureWorker(context, uri, getReadDocParams, eachVirtual
32
32
  }
33
33
  results.push(result);
34
34
  if (!combineResult) {
35
- return false;
35
+ break;
36
36
  }
37
37
  }
38
38
  }
39
- return true;
40
- });
39
+ }
41
40
  }
42
41
  else {
43
42
  const document = context.documents.get(uri, sourceFile.languageId, sourceFile.snapshot);
@@ -77,23 +76,19 @@ async function safeCall(cb, errorMsg) {
77
76
  }
78
77
  }
79
78
  exports.safeCall = safeCall;
80
- async function visitEmbedded(context, current, cb, rootFile = current) {
81
- for (const embedded of current.embeddedCodes) {
82
- if (!await visitEmbedded(context, embedded, cb, rootFile)) {
83
- return false;
84
- }
79
+ function* eachEmbeddedDocument(context, current, rootCode = current) {
80
+ for (const embeddedCode of current.embeddedCodes) {
81
+ yield* eachEmbeddedDocument(context, embeddedCode, rootCode);
85
82
  }
86
83
  for (const map of context.documents.getMaps(current)) {
87
84
  const sourceFile = context.language.files.get(map.sourceFileDocument.uri);
88
- if (sourceFile?.generated?.code === rootFile
89
- && !context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(context.language.files.getByVirtualCode(current).id, current.id))
90
- && !await cb(current, map)) {
91
- return false;
85
+ if (sourceFile?.generated?.code === rootCode
86
+ && !context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(context.language.files.getByVirtualCode(current).id, current.id))) {
87
+ yield map;
92
88
  }
93
89
  }
94
- return true;
95
90
  }
96
- exports.visitEmbedded = visitEmbedded;
91
+ exports.eachEmbeddedDocument = eachEmbeddedDocument;
97
92
  function getEmbeddedFilesByLevel(context, sourceFileUri, rootFile, level) {
98
93
  const embeddedFilesByLevel = [[rootFile]];
99
94
  while (true) {
@@ -1,7 +1,8 @@
1
1
  import type * as vscode from 'vscode-languageserver-protocol';
2
+ import type { TextDocument } from 'vscode-languageserver-textdocument';
2
3
  import type { ServiceContext } from '../types';
3
4
  export declare function transformCompletionItem<T extends vscode.CompletionItem>(item: T, getOtherRange: (range: vscode.Range) => vscode.Range | undefined, document: vscode.TextDocument): T;
4
- export declare function transformCompletionList<T extends vscode.CompletionList>(completionList: T, getOtherRange: (range: vscode.Range) => vscode.Range | undefined, document: vscode.TextDocument, onItem?: (newItem: vscode.CompletionItem, oldItem: vscode.CompletionItem) => void): T;
5
+ export declare function transformCompletionList<T extends vscode.CompletionList>(completionList: T, getOtherRange: (range: vscode.Range) => vscode.Range | undefined, document: TextDocument): T;
5
6
  export declare function transformDocumentSymbol(symbol: vscode.DocumentSymbol, getOtherRange: (range: vscode.Range) => vscode.Range | undefined): vscode.DocumentSymbol | undefined;
6
7
  export declare function transformFoldingRanges(ranges: vscode.FoldingRange[], getOtherRange: (range: vscode.Range) => vscode.Range | undefined): vscode.FoldingRange[];
7
8
  export declare function transformHover<T extends vscode.Hover>(hover: T, getOtherRange: (range: vscode.Range) => vscode.Range | undefined): T | undefined;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pushEditToDocumentChanges = exports.transformWorkspaceEdit = exports.transformWorkspaceSymbol = exports.transformTextEdit = exports.transformSelectionRanges = exports.transformSelectionRange = exports.transformLocations = exports.transformLocation = exports.transformHover = exports.transformFoldingRanges = exports.transformDocumentSymbol = exports.transformCompletionList = exports.transformCompletionItem = void 0;
4
- const common_1 = require("./common");
5
4
  const language_core_1 = require("@volar/language-core");
5
+ const common_1 = require("./common");
6
6
  function transformCompletionItem(item, getOtherRange, document) {
7
7
  return {
8
8
  ...item,
@@ -15,7 +15,7 @@ function transformCompletionItem(item, getOtherRange, document) {
15
15
  };
16
16
  }
17
17
  exports.transformCompletionItem = transformCompletionItem;
18
- function transformCompletionList(completionList, getOtherRange, document, onItem) {
18
+ function transformCompletionList(completionList, getOtherRange, document) {
19
19
  return {
20
20
  isIncomplete: completionList.isIncomplete,
21
21
  itemDefaults: completionList.itemDefaults ? {
@@ -29,11 +29,7 @@ function transformCompletionList(completionList, getOtherRange, document, onItem
29
29
  : getOtherRange(completionList.itemDefaults.editRange)
30
30
  : undefined,
31
31
  } : undefined,
32
- items: completionList.items.map(item => {
33
- const newItem = transformCompletionItem(item, getOtherRange, document);
34
- onItem?.(newItem, item);
35
- return newItem;
36
- }),
32
+ items: completionList.items.map(item => transformCompletionItem(item, getOtherRange, document)),
37
33
  };
38
34
  }
39
35
  exports.transformCompletionList = transformCompletionList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-service",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,10 +12,10 @@
12
12
  "directory": "packages/language-service"
13
13
  },
14
14
  "dependencies": {
15
- "@volar/language-core": "2.0.0",
15
+ "@volar/language-core": "2.0.2",
16
16
  "vscode-languageserver-protocol": "^3.17.5",
17
17
  "vscode-languageserver-textdocument": "^1.0.11",
18
18
  "vscode-uri": "^3.0.8"
19
19
  },
20
- "gitHead": "95217136d2727bb7304443d91cfde3dbe711369c"
20
+ "gitHead": "ae8bccc570e6ee752693b1bfd9e815668b5f32b9"
21
21
  }