@volar/language-core 1.7.5 → 1.7.7

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.
@@ -5,6 +5,9 @@ const virtualFiles_1 = require("./virtualFiles");
5
5
  ;
6
6
  function createLanguageContext(rawHost, languages) {
7
7
  let host = rawHost;
8
+ let lastRootFiles = new Map();
9
+ let lastProjectVersion;
10
+ const virtualFiles = (0, virtualFiles_1.createVirtualFiles)(languages);
8
11
  for (const language of languages.reverse()) {
9
12
  if (language.resolveHost) {
10
13
  const pastHost = host;
@@ -23,8 +26,6 @@ function createLanguageContext(rawHost, languages) {
23
26
  });
24
27
  }
25
28
  }
26
- let lastProjectVersion;
27
- const virtualFiles = (0, virtualFiles_1.createVirtualFiles)(languages);
28
29
  return {
29
30
  rawHost,
30
31
  host,
@@ -40,27 +41,27 @@ function createLanguageContext(rawHost, languages) {
40
41
  const shouldUpdate = newProjectVersion !== lastProjectVersion;
41
42
  if (!shouldUpdate)
42
43
  return;
43
- lastProjectVersion = newProjectVersion;
44
- const remainRootFiles = new Set(host.getScriptFileNames());
45
- for (const { fileName, snapshot } of virtualFiles.allSources()) {
44
+ const nowRootFiles = new Map();
45
+ const remainRootFiles = new Set(lastRootFiles.keys());
46
+ for (const rootFileName of host.getScriptFileNames()) {
47
+ nowRootFiles.set(rootFileName, host.getScriptSnapshot(rootFileName));
48
+ }
49
+ for (const [fileName, snapshot] of nowRootFiles) {
46
50
  remainRootFiles.delete(fileName);
47
- const newSnapshot = host.getScriptSnapshot(fileName);
48
- if (!newSnapshot) {
49
- // delete
50
- virtualFiles.deleteSource(fileName);
51
- }
52
- else if (newSnapshot !== snapshot) {
53
- // update
54
- virtualFiles.updateSource(fileName, newSnapshot, host.getLanguageId?.(fileName));
51
+ if (lastRootFiles.get(fileName) !== nowRootFiles.get(fileName)) {
52
+ if (snapshot) {
53
+ virtualFiles.updateSource(fileName, snapshot, host.getLanguageId?.(fileName));
54
+ }
55
+ else {
56
+ virtualFiles.deleteSource(fileName);
57
+ }
55
58
  }
56
59
  }
57
- // create
58
- for (const fileName of [...remainRootFiles]) {
59
- const snapshot = host.getScriptSnapshot(fileName);
60
- if (snapshot) {
61
- virtualFiles.updateSource(fileName, snapshot, host.getLanguageId?.(fileName));
62
- }
60
+ for (const fileName of remainRootFiles) {
61
+ virtualFiles.deleteSource(fileName);
63
62
  }
63
+ lastRootFiles = nowRootFiles;
64
+ lastProjectVersion = newProjectVersion;
64
65
  }
65
66
  }
66
67
  exports.createLanguageContext = createLanguageContext;
@@ -8,7 +8,6 @@ function createVirtualFiles(languages) {
8
8
  const virtualFiles = new Map();
9
9
  const virtualFileMaps = new WeakMap();
10
10
  const virtualFileToMirrorMap = new WeakMap();
11
- let sourceFilesDirty = true;
12
11
  return {
13
12
  allSources() {
14
13
  return Array.from(sourceFiles.values());
@@ -24,16 +23,18 @@ function createVirtualFiles(languages) {
24
23
  }
25
24
  else {
26
25
  value.snapshot = snapshot;
26
+ deleteVirtualFiles(value);
27
27
  value.language.updateVirtualFile(value.root, snapshot);
28
- sourceFilesDirty = true;
28
+ updateVirtualFiles(value);
29
29
  return value.root; // updated
30
30
  }
31
31
  }
32
32
  for (const language of languages) {
33
33
  const virtualFile = language.createVirtualFile(fileName, snapshot, languageId);
34
34
  if (virtualFile) {
35
- sourceFiles.set(key, { fileName, languageId, snapshot, root: virtualFile, language });
36
- sourceFilesDirty = true;
35
+ const source = { fileName, languageId, snapshot, root: virtualFile, language };
36
+ sourceFiles.set(key, source);
37
+ updateVirtualFiles(source);
37
38
  return virtualFile; // created
38
39
  }
39
40
  }
@@ -44,7 +45,7 @@ function createVirtualFiles(languages) {
44
45
  if (value) {
45
46
  value.language.deleteVirtualFile?.(value.root);
46
47
  sourceFiles.delete(key); // deleted
47
- sourceFilesDirty = true;
48
+ deleteVirtualFiles(value);
48
49
  }
49
50
  },
50
51
  getSource(fileName) {
@@ -55,27 +56,25 @@ function createVirtualFiles(languages) {
55
56
  getMirrorMap: getMirrorMap,
56
57
  getMaps: getMapsByVirtualFile,
57
58
  hasVirtualFile(fileName) {
58
- return !!getVirtualFileToSourceFileMap().get(normalizePath(fileName));
59
+ return !!virtualFiles.get(normalizePath(fileName));
59
60
  },
60
61
  getVirtualFile(fileName) {
61
- const sourceAndVirtual = getVirtualFileToSourceFileMap().get(normalizePath(fileName));
62
+ const sourceAndVirtual = virtualFiles.get(normalizePath(fileName));
62
63
  if (sourceAndVirtual) {
63
64
  return [sourceAndVirtual.virtualFile, sourceAndVirtual.source];
64
65
  }
65
66
  return [undefined, undefined];
66
67
  },
67
68
  };
68
- function getVirtualFileToSourceFileMap() {
69
- if (sourceFilesDirty) {
70
- sourceFilesDirty = false;
71
- virtualFiles.clear();
72
- for (const [_, row] of sourceFiles) {
73
- forEachEmbeddedFile(row.root, file => {
74
- virtualFiles.set(normalizePath(file.fileName), { virtualFile: file, source: row });
75
- });
76
- }
77
- }
78
- return virtualFiles;
69
+ function deleteVirtualFiles(source) {
70
+ forEachEmbeddedFile(source.root, file => {
71
+ virtualFiles.delete(normalizePath(file.fileName));
72
+ });
73
+ }
74
+ function updateVirtualFiles(source) {
75
+ forEachEmbeddedFile(source.root, file => {
76
+ virtualFiles.set(normalizePath(file.fileName), { virtualFile: file, source });
77
+ });
79
78
  }
80
79
  function getMapsByVirtualFile(virtualFile) {
81
80
  if (!virtualFileMaps.has(virtualFile.snapshot)) {
@@ -87,7 +86,7 @@ function createVirtualFiles(languages) {
87
86
  return [sourceFileName, source.snapshot];
88
87
  }
89
88
  else {
90
- const source = getVirtualFileToSourceFileMap().get(normalizePath(virtualFile.fileName)).source;
89
+ const source = virtualFiles.get(normalizePath(virtualFile.fileName)).source;
91
90
  return [source.fileName, source.snapshot];
92
91
  }
93
92
  }, virtualFileMaps.get(virtualFile.snapshot));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-core",
3
- "version": "1.7.5",
3
+ "version": "1.7.7",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/language-core"
14
14
  },
15
15
  "dependencies": {
16
- "@volar/source-map": "1.7.5"
16
+ "@volar/source-map": "1.7.7"
17
17
  },
18
- "gitHead": "57fa4f05df4c593aa4a7b15c159f864d407bd1ac"
18
+ "gitHead": "ad13290da9f223f6d198d858faf5349933112200"
19
19
  }