@volar/language-core 2.4.27 → 2.4.29

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.
Files changed (3) hide show
  1. package/index.js +16 -3
  2. package/lib/types.d.ts +9 -0
  3. package/package.json +16 -16
package/index.js CHANGED
@@ -48,8 +48,9 @@ function createLanguage(plugins, scriptRegistry, sync, onAssociationDirty) {
48
48
  return scriptRegistry.get(id);
49
49
  },
50
50
  set(id, snapshot, languageId, _plugins = plugins) {
51
+ const scriptPlugins = filterPluginsByExtension(id, plugins);
51
52
  if (!languageId) {
52
- for (const plugin of plugins) {
53
+ for (const plugin of scriptPlugins) {
53
54
  languageId = plugin.getLanguageId?.(id);
54
55
  if (languageId) {
55
56
  break;
@@ -61,7 +62,7 @@ function createLanguage(plugins, scriptRegistry, sync, onAssociationDirty) {
61
62
  return;
62
63
  }
63
64
  let associatedOnly = false;
64
- for (const plugin of plugins) {
65
+ for (const plugin of scriptPlugins) {
65
66
  if (plugin.isAssociatedFileOnly?.(id, languageId)) {
66
67
  associatedOnly = true;
67
68
  break;
@@ -125,7 +126,7 @@ function createLanguage(plugins, scriptRegistry, sync, onAssociationDirty) {
125
126
  if (associatedOnly) {
126
127
  return sourceScript;
127
128
  }
128
- for (const languagePlugin of _plugins) {
129
+ for (const languagePlugin of filterPluginsByExtension(id, _plugins)) {
129
130
  const virtualCode = languagePlugin.createVirtualCode?.(id, languageId, snapshot, prepareCreateVirtualCode(sourceScript));
130
131
  if (virtualCode) {
131
132
  sourceScript.generated = {
@@ -228,6 +229,18 @@ function createLanguage(plugins, scriptRegistry, sync, onAssociationDirty) {
228
229
  };
229
230
  }
230
231
  }
232
+ function filterPluginsByExtension(id, plugins) {
233
+ const fileName = String(id);
234
+ let longestExtension;
235
+ for (const plugin of plugins) {
236
+ for (const extension of plugin.extensions ?? []) {
237
+ if (fileName.endsWith(extension) && extension.length > (longestExtension?.length ?? -1)) {
238
+ longestExtension = extension;
239
+ }
240
+ }
241
+ }
242
+ return plugins.filter(plugin => !plugin.extensions || (longestExtension !== undefined && plugin.extensions.includes(longestExtension)));
243
+ }
231
244
  function* forEachEmbeddedCode(virtualCode) {
232
245
  yield virtualCode;
233
246
  if (virtualCode.embeddedCodes) {
package/lib/types.d.ts CHANGED
@@ -103,6 +103,15 @@ export interface CodeInformation {
103
103
  format?: boolean;
104
104
  }
105
105
  export interface LanguagePlugin<T = unknown, K extends VirtualCode = VirtualCode> {
106
+ /**
107
+ * The file extensions this plugin handles, including the leading dot (e.g. `['.vue', '.vue.md']`), filled in by the host.
108
+ *
109
+ * When set, a script is routed to this plugin only if its longest extension matched by any plugin of the language
110
+ * is one of these; plugins that list the same extension keep their order. Script IDs are matched by `String(scriptId)`.
111
+ *
112
+ * When omitted, the plugin is offered every script and decides for itself in `getLanguageId` / `createVirtualCode`.
113
+ */
114
+ extensions?: string[];
106
115
  /**
107
116
  * 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.
108
117
  */
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
- "name": "@volar/language-core",
3
- "version": "2.4.27",
4
- "license": "MIT",
5
- "files": [
6
- "**/*.js",
7
- "**/*.d.ts"
8
- ],
9
- "repository": {
10
- "type": "git",
11
- "url": "https://github.com/volarjs/volar.js.git",
12
- "directory": "packages/language-core"
13
- },
14
- "dependencies": {
15
- "@volar/source-map": "2.4.27"
16
- }
17
- }
2
+ "name": "@volar/language-core",
3
+ "version": "2.4.29",
4
+ "license": "MIT",
5
+ "files": [
6
+ "**/*.js",
7
+ "**/*.d.ts"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/volarjs/volar.js.git",
12
+ "directory": "packages/language-core"
13
+ },
14
+ "dependencies": {
15
+ "@volar/source-map": "2.4.29"
16
+ }
17
+ }