@volar/language-core 2.3.0 → 2.3.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/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- export * from '@volar/source-map';
1
+ export { Mapping, SourceMap } from '@volar/source-map';
2
2
  export * from './lib/editorFeatures';
3
3
  export * from './lib/linkedCodeMap';
4
4
  export * from './lib/types';
5
5
  export * from './lib/utils';
6
- import type { Language, LanguagePlugin, SourceScript, VirtualCode } from './lib/types';
6
+ import type { Language, LanguagePlugin, MapperFactory, SourceScript, VirtualCode } from './lib/types';
7
+ export declare const defaultMapperFactory: MapperFactory;
7
8
  export declare function createLanguage<T>(plugins: LanguagePlugin<T>[], scriptRegistry: Map<T, SourceScript<T>>, sync: (id: T) => void): Language<T>;
8
9
  export declare function forEachEmbeddedCode(virtualCode: VirtualCode): Generator<VirtualCode>;
package/index.js CHANGED
@@ -14,19 +14,25 @@ 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.createLanguage = void 0;
18
- __exportStar(require("@volar/source-map"), exports);
17
+ exports.defaultMapperFactory = exports.SourceMap = void 0;
18
+ exports.createLanguage = createLanguage;
19
+ exports.forEachEmbeddedCode = forEachEmbeddedCode;
20
+ var source_map_1 = require("@volar/source-map");
21
+ Object.defineProperty(exports, "SourceMap", { enumerable: true, get: function () { return source_map_1.SourceMap; } });
19
22
  __exportStar(require("./lib/editorFeatures"), exports);
20
23
  __exportStar(require("./lib/linkedCodeMap"), exports);
21
24
  __exportStar(require("./lib/types"), exports);
22
25
  __exportStar(require("./lib/utils"), exports);
23
- const source_map_1 = require("@volar/source-map");
26
+ const source_map_2 = require("@volar/source-map");
24
27
  const linkedCodeMap_1 = require("./lib/linkedCodeMap");
28
+ const defaultMapperFactory = mappings => new source_map_2.SourceMap(mappings);
29
+ exports.defaultMapperFactory = defaultMapperFactory;
25
30
  function createLanguage(plugins, scriptRegistry, sync) {
26
31
  const virtualCodeToSourceScriptMap = new WeakMap();
27
32
  const virtualCodeToSourceMap = new WeakMap();
28
33
  const virtualCodeToLinkedCodeMap = new WeakMap();
29
- return {
34
+ const language = {
35
+ mapperFactory: exports.defaultMapperFactory,
30
36
  plugins,
31
37
  scripts: {
32
38
  fromVirtualCode(virtualCode) {
@@ -80,6 +86,7 @@ function createLanguage(plugins, scriptRegistry, sync) {
80
86
  ? updateVirtualCode(id, sourceScript.generated.root, snapshot, codegenCtx)
81
87
  : createVirtualCode?.(id, languageId, snapshot, codegenCtx);
82
88
  if (newVirtualCode) {
89
+ verifyVirtualCode(newVirtualCode);
83
90
  sourceScript.generated.root = newVirtualCode;
84
91
  sourceScript.generated.embeddedCodes.clear();
85
92
  for (const code of forEachEmbeddedCode(sourceScript.generated.root)) {
@@ -117,6 +124,7 @@ function createLanguage(plugins, scriptRegistry, sync) {
117
124
  for (const languagePlugin of _plugins) {
118
125
  const virtualCode = languagePlugin.createVirtualCode?.(id, languageId, snapshot, prepareCreateVirtualCode(sourceScript));
119
126
  if (virtualCode) {
127
+ verifyVirtualCode(virtualCode);
120
128
  sourceScript.generated = {
121
129
  root: virtualCode,
122
130
  languagePlugin,
@@ -149,15 +157,14 @@ function createLanguage(plugins, scriptRegistry, sync) {
149
157
  }
150
158
  if (!mapCache.has(sourceScript.snapshot)) {
151
159
  const mappings = virtualCode.associatedScriptMappings?.get(sourceScript.id) ?? virtualCode.mappings;
152
- mapCache.set(sourceScript.snapshot, new source_map_1.SourceMap(mappings));
160
+ mapCache.set(sourceScript.snapshot, language.mapperFactory(mappings));
153
161
  }
154
162
  return mapCache.get(sourceScript.snapshot);
155
163
  },
156
164
  *forEach(virtualCode) {
157
165
  const sourceScript = virtualCodeToSourceScriptMap.get(virtualCode);
158
166
  yield [
159
- sourceScript.id,
160
- sourceScript.snapshot,
167
+ sourceScript,
161
168
  this.get(virtualCode, sourceScript),
162
169
  ];
163
170
  if (virtualCode.associatedScriptMappings) {
@@ -165,8 +172,7 @@ function createLanguage(plugins, scriptRegistry, sync) {
165
172
  const relatedSourceScript = scriptRegistry.get(relatedScriptId);
166
173
  if (relatedSourceScript) {
167
174
  yield [
168
- relatedSourceScript.id,
169
- relatedSourceScript.snapshot,
175
+ relatedSourceScript,
170
176
  this.get(virtualCode, relatedSourceScript),
171
177
  ];
172
178
  }
@@ -190,6 +196,7 @@ function createLanguage(plugins, scriptRegistry, sync) {
190
196
  },
191
197
  },
192
198
  };
199
+ return language;
193
200
  function triggerTargetsDirty(sourceScript) {
194
201
  sourceScript.targetIds.forEach(id => {
195
202
  const sourceScript = scriptRegistry.get(id);
@@ -217,7 +224,13 @@ function createLanguage(plugins, scriptRegistry, sync) {
217
224
  };
218
225
  }
219
226
  }
220
- exports.createLanguage = createLanguage;
227
+ function verifyVirtualCode(virtualCode) {
228
+ for (const code of forEachEmbeddedCode(virtualCode)) {
229
+ if (code.id !== code.id.toLowerCase()) {
230
+ throw new Error(`VirtualCode id must be lowercase: ${code.id}`);
231
+ }
232
+ }
233
+ }
221
234
  function* forEachEmbeddedCode(virtualCode) {
222
235
  yield virtualCode;
223
236
  if (virtualCode.embeddedCodes) {
@@ -226,5 +239,4 @@ function* forEachEmbeddedCode(virtualCode) {
226
239
  }
227
240
  }
228
241
  }
229
- exports.forEachEmbeddedCode = forEachEmbeddedCode;
230
242
  //# sourceMappingURL=index.js.map
@@ -1,120 +1,119 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveRenameEditText = exports.resolveRenameNewName = exports.shouldReportDiagnostics = exports.isSignatureHelpEnabled = exports.isAutoInsertEnabled = exports.isCompletionEnabled = exports.isFormattingEnabled = exports.isCodeActionsEnabled = exports.isDiagnosticsEnabled = exports.isDocumentLinkEnabled = exports.isColorEnabled = exports.isLinkedEditingEnabled = exports.isSelectionRangesEnabled = exports.isFoldingRangesEnabled = exports.isSymbolsEnabled = exports.isHighlightEnabled = exports.isImplementationEnabled = exports.isReferencesEnabled = exports.isTypeDefinitionEnabled = exports.isDefinitionEnabled = exports.isRenameEnabled = exports.isCallHierarchyEnabled = exports.isSemanticTokensEnabled = exports.isCodeLensEnabled = exports.isInlayHintsEnabled = exports.isHoverEnabled = void 0;
3
+ exports.isHoverEnabled = isHoverEnabled;
4
+ exports.isInlayHintsEnabled = isInlayHintsEnabled;
5
+ exports.isCodeLensEnabled = isCodeLensEnabled;
6
+ exports.isSemanticTokensEnabled = isSemanticTokensEnabled;
7
+ exports.isCallHierarchyEnabled = isCallHierarchyEnabled;
8
+ exports.isRenameEnabled = isRenameEnabled;
9
+ exports.isDefinitionEnabled = isDefinitionEnabled;
10
+ exports.isTypeDefinitionEnabled = isTypeDefinitionEnabled;
11
+ exports.isReferencesEnabled = isReferencesEnabled;
12
+ exports.isImplementationEnabled = isImplementationEnabled;
13
+ exports.isHighlightEnabled = isHighlightEnabled;
14
+ exports.isSymbolsEnabled = isSymbolsEnabled;
15
+ exports.isFoldingRangesEnabled = isFoldingRangesEnabled;
16
+ exports.isSelectionRangesEnabled = isSelectionRangesEnabled;
17
+ exports.isLinkedEditingEnabled = isLinkedEditingEnabled;
18
+ exports.isColorEnabled = isColorEnabled;
19
+ exports.isDocumentLinkEnabled = isDocumentLinkEnabled;
20
+ exports.isDiagnosticsEnabled = isDiagnosticsEnabled;
21
+ exports.isCodeActionsEnabled = isCodeActionsEnabled;
22
+ exports.isFormattingEnabled = isFormattingEnabled;
23
+ exports.isCompletionEnabled = isCompletionEnabled;
24
+ exports.isAutoInsertEnabled = isAutoInsertEnabled;
25
+ exports.isSignatureHelpEnabled = isSignatureHelpEnabled;
26
+ exports.shouldReportDiagnostics = shouldReportDiagnostics;
27
+ exports.resolveRenameNewName = resolveRenameNewName;
28
+ exports.resolveRenameEditText = resolveRenameEditText;
4
29
  function isHoverEnabled(info) {
5
30
  return !!info.semantic;
6
31
  }
7
- exports.isHoverEnabled = isHoverEnabled;
8
32
  function isInlayHintsEnabled(info) {
9
33
  return !!info.semantic;
10
34
  }
11
- exports.isInlayHintsEnabled = isInlayHintsEnabled;
12
35
  function isCodeLensEnabled(info) {
13
36
  return !!info.semantic;
14
37
  }
15
- exports.isCodeLensEnabled = isCodeLensEnabled;
16
38
  function isSemanticTokensEnabled(info) {
17
39
  return typeof info.semantic === 'object'
18
40
  ? info.semantic.shouldHighlight?.() ?? true
19
41
  : !!info.semantic;
20
42
  }
21
- exports.isSemanticTokensEnabled = isSemanticTokensEnabled;
22
43
  function isCallHierarchyEnabled(info) {
23
44
  return !!info.navigation;
24
45
  }
25
- exports.isCallHierarchyEnabled = isCallHierarchyEnabled;
26
46
  function isRenameEnabled(info) {
27
47
  return typeof info.navigation === 'object'
28
48
  ? info.navigation.shouldRename?.() ?? true
29
49
  : !!info.navigation;
30
50
  }
31
- exports.isRenameEnabled = isRenameEnabled;
32
51
  function isDefinitionEnabled(info) {
33
52
  return !!info.navigation;
34
53
  }
35
- exports.isDefinitionEnabled = isDefinitionEnabled;
36
54
  function isTypeDefinitionEnabled(info) {
37
55
  return !!info.navigation;
38
56
  }
39
- exports.isTypeDefinitionEnabled = isTypeDefinitionEnabled;
40
57
  function isReferencesEnabled(info) {
41
58
  return !!info.navigation;
42
59
  }
43
- exports.isReferencesEnabled = isReferencesEnabled;
44
60
  function isImplementationEnabled(info) {
45
61
  return !!info.navigation;
46
62
  }
47
- exports.isImplementationEnabled = isImplementationEnabled;
48
63
  function isHighlightEnabled(info) {
49
64
  return !!info.navigation;
50
65
  }
51
- exports.isHighlightEnabled = isHighlightEnabled;
52
66
  function isSymbolsEnabled(info) {
53
67
  return !!info.structure;
54
68
  }
55
- exports.isSymbolsEnabled = isSymbolsEnabled;
56
69
  function isFoldingRangesEnabled(info) {
57
70
  return !!info.structure;
58
71
  }
59
- exports.isFoldingRangesEnabled = isFoldingRangesEnabled;
60
72
  function isSelectionRangesEnabled(info) {
61
73
  return !!info.structure;
62
74
  }
63
- exports.isSelectionRangesEnabled = isSelectionRangesEnabled;
64
75
  function isLinkedEditingEnabled(info) {
65
76
  return !!info.structure;
66
77
  }
67
- exports.isLinkedEditingEnabled = isLinkedEditingEnabled;
68
78
  function isColorEnabled(info) {
69
79
  return !!info.structure;
70
80
  }
71
- exports.isColorEnabled = isColorEnabled;
72
81
  function isDocumentLinkEnabled(info) {
73
82
  return !!info.structure;
74
83
  }
75
- exports.isDocumentLinkEnabled = isDocumentLinkEnabled;
76
84
  function isDiagnosticsEnabled(info) {
77
85
  return !!info.verification;
78
86
  }
79
- exports.isDiagnosticsEnabled = isDiagnosticsEnabled;
80
87
  function isCodeActionsEnabled(info) {
81
88
  return !!info.verification;
82
89
  }
83
- exports.isCodeActionsEnabled = isCodeActionsEnabled;
84
90
  function isFormattingEnabled(info) {
85
91
  return !!info.format;
86
92
  }
87
- exports.isFormattingEnabled = isFormattingEnabled;
88
93
  function isCompletionEnabled(info) {
89
94
  return !!info.completion;
90
95
  }
91
- exports.isCompletionEnabled = isCompletionEnabled;
92
96
  function isAutoInsertEnabled(info) {
93
97
  return !!info.completion;
94
98
  }
95
- exports.isAutoInsertEnabled = isAutoInsertEnabled;
96
99
  function isSignatureHelpEnabled(info) {
97
100
  return !!info.completion;
98
101
  }
99
- exports.isSignatureHelpEnabled = isSignatureHelpEnabled;
100
102
  // should...
101
103
  function shouldReportDiagnostics(info) {
102
104
  return typeof info.verification === 'object'
103
105
  ? info.verification.shouldReport?.() ?? true
104
106
  : !!info.verification;
105
107
  }
106
- exports.shouldReportDiagnostics = shouldReportDiagnostics;
107
108
  // resolve...
108
109
  function resolveRenameNewName(newName, info) {
109
110
  return typeof info.navigation === 'object'
110
111
  ? info.navigation.resolveRenameNewName?.(newName) ?? newName
111
112
  : newName;
112
113
  }
113
- exports.resolveRenameNewName = resolveRenameNewName;
114
114
  function resolveRenameEditText(text, info) {
115
115
  return typeof info.navigation === 'object'
116
116
  ? info.navigation.resolveRenameEditText?.(text) ?? text
117
117
  : text;
118
118
  }
119
- exports.resolveRenameEditText = resolveRenameEditText;
120
119
  //# sourceMappingURL=editorFeatures.js.map
@@ -4,10 +4,10 @@ exports.LinkedCodeMap = void 0;
4
4
  const source_map_1 = require("@volar/source-map");
5
5
  class LinkedCodeMap extends source_map_1.SourceMap {
6
6
  *getLinkedOffsets(start) {
7
- for (const mapped of this.getGeneratedOffsets(start)) {
7
+ for (const mapped of this.toGeneratedLocation(start)) {
8
8
  yield mapped[0];
9
9
  }
10
- for (const mapped of this.getSourceOffsets(start)) {
10
+ for (const mapped of this.toSourceLocation(start)) {
11
11
  yield mapped[0];
12
12
  }
13
13
  }
package/lib/types.d.ts CHANGED
@@ -1,7 +1,16 @@
1
- import type { Mapping, SourceMap } from '@volar/source-map';
1
+ import type { Mapping } from '@volar/source-map';
2
2
  import type * as ts from 'typescript';
3
3
  import type { LinkedCodeMap } from './linkedCodeMap';
4
+ export interface Mapper {
5
+ mappings: Mapping<CodeInformation>[];
6
+ toSourceRange(start: number, end: number, fallbackToAnyMatch: boolean, filter?: (data: CodeInformation) => boolean): Generator<readonly [number, number, Mapping<CodeInformation>, Mapping<CodeInformation>]>;
7
+ toGeneratedRange(start: number, end: number, fallbackToAnyMatch: boolean, filter?: (data: CodeInformation) => boolean): Generator<readonly [number, number, Mapping<CodeInformation>, Mapping<CodeInformation>]>;
8
+ toSourceLocation(generatedOffset: number, filter?: (data: CodeInformation) => boolean): Generator<readonly [number, Mapping<CodeInformation>]>;
9
+ toGeneratedLocation(sourceOffset: number, filter?: (data: CodeInformation) => boolean): Generator<readonly [number, Mapping<CodeInformation>]>;
10
+ }
11
+ export type MapperFactory = (mappings: Mapping<CodeInformation>[]) => Mapper;
4
12
  export interface Language<T = unknown> {
13
+ mapperFactory: MapperFactory;
5
14
  plugins: LanguagePlugin<T>[];
6
15
  scripts: {
7
16
  get(id: T): SourceScript<T> | undefined;
@@ -10,8 +19,8 @@ export interface Language<T = unknown> {
10
19
  fromVirtualCode(virtualCode: VirtualCode): SourceScript<T>;
11
20
  };
12
21
  maps: {
13
- get(virtualCode: VirtualCode, sourceScript: SourceScript<T>): SourceMap<CodeInformation>;
14
- forEach(virtualCode: VirtualCode): Generator<[id: T, snapshot: ts.IScriptSnapshot, map: SourceMap<CodeInformation>]>;
22
+ get(virtualCode: VirtualCode, sourceScript: SourceScript<T>): Mapper;
23
+ forEach(virtualCode: VirtualCode): Generator<[sourceScript: SourceScript<T>, map: Mapper]>;
15
24
  };
16
25
  linkedCodeMaps: {
17
26
  get(virtualCode: VirtualCode): LinkedCodeMap | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-core",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
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"
15
+ "@volar/source-map": "2.3.2"
16
16
  },
17
- "gitHead": "4f6488605e22e0f76ea877460848a443fd3e8762"
17
+ "gitHead": "d07e7d7c4176eec4905e7a629ae6f278d1c72b48"
18
18
  }