@volar/language-service 2.4.30 → 2.4.32-alpha.0

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.
@@ -7,14 +7,19 @@ const cancellation_1 = require("../utils/cancellation");
7
7
  const featureWorkers_1 = require("../utils/featureWorkers");
8
8
  const transform_1 = require("../utils/transform");
9
9
  function register(context) {
10
+ // The result of the most recently started request that finished without
11
+ // being cancelled: what a TriggerForIncompleteCompletions request refreshes.
12
+ // Only a finished request writes it; a request reads and fills its own result.
10
13
  let lastResult;
14
+ let started = 0;
11
15
  return async (uri, position, completionContext = {
12
16
  triggerKind: 1,
13
17
  }, token = cancellation_1.NoneCancellationToken) => {
14
- // The results of this request. `lastResult` is only a cache for the
15
- // TriggerForIncompleteCompletions case; requests that overlap must not
16
- // push into each other's list.
17
- let currentResult;
18
+ const currentResult = {
19
+ uri,
20
+ order: ++started,
21
+ results: [],
22
+ };
18
23
  let langaugeIdAndSnapshot;
19
24
  let sourceScript;
20
25
  const decoded = context.decodeEmbeddedDocumentUri(uri);
@@ -33,8 +38,11 @@ function register(context) {
33
38
  }
34
39
  if (completionContext?.triggerKind === 3
35
40
  && lastResult?.uri.toString() === uri.toString()) {
36
- currentResult = lastResult;
41
+ currentResult.results = lastResult.results.map(cacheData => ({ ...cacheData }));
37
42
  for (const cacheData of currentResult.results) {
43
+ if (token.isCancellationRequested) {
44
+ break;
45
+ }
38
46
  if (!cacheData.list?.isIncomplete) {
39
47
  continue;
40
48
  }
@@ -109,10 +117,6 @@ function register(context) {
109
117
  }
110
118
  }
111
119
  else {
112
- currentResult = lastResult = {
113
- uri,
114
- results: [],
115
- };
116
120
  // monky fix https://github.com/johnsoncodehk/volar/issues/1358
117
121
  let isFirstMapping = true;
118
122
  let mainCompletionUri;
@@ -198,6 +202,9 @@ function register(context) {
198
202
  await worker(document, position);
199
203
  }
200
204
  }
205
+ if (!token.isCancellationRequested && (!lastResult || currentResult.order > lastResult.order)) {
206
+ lastResult = currentResult;
207
+ }
201
208
  return combineCompletionList(currentResult.results.map(cacheData => cacheData.list));
202
209
  function sortServices(a, b) {
203
210
  return (b.isAdditionalCompletion ? -1 : 1) - (a.isAdditionalCompletion ? -1 : 1);
@@ -45,10 +45,19 @@ function register(context) {
45
45
  }
46
46
  }
47
47
  }
48
+ // The formatted copy is generated by the plugin of the document itself, not routed like a new script.
49
+ // Its ID differs only in the fragment, so a plugin that routes or names files by path sees the same file,
50
+ // while one that caches virtual codes by ID does not overwrite the document's own.
51
+ const { languagePlugin } = sourceScript.generated;
52
+ const tempId = sourceScript.id.with({ fragment: 'volar-formatting' });
53
+ const codegenCtx = {
54
+ getAssociatedScript: id => context.language.scripts.get(id),
55
+ };
56
+ let tempVirtualFile;
48
57
  try {
49
58
  const originalDocument = document;
50
59
  let tempSourceSnapshot = sourceScript.snapshot;
51
- let tempVirtualFile = context.language.scripts.set(vscode_uri_1.URI.parse(sourceScript.id.toString() + '.tmp'), sourceScript.snapshot, sourceScript.languageId, [sourceScript.generated.languagePlugin])?.generated?.root;
60
+ tempVirtualFile = languagePlugin.createVirtualCode?.(tempId, sourceScript.languageId, tempSourceSnapshot, codegenCtx);
52
61
  if (!tempVirtualFile) {
53
62
  return;
54
63
  }
@@ -108,8 +117,12 @@ function register(context) {
108
117
  const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
109
118
  document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
110
119
  tempSourceSnapshot = (0, common_1.stringToSnapshot)(newText);
111
- tempVirtualFile = context.language.scripts.set(vscode_uri_1.URI.parse(sourceScript.id.toString() + '.tmp'), tempSourceSnapshot, sourceScript.languageId, [sourceScript.generated.languagePlugin])?.generated?.root;
120
+ const previousVirtualFile = tempVirtualFile;
121
+ tempVirtualFile = languagePlugin.updateVirtualCode
122
+ ? languagePlugin.updateVirtualCode(tempId, previousVirtualFile, tempSourceSnapshot, codegenCtx)
123
+ : languagePlugin.createVirtualCode?.(tempId, sourceScript.languageId, tempSourceSnapshot, codegenCtx);
112
124
  if (!tempVirtualFile) {
125
+ languagePlugin.disposeVirtualCode?.(tempId, previousVirtualFile);
113
126
  break;
114
127
  }
115
128
  }
@@ -128,7 +141,9 @@ function register(context) {
128
141
  return [textEdit];
129
142
  }
130
143
  finally {
131
- context.language.scripts.delete(vscode_uri_1.URI.parse(sourceScript.id.toString() + '.tmp'));
144
+ if (tempVirtualFile) {
145
+ languagePlugin.disposeVirtualCode?.(tempId, tempVirtualFile);
146
+ }
132
147
  }
133
148
  async function tryFormat(sourceDocument, document, sourceScript, virtualCode, embeddedLevel, rangeOrPosition, ch) {
134
149
  if (context.disabledEmbeddedDocumentUris.get(vscode_uri_1.URI.parse(document.uri))) {
@@ -37,9 +37,18 @@ function createLocationSet() {
37
37
  }
38
38
  }
39
39
  function withCodeAction(items) {
40
- return dedupe(items, item => [
41
- item.title,
42
- ].join(':'));
40
+ return dedupe(items, item => {
41
+ // Two actions are the same when they do the same thing to the source file.
42
+ if (item.edit || item.command) {
43
+ // The effect is here (the edit is already mapped to the source file): compare it.
44
+ return JSON.stringify([item.title, item.kind, item.edit, item.command]);
45
+ }
46
+ // Resolved lazily: the effect is known only to the plugin, after resolve. The plugin's
47
+ // data is the identity of an unresolved action, so actions are merged only when the
48
+ // same plugin gave them the same data, never by title alone.
49
+ const data = item.data;
50
+ return JSON.stringify([item.title, item.kind, data?.pluginIndex, data?.original?.data]);
51
+ });
43
52
  }
44
53
  function withTextEdits(items) {
45
54
  return dedupe(items, item => [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-service",
3
- "version": "2.4.30",
3
+ "version": "2.4.32-alpha.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/language-service"
13
13
  },
14
14
  "dependencies": {
15
- "@volar/language-core": "2.4.30",
15
+ "@volar/language-core": "2.4.32-alpha.0",
16
16
  "vscode-languageserver-protocol": "^3.17.5",
17
17
  "vscode-languageserver-textdocument": "^1.0.11",
18
18
  "vscode-uri": "^3.0.8"