@volar/language-core 2.3.0-alpha.3 → 2.3.0-alpha.5

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 CHANGED
@@ -3,9 +3,6 @@ export * from './lib/editorFeatures';
3
3
  export * from './lib/linkedCodeMap';
4
4
  export * from './lib/types';
5
5
  export * from './lib/utils';
6
- import { SourceMap } from '@volar/source-map';
7
- import type * as ts from 'typescript';
8
- import type { CodeInformation, Language, LanguagePlugin, SourceScript, VirtualCode } from './lib/types';
6
+ import type { Language, LanguagePlugin, SourceScript, VirtualCode } from './lib/types';
9
7
  export declare function createLanguage<T>(plugins: LanguagePlugin<T>[], scriptRegistry: Map<T, SourceScript<T>>, sync: (id: T) => void): Language<T>;
10
- export declare function updateVirtualCodeMapOfMap<T>(virtualCode: VirtualCode, mapOfMap: Map<T, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>, getSourceSnapshot: (source: string | undefined) => [T, ts.IScriptSnapshot] | undefined): void;
11
8
  export declare function forEachEmbeddedCode(virtualCode: VirtualCode): Generator<VirtualCode>;
package/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.forEachEmbeddedCode = exports.updateVirtualCodeMapOfMap = exports.createLanguage = void 0;
17
+ exports.forEachEmbeddedCode = exports.createLanguage = void 0;
18
18
  __exportStar(require("@volar/source-map"), exports);
19
19
  __exportStar(require("./lib/editorFeatures"), exports);
20
20
  __exportStar(require("./lib/linkedCodeMap"), exports);
@@ -23,12 +23,15 @@ __exportStar(require("./lib/utils"), exports);
23
23
  const source_map_1 = require("@volar/source-map");
24
24
  const linkedCodeMap_1 = require("./lib/linkedCodeMap");
25
25
  function createLanguage(plugins, scriptRegistry, sync) {
26
- const virtualCodeToSourceFileMap = new WeakMap();
27
- const virtualCodeToMaps = new WeakMap();
26
+ const virtualCodeToSourceScriptMap = new WeakMap();
27
+ const virtualCodeToSourceMap = new WeakMap();
28
28
  const virtualCodeToLinkedCodeMap = new WeakMap();
29
29
  return {
30
30
  plugins,
31
31
  scripts: {
32
+ fromVirtualCode(virtualCode) {
33
+ return virtualCodeToSourceScriptMap.get(virtualCode);
34
+ },
32
35
  get(id) {
33
36
  sync(id);
34
37
  return scriptRegistry.get(id);
@@ -57,12 +60,15 @@ function createLanguage(plugins, scriptRegistry, sync) {
57
60
  // snapshot updated
58
61
  sourceScript.snapshot = snapshot;
59
62
  if (sourceScript.generated) {
60
- const newVirtualCode = sourceScript.generated.languagePlugin.updateVirtualCode?.(id, sourceScript.generated.root, snapshot);
63
+ const { updateVirtualCode, createVirtualCode } = sourceScript.generated.languagePlugin;
64
+ const newVirtualCode = updateVirtualCode
65
+ ? updateVirtualCode(id, sourceScript.generated.root, snapshot)
66
+ : createVirtualCode?.(id, languageId, snapshot);
61
67
  if (newVirtualCode) {
62
68
  sourceScript.generated.root = newVirtualCode;
63
69
  sourceScript.generated.embeddedCodes.clear();
64
70
  for (const code of forEachEmbeddedCode(sourceScript.generated.root)) {
65
- virtualCodeToSourceFileMap.set(code, sourceScript);
71
+ virtualCodeToSourceScriptMap.set(code, sourceScript);
66
72
  sourceScript.generated.embeddedCodes.set(code.id, code);
67
73
  }
68
74
  return sourceScript;
@@ -91,7 +97,7 @@ function createLanguage(plugins, scriptRegistry, sync) {
91
97
  embeddedCodes: new Map(),
92
98
  };
93
99
  for (const code of forEachEmbeddedCode(virtualCode)) {
94
- virtualCodeToSourceFileMap.set(code, sourceScript);
100
+ virtualCodeToSourceScriptMap.set(code, sourceScript);
95
101
  sourceScript.generated.embeddedCodes.set(code.id, code);
96
102
  }
97
103
  break;
@@ -111,76 +117,39 @@ function createLanguage(plugins, scriptRegistry, sync) {
111
117
  },
112
118
  },
113
119
  maps: {
114
- get(virtualCode, scriptId) {
115
- if (!scriptId) {
116
- const sourceScript = virtualCodeToSourceFileMap.get(virtualCode);
117
- if (!sourceScript) {
118
- return;
119
- }
120
- scriptId = sourceScript.id;
121
- }
122
- for (const [id, [_snapshot, map]] of this.forEach(virtualCode)) {
123
- if (id === scriptId) {
124
- return map;
125
- }
126
- }
127
- },
128
- forEach(virtualCode) {
129
- let map = virtualCodeToMaps.get(virtualCode.snapshot);
130
- if (!map) {
131
- map = new Map();
132
- virtualCodeToMaps.set(virtualCode.snapshot, map);
133
- }
134
- updateVirtualCodeMapOfMap(virtualCode, map, id => {
135
- if (id) {
120
+ get(virtualCode) {
121
+ const sourceScript = virtualCodeToSourceScriptMap.get(virtualCode);
122
+ let mapCache = virtualCodeToSourceMap.get(virtualCode.snapshot);
123
+ if (mapCache?.[0] !== sourceScript.snapshot) {
124
+ if (virtualCode.mappings.some(mapping => mapping.source)) {
136
125
  throw 'not implemented';
137
- // const sourceScript = sourceScripts.get(id)!;
138
- // return [id, sourceScript.snapshot];
139
- }
140
- else {
141
- const sourceScript = virtualCodeToSourceFileMap.get(virtualCode);
142
- return [sourceScript.id, sourceScript.snapshot];
143
126
  }
144
- });
145
- return map;
127
+ virtualCodeToSourceMap.set(virtualCode.snapshot, mapCache = [
128
+ sourceScript.snapshot,
129
+ new source_map_1.SourceMap(virtualCode.mappings),
130
+ ]);
131
+ }
132
+ return mapCache[1];
146
133
  },
147
134
  },
148
135
  linkedCodeMaps: {
149
136
  get(virtualCode) {
150
- if (!virtualCodeToLinkedCodeMap.has(virtualCode.snapshot)) {
151
- virtualCodeToLinkedCodeMap.set(virtualCode.snapshot, virtualCode.linkedCodeMappings
152
- ? new linkedCodeMap_1.LinkedCodeMap(virtualCode.linkedCodeMappings)
153
- : undefined);
137
+ const sourceScript = virtualCodeToSourceScriptMap.get(virtualCode);
138
+ let mapCache = virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
139
+ if (mapCache?.[0] !== sourceScript.snapshot) {
140
+ virtualCodeToLinkedCodeMap.set(virtualCode.snapshot, mapCache = [
141
+ sourceScript.snapshot,
142
+ virtualCode.linkedCodeMappings
143
+ ? new linkedCodeMap_1.LinkedCodeMap(virtualCode.linkedCodeMappings)
144
+ : undefined,
145
+ ]);
154
146
  }
155
- return virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
147
+ return mapCache[1];
156
148
  },
157
149
  },
158
150
  };
159
151
  }
160
152
  exports.createLanguage = createLanguage;
161
- function updateVirtualCodeMapOfMap(virtualCode, mapOfMap, getSourceSnapshot) {
162
- const sources = new Set();
163
- if (!virtualCode.mappings.length) {
164
- const source = getSourceSnapshot(undefined);
165
- if (source) {
166
- mapOfMap.set(source[0], [source[1], new source_map_1.SourceMap([])]);
167
- }
168
- }
169
- for (const mapping of virtualCode.mappings) {
170
- if (sources.has(mapping.source)) {
171
- continue;
172
- }
173
- sources.add(mapping.source);
174
- const source = getSourceSnapshot(mapping.source);
175
- if (!source) {
176
- continue;
177
- }
178
- if (!mapOfMap.has(source[0]) || mapOfMap.get(source[0])[0] !== source[1]) {
179
- mapOfMap.set(source[0], [source[1], new source_map_1.SourceMap(virtualCode.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
180
- }
181
- }
182
- }
183
- exports.updateVirtualCodeMapOfMap = updateVirtualCodeMapOfMap;
184
153
  function* forEachEmbeddedCode(virtualCode) {
185
154
  yield virtualCode;
186
155
  if (virtualCode.embeddedCodes) {
package/lib/types.d.ts CHANGED
@@ -7,10 +7,10 @@ export interface Language<T> {
7
7
  get(id: T): SourceScript<T> | undefined;
8
8
  set(id: T, snapshot: ts.IScriptSnapshot, languageId?: string, plugins?: LanguagePlugin<T>[]): SourceScript<T> | undefined;
9
9
  delete(id: T): void;
10
+ fromVirtualCode(virtualCode: VirtualCode): SourceScript<T>;
10
11
  };
11
12
  maps: {
12
- get(virtualCode: VirtualCode, scriptId?: T): SourceMap<CodeInformation> | undefined;
13
- forEach(virtualCode: VirtualCode): Map<T, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>;
13
+ get(virtualCode: VirtualCode): SourceMap<CodeInformation>;
14
14
  };
15
15
  linkedCodeMaps: {
16
16
  get(virtualCode: VirtualCode): LinkedCodeMap | undefined;
@@ -22,7 +22,7 @@ export interface Language<T> {
22
22
  sync?(): Promise<number>;
23
23
  };
24
24
  languageServiceHost: ts.LanguageServiceHost;
25
- getExtraServiceScript(fileName: string): [SourceScript<T>, TypeScriptExtraServiceScript] | undefined;
25
+ getExtraServiceScript(fileName: string): TypeScriptExtraServiceScript | undefined;
26
26
  asScriptId(fileName: string): T;
27
27
  asFileName(scriptId: T): string;
28
28
  };
@@ -80,26 +80,37 @@ export interface TypeScriptExtraServiceScript extends TypeScriptServiceScript {
80
80
  fileName: string;
81
81
  }
82
82
  export interface LanguagePlugin<T, K extends VirtualCode = VirtualCode> {
83
+ /**
84
+ * For files that are not opened in the IDE, the language ID will not be synchronized to the language server, so a hook is needed to parse the language ID of files that are known extension but not opened in the IDE.
85
+ */
83
86
  getLanguageId(scriptId: T): string | undefined;
87
+ /**
88
+ * Generate a virtual code.
89
+ */
84
90
  createVirtualCode?(scriptId: T, languageId: string, snapshot: ts.IScriptSnapshot): K | undefined;
91
+ /**
92
+ * Incremental update a virtual code. If not provide, call createVirtualCode again.
93
+ */
85
94
  updateVirtualCode?(scriptId: T, virtualCode: K, newSnapshot: ts.IScriptSnapshot): K | undefined;
95
+ /**
96
+ * Cleanup a virtual code.
97
+ */
86
98
  disposeVirtualCode?(scriptId: T, virtualCode: K): void;
87
- typescript?: {
88
- /**
89
- * LSP + TS Plugin
90
- */
91
- extraFileExtensions: ts.FileExtensionInfo[];
92
- /**
93
- * LSP + TS Plugin
94
- */
95
- getServiceScript(rootVirtualCode: K): TypeScriptServiceScript | undefined;
96
- /**
97
- * LSP only
98
- */
99
- getExtraServiceScripts?(fileName: string, rootVirtualCode: K): TypeScriptExtraServiceScript[];
100
- /**
101
- * LSP only
102
- */
103
- resolveLanguageServiceHost?(host: ts.LanguageServiceHost): ts.LanguageServiceHost;
104
- };
99
+ typescript?: TypeScriptGenericOptions<K> & TypeScriptNonTSPluginOptions<K>;
100
+ }
101
+ /**
102
+ * The following options available to all situations.
103
+ */
104
+ interface TypeScriptGenericOptions<T> {
105
+ extraFileExtensions: ts.FileExtensionInfo[];
106
+ resolveHiddenExtensions?: boolean;
107
+ getServiceScript(rootVirtualCode: T): TypeScriptServiceScript | undefined;
108
+ }
109
+ /**
110
+ * The following options will not be available in TS plugin.
111
+ */
112
+ interface TypeScriptNonTSPluginOptions<T> {
113
+ getExtraServiceScripts?(fileName: string, rootVirtualCode: T): TypeScriptExtraServiceScript[];
114
+ resolveLanguageServiceHost?(host: ts.LanguageServiceHost): ts.LanguageServiceHost;
105
115
  }
116
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-core",
3
- "version": "2.3.0-alpha.3",
3
+ "version": "2.3.0-alpha.5",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/language-core"
13
13
  },
14
14
  "dependencies": {
15
- "@volar/source-map": "2.3.0-alpha.3"
15
+ "@volar/source-map": "2.3.0-alpha.5"
16
16
  },
17
- "gitHead": "d322a122012dc339a3ac5139220e91649d508522"
17
+ "gitHead": "c3f6867de39095913ceaa2256ad35a9ca1a90487"
18
18
  }