@volar/language-core 2.1.6 → 2.2.0-alpha.1

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
@@ -1,7 +1,12 @@
1
+ export * from '@volar/source-map';
1
2
  export * from './lib/editorFeatures';
2
- export * from './lib/fileRegistry';
3
3
  export * from './lib/linkedCodeMap';
4
4
  export * from './lib/types';
5
5
  export * from './lib/utils';
6
- export * from '@volar/source-map';
6
+ import { SourceMap } from '@volar/source-map';
7
+ import type * as ts from 'typescript';
8
+ import type { CodeInformation, Language, LanguagePlugin, VirtualCode } from './lib/types';
9
+ export declare function createLanguage(plugins: LanguagePlugin[], caseSensitive: boolean, sync: (id: string) => void): Language;
10
+ export declare function updateVirtualCodeMapOfMap(virtualCode: VirtualCode, mapOfMap: Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>, getSourceSnapshot: (id: string | undefined) => [string, ts.IScriptSnapshot] | undefined): void;
11
+ export declare function forEachEmbeddedCode(virtualCode: VirtualCode): Generator<VirtualCode>;
7
12
  export declare function resolveCommonLanguageId(fileNameOrUri: string): string;
package/index.js CHANGED
@@ -14,13 +14,158 @@ 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.resolveCommonLanguageId = void 0;
17
+ exports.resolveCommonLanguageId = exports.forEachEmbeddedCode = exports.updateVirtualCodeMapOfMap = exports.createLanguage = void 0;
18
+ __exportStar(require("@volar/source-map"), exports);
18
19
  __exportStar(require("./lib/editorFeatures"), exports);
19
- __exportStar(require("./lib/fileRegistry"), exports);
20
20
  __exportStar(require("./lib/linkedCodeMap"), exports);
21
21
  __exportStar(require("./lib/types"), exports);
22
22
  __exportStar(require("./lib/utils"), exports);
23
- __exportStar(require("@volar/source-map"), exports);
23
+ const source_map_1 = require("@volar/source-map");
24
+ const linkedCodeMap_1 = require("./lib/linkedCodeMap");
25
+ const utils_1 = require("./lib/utils");
26
+ function createLanguage(plugins, caseSensitive, sync) {
27
+ const sourceScripts = new utils_1.FileMap(caseSensitive);
28
+ const virtualCodeToSourceFileMap = new WeakMap();
29
+ const virtualCodeToMaps = new WeakMap();
30
+ const virtualCodeToLinkedCodeMap = new WeakMap();
31
+ return {
32
+ plugins,
33
+ scripts: {
34
+ get(id) {
35
+ sync(id);
36
+ return sourceScripts.get(id);
37
+ },
38
+ set(id, languageId, snapshot, _plugins = plugins) {
39
+ if (sourceScripts.has(id)) {
40
+ const sourceScript = sourceScripts.get(id);
41
+ if (sourceScript.languageId !== languageId) {
42
+ // languageId changed
43
+ this.delete(id);
44
+ return this.set(id, languageId, snapshot);
45
+ }
46
+ else if (sourceScript.snapshot !== snapshot) {
47
+ // snapshot updated
48
+ sourceScript.snapshot = snapshot;
49
+ if (sourceScript.generated) {
50
+ sourceScript.generated.root = sourceScript.generated.languagePlugin.updateVirtualCode(id, sourceScript.generated.root, snapshot);
51
+ sourceScript.generated.embeddedCodes.clear();
52
+ for (const code of forEachEmbeddedCode(sourceScript.generated.root)) {
53
+ virtualCodeToSourceFileMap.set(code, sourceScript);
54
+ sourceScript.generated.embeddedCodes.set(code.id, code);
55
+ }
56
+ }
57
+ return sourceScript;
58
+ }
59
+ else {
60
+ // not changed
61
+ return sourceScript;
62
+ }
63
+ }
64
+ else {
65
+ // created
66
+ const sourceScript = { id, languageId, snapshot };
67
+ sourceScripts.set(id, sourceScript);
68
+ for (const languagePlugin of _plugins) {
69
+ const virtualCode = languagePlugin.createVirtualCode(id, languageId, snapshot);
70
+ if (virtualCode) {
71
+ sourceScript.generated = {
72
+ root: virtualCode,
73
+ languagePlugin,
74
+ embeddedCodes: new Map(),
75
+ };
76
+ for (const code of forEachEmbeddedCode(virtualCode)) {
77
+ virtualCodeToSourceFileMap.set(code, sourceScript);
78
+ sourceScript.generated.embeddedCodes.set(code.id, code);
79
+ }
80
+ break;
81
+ }
82
+ }
83
+ return sourceScript;
84
+ }
85
+ },
86
+ delete(id) {
87
+ const value = sourceScripts.get(id);
88
+ if (value) {
89
+ if (value.generated) {
90
+ value.generated.languagePlugin.disposeVirtualCode?.(id, value.generated.root);
91
+ }
92
+ sourceScripts.delete(id);
93
+ }
94
+ },
95
+ },
96
+ maps: {
97
+ get(virtualCode, scriptId) {
98
+ if (!scriptId) {
99
+ const sourceScript = virtualCodeToSourceFileMap.get(virtualCode);
100
+ if (!sourceScript) {
101
+ return;
102
+ }
103
+ scriptId = sourceScript.id;
104
+ }
105
+ for (const [id, [_snapshot, map]] of this.forEach(virtualCode)) {
106
+ if (id === scriptId) {
107
+ return map;
108
+ }
109
+ }
110
+ },
111
+ forEach(virtualCode) {
112
+ let map = virtualCodeToMaps.get(virtualCode.snapshot);
113
+ if (!map) {
114
+ map = new Map();
115
+ virtualCodeToMaps.set(virtualCode.snapshot, map);
116
+ }
117
+ updateVirtualCodeMapOfMap(virtualCode, map, id => {
118
+ if (id) {
119
+ const sourceScript = sourceScripts.get(id);
120
+ return [id, sourceScript.snapshot];
121
+ }
122
+ else {
123
+ const sourceScript = virtualCodeToSourceFileMap.get(virtualCode);
124
+ return [sourceScript.id, sourceScript.snapshot];
125
+ }
126
+ });
127
+ return map;
128
+ },
129
+ },
130
+ linkedCodeMaps: {
131
+ get(virtualCode) {
132
+ if (!virtualCodeToLinkedCodeMap.has(virtualCode.snapshot)) {
133
+ virtualCodeToLinkedCodeMap.set(virtualCode.snapshot, virtualCode.linkedCodeMappings
134
+ ? new linkedCodeMap_1.LinkedCodeMap(virtualCode.linkedCodeMappings)
135
+ : undefined);
136
+ }
137
+ return virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
138
+ },
139
+ },
140
+ };
141
+ }
142
+ exports.createLanguage = createLanguage;
143
+ function updateVirtualCodeMapOfMap(virtualCode, mapOfMap, getSourceSnapshot) {
144
+ const sources = new Set();
145
+ for (const mapping of virtualCode.mappings) {
146
+ if (sources.has(mapping.source)) {
147
+ continue;
148
+ }
149
+ sources.add(mapping.source);
150
+ const source = getSourceSnapshot(mapping.source);
151
+ if (!source) {
152
+ continue;
153
+ }
154
+ if (!mapOfMap.has(source[0]) || mapOfMap.get(source[0])[0] !== source[1]) {
155
+ mapOfMap.set(source[0], [source[1], new source_map_1.SourceMap(virtualCode.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
156
+ }
157
+ }
158
+ }
159
+ exports.updateVirtualCodeMapOfMap = updateVirtualCodeMapOfMap;
160
+ function* forEachEmbeddedCode(virtualCode) {
161
+ yield virtualCode;
162
+ if (virtualCode.embeddedCodes) {
163
+ for (const embeddedCode of virtualCode.embeddedCodes) {
164
+ yield* forEachEmbeddedCode(embeddedCode);
165
+ }
166
+ }
167
+ }
168
+ exports.forEachEmbeddedCode = forEachEmbeddedCode;
24
169
  function resolveCommonLanguageId(fileNameOrUri) {
25
170
  const ext = fileNameOrUri.split('.').pop();
26
171
  switch (ext) {
package/lib/types.d.ts CHANGED
@@ -1,7 +1,27 @@
1
- import type { Mapping, Stack } from '@volar/source-map';
1
+ import type { Mapping, SourceMap, Stack } from '@volar/source-map';
2
2
  import type * as ts from 'typescript';
3
- import type { FileRegistry } from './fileRegistry';
4
- export interface SourceFile {
3
+ import type { LinkedCodeMap } from './linkedCodeMap';
4
+ export interface Language {
5
+ plugins: LanguagePlugin[];
6
+ scripts: {
7
+ get(id: string): SourceScript | undefined;
8
+ set(id: string, languageId: string, snapshot: ts.IScriptSnapshot, plugins?: LanguagePlugin[]): SourceScript;
9
+ delete(id: string): void;
10
+ };
11
+ maps: {
12
+ get(virtualCode: VirtualCode, scriptId?: string): SourceMap<CodeInformation> | undefined;
13
+ forEach(virtualCode: VirtualCode): Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>;
14
+ };
15
+ linkedCodeMaps: {
16
+ get(virtualCode: VirtualCode): LinkedCodeMap | undefined;
17
+ };
18
+ typescript?: {
19
+ projectHost: TypeScriptProjectHost;
20
+ languageServiceHost: ts.LanguageServiceHost;
21
+ getExtraServiceScript(fileName: string): ExtraServiceScript | undefined;
22
+ };
23
+ }
24
+ export interface SourceScript {
5
25
  /**
6
26
  * uri or fileName
7
27
  */
@@ -9,8 +29,9 @@ export interface SourceFile {
9
29
  languageId: string;
10
30
  snapshot: ts.IScriptSnapshot;
11
31
  generated?: {
12
- code: VirtualCode;
32
+ root: VirtualCode;
13
33
  languagePlugin: LanguagePlugin;
34
+ embeddedCodes: Map<string, VirtualCode>;
14
35
  };
15
36
  }
16
37
  export type CodeMapping = Mapping<CodeInformation>;
@@ -57,9 +78,9 @@ export interface ExtraServiceScript extends ServiceScript {
57
78
  fileName: string;
58
79
  }
59
80
  export interface LanguagePlugin<T extends VirtualCode = VirtualCode> {
60
- createVirtualCode(fileId: string, languageId: string, snapshot: ts.IScriptSnapshot, files?: FileRegistry): T | undefined;
61
- updateVirtualCode(fileId: string, virtualCode: T, newSnapshot: ts.IScriptSnapshot, files?: FileRegistry): T;
62
- disposeVirtualCode?(fileId: string, virtualCode: T, files?: FileRegistry): void;
81
+ createVirtualCode(scriptId: string, languageId: string, snapshot: ts.IScriptSnapshot): T | undefined;
82
+ updateVirtualCode(scriptId: string, virtualCode: T, newSnapshot: ts.IScriptSnapshot): T;
83
+ disposeVirtualCode?(scriptId: string, virtualCode: T): void;
63
84
  typescript?: {
64
85
  /**
65
86
  * LSP + TS Plugin
@@ -68,29 +89,22 @@ export interface LanguagePlugin<T extends VirtualCode = VirtualCode> {
68
89
  /**
69
90
  * LSP + TS Plugin
70
91
  */
71
- getScript(rootVirtualCode: T): ServiceScript | undefined;
92
+ getServiceScript(rootVirtualCode: T): ServiceScript | undefined;
72
93
  /**
73
94
  * LSP only
74
95
  */
75
- getExtraScripts?(fileName: string, rootVirtualCode: T): ExtraServiceScript[];
96
+ getExtraServiceScripts?(fileName: string, rootVirtualCode: T): ExtraServiceScript[];
76
97
  /**
77
98
  * LSP only
78
99
  */
79
100
  resolveLanguageServiceHost?(host: ts.LanguageServiceHost): ts.LanguageServiceHost;
80
101
  };
81
102
  }
82
- export interface LanguageContext {
83
- files: FileRegistry;
84
- typescript?: {
85
- configFileName: string | undefined;
86
- sys: ts.System & {
87
- sync?(): Promise<number>;
88
- };
89
- projectHost: TypeScriptProjectHost;
90
- languageServiceHost: ts.LanguageServiceHost;
91
- getExtraScript(fileName: string): ExtraServiceScript | undefined;
92
- };
93
- }
94
- export interface TypeScriptProjectHost extends Pick<ts.LanguageServiceHost, 'getLocalizedDiagnosticMessages' | 'getCompilationSettings' | 'getProjectReferences' | 'getCurrentDirectory' | 'getScriptFileNames' | 'getProjectVersion' | 'getScriptSnapshot'> {
95
- getLanguageId(fileId: string): string;
103
+ export interface TypeScriptProjectHost extends ts.System, Pick<ts.LanguageServiceHost, 'getLocalizedDiagnosticMessages' | 'getCompilationSettings' | 'getProjectReferences' | 'getCurrentDirectory' | 'getScriptFileNames' | 'getProjectVersion' | 'getScriptSnapshot'> {
104
+ configFileName: string | undefined;
105
+ getLanguageId(scriptId: string): string;
106
+ getSystemVersion?(): number;
107
+ syncSystem?(): Promise<number>;
108
+ scriptIdToFileName(scriptId: string): string;
109
+ fileNameToScriptId(fileName: string): string;
96
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-core",
3
- "version": "2.1.6",
3
+ "version": "2.2.0-alpha.1",
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.1.6"
15
+ "@volar/source-map": "2.2.0-alpha.1"
16
16
  },
17
- "gitHead": "7feafa54fd3540569d492c2ab582c10c0cdc84d2"
17
+ "gitHead": "e6dcb83ecadcfcc9e22083eea61e1ee817a9d483"
18
18
  }