@volar/language-service 1.0.19 → 1.0.21

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 (37) hide show
  1. package/out/baseLanguageService.d.ts +17 -6
  2. package/out/baseLanguageService.js +38 -18
  3. package/out/documentFeatures/colorPresentations.d.ts +2 -3
  4. package/out/documentFeatures/colorPresentations.js +7 -4
  5. package/out/documentFeatures/documentColors.d.ts +2 -3
  6. package/out/documentFeatures/documentColors.js +4 -4
  7. package/out/documentFeatures/documentSymbols.d.ts +2 -3
  8. package/out/documentFeatures/documentSymbols.js +5 -5
  9. package/out/documentFeatures/foldingRanges.d.ts +2 -3
  10. package/out/documentFeatures/foldingRanges.js +2 -2
  11. package/out/documentFeatures/format.d.ts +3 -2
  12. package/out/documentFeatures/format.js +76 -39
  13. package/out/documentFeatures/linkedEditingRanges.d.ts +2 -3
  14. package/out/documentFeatures/linkedEditingRanges.js +3 -3
  15. package/out/documentFeatures/selectionRanges.d.ts +2 -3
  16. package/out/documentFeatures/selectionRanges.js +10 -8
  17. package/out/documents.d.ts +1 -0
  18. package/out/documents.js +10 -2
  19. package/out/index.d.ts +0 -1
  20. package/out/index.js +0 -1
  21. package/out/languageFeatures/autoInsert.d.ts +2 -3
  22. package/out/languageFeatures/complete.js +2 -2
  23. package/out/languageFeatures/completeResolve.js +1 -1
  24. package/out/languageFeatures/inlayHints.js +1 -1
  25. package/out/types.d.ts +9 -20
  26. package/out/types.js +0 -1
  27. package/out/utils/common.d.ts +1 -0
  28. package/out/utils/common.js +9 -1
  29. package/out/utils/featureWorkers.d.ts +4 -5
  30. package/out/utils/featureWorkers.js +9 -50
  31. package/package.json +7 -7
  32. package/out/baseDocumentService.d.ts +0 -33
  33. package/out/baseDocumentService.js +0 -85
  34. package/out/documentFeatures/autoInsert.d.ts +0 -5
  35. package/out/documentFeatures/autoInsert.js +0 -32
  36. package/out/utils/singleFileTypeScriptService.d.ts +0 -4
  37. package/out/utils/singleFileTypeScriptService.js +0 -30
@@ -1,17 +1,28 @@
1
1
  import { createLanguageContext, LanguageServiceHost } from '@volar/language-core';
2
2
  import * as executeCommand from './languageFeatures/executeCommand';
3
- import { LanguageServicePlugin, LanguageServicePluginContext, LanguageServiceRuntimeContext } from './types';
3
+ import { LanguageServicePlugin, LanguageServicePluginInstance, LanguageServiceRuntimeContext } from './types';
4
4
  import type * as ts from 'typescript/lib/tsserverlibrary';
5
5
  import type * as _ from 'vscode-languageserver-protocol';
6
6
  export type LanguageService = ReturnType<typeof createLanguageService>;
7
7
  export declare function createLanguageServiceContext(options: {
8
8
  host: LanguageServiceHost;
9
9
  context: ReturnType<typeof createLanguageContext>;
10
- getPlugins(): LanguageServicePlugin[];
11
- env: LanguageServicePluginContext['env'];
10
+ getPlugins(): (LanguageServicePlugin | LanguageServicePluginInstance)[];
11
+ env: LanguageServiceRuntimeContext['env'];
12
12
  documentRegistry: ts.DocumentRegistry | undefined;
13
- }): LanguageServiceRuntimeContext;
13
+ getLanguageService: () => LanguageService;
14
+ }): LanguageServiceRuntimeContext<LanguageServiceHost>;
14
15
  export declare function createLanguageService(context: LanguageServiceRuntimeContext): {
16
+ format: (uri: string, options: _.FormattingOptions, range?: _.Range | undefined, onTypeParams?: {
17
+ ch: string;
18
+ position: _.Position;
19
+ } | undefined) => Promise<_.TextEdit[] | undefined>;
20
+ getFoldingRanges: (uri: string) => Promise<_.FoldingRange[] | undefined>;
21
+ getSelectionRanges: (uri: string, positions: _.Position[]) => Promise<_.SelectionRange[] | undefined>;
22
+ findLinkedEditingRanges: (uri: string, position: _.Position) => Promise<_.LinkedEditingRanges | undefined>;
23
+ findDocumentSymbols: (uri: string) => Promise<_.SymbolInformation[] | undefined>;
24
+ findDocumentColors: (uri: string) => Promise<_.ColorInformation[] | undefined>;
25
+ getColorPresentations: (uri: string, color: _.Color, range: _.Range) => Promise<_.ColorPresentation[] | undefined>;
15
26
  doValidation: (uri: string, token?: _.CancellationToken | undefined, response?: ((result: _.Diagnostic[]) => void) | undefined) => Promise<_.Diagnostic[]>;
16
27
  findReferences: (uri: string, position: _.Position) => Promise<_.Location[] | undefined>;
17
28
  findFileReferences: (uri: string) => import("./types").NullableResult<_.Location[]>;
@@ -48,6 +59,6 @@ export declare function createLanguageService(context: LanguageServiceRuntimeCon
48
59
  getIncomingCalls(item: _.CallHierarchyItem): Promise<_.CallHierarchyIncomingCall[]>;
49
60
  getOutgoingCalls(item: _.CallHierarchyItem): Promise<_.CallHierarchyOutgoingCall[]>;
50
61
  };
51
- dispose: () => void;
52
- context: LanguageServiceRuntimeContext;
62
+ dispose: () => void | undefined;
63
+ context: LanguageServiceRuntimeContext<LanguageServiceHost>;
53
64
  };
@@ -27,37 +27,50 @@ const renamePrepare = require("./languageFeatures/renamePrepare");
27
27
  const signatureHelp = require("./languageFeatures/signatureHelp");
28
28
  const diagnostics = require("./languageFeatures/validation");
29
29
  const workspaceSymbol = require("./languageFeatures/workspaceSymbols");
30
+ const colorPresentations = require("./documentFeatures/colorPresentations");
31
+ const documentColors = require("./documentFeatures/documentColors");
32
+ const documentSymbols = require("./documentFeatures/documentSymbols");
33
+ const foldingRanges = require("./documentFeatures/foldingRanges");
34
+ const format = require("./documentFeatures/format");
35
+ const linkedEditingRanges = require("./documentFeatures/linkedEditingRanges");
36
+ const selectionRanges = require("./documentFeatures/selectionRanges");
30
37
  function createLanguageServiceContext(options) {
31
38
  const ts = options.host.getTypeScriptModule();
32
- const tsLs = ts.createLanguageService(options.context.typescript.languageServiceHost, options.documentRegistry);
33
- tsFaster.decorate(ts, options.context.typescript.languageServiceHost, tsLs);
39
+ const tsLs = ts === null || ts === void 0 ? void 0 : ts.createLanguageService(options.context.typescript.languageServiceHost, options.documentRegistry);
40
+ if (ts && tsLs) {
41
+ tsFaster.decorate(ts, options.context.typescript.languageServiceHost, tsLs);
42
+ }
34
43
  let plugins;
35
- const pluginContext = {
36
- env: options.env,
37
- typescript: {
38
- module: options.host.getTypeScriptModule(),
39
- languageServiceHost: options.context.typescript.languageServiceHost,
40
- languageService: tsLs,
41
- },
42
- };
43
44
  const textDocumentMapper = (0, documents_1.createDocumentsAndSourceMaps)(options.context.virtualFiles);
44
45
  const documents = new WeakMap();
45
46
  const documentVersions = new Map();
46
47
  const context = {
47
48
  host: options.host,
48
49
  core: options.context,
50
+ env: options.env,
49
51
  get plugins() {
50
- var _a;
51
52
  if (!plugins) {
52
- plugins = options.getPlugins();
53
- for (const plugin of plugins) {
54
- (_a = plugin.setup) === null || _a === void 0 ? void 0 : _a.call(plugin, pluginContext);
55
- }
53
+ plugins = []; // avoid infinite loop
54
+ plugins = options.getPlugins().map(plugin => {
55
+ var _a, _b;
56
+ if (plugin instanceof Function) {
57
+ const _plugin = plugin(this, options.getLanguageService());
58
+ (_a = _plugin.setup) === null || _a === void 0 ? void 0 : _a.call(_plugin, this);
59
+ return _plugin;
60
+ }
61
+ else {
62
+ (_b = plugin.setup) === null || _b === void 0 ? void 0 : _b.call(plugin, this);
63
+ return plugin;
64
+ }
65
+ });
56
66
  }
57
67
  return plugins;
58
68
  },
59
- pluginContext,
60
- typescriptLanguageService: tsLs,
69
+ typescript: ts && tsLs ? {
70
+ module: ts,
71
+ languageServiceHost: options.context.typescript.languageServiceHost,
72
+ languageService: tsLs,
73
+ } : undefined,
61
74
  documents: textDocumentMapper,
62
75
  getTextDocument,
63
76
  };
@@ -81,6 +94,13 @@ function createLanguageServiceContext(options) {
81
94
  exports.createLanguageServiceContext = createLanguageServiceContext;
82
95
  function createLanguageService(context) {
83
96
  return {
97
+ format: format.register(context),
98
+ getFoldingRanges: foldingRanges.register(context),
99
+ getSelectionRanges: selectionRanges.register(context),
100
+ findLinkedEditingRanges: linkedEditingRanges.register(context),
101
+ findDocumentSymbols: documentSymbols.register(context),
102
+ findDocumentColors: documentColors.register(context),
103
+ getColorPresentations: colorPresentations.register(context),
84
104
  doValidation: diagnostics.register(context),
85
105
  findReferences: references.register(context),
86
106
  findFileReferences: fileReferences.register(context),
@@ -106,7 +126,7 @@ function createLanguageService(context) {
106
126
  doExecuteCommand: executeCommand.register(context),
107
127
  getInlayHints: inlayHints.register(context),
108
128
  callHierarchy: callHierarchy.register(context),
109
- dispose: () => context.typescriptLanguageService.dispose(),
129
+ dispose: () => { var _a; return (_a = context.typescript) === null || _a === void 0 ? void 0 : _a.languageService.dispose(); },
110
130
  context,
111
131
  };
112
132
  }
@@ -1,4 +1,3 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
1
+ import type { LanguageServiceRuntimeContext } from '../types';
3
2
  import * as vscode from 'vscode-languageserver-protocol';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument, color: vscode.Color, range: vscode.Range) => Promise<vscode.ColorPresentation[] | undefined>;
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string, color: vscode.Color, range: vscode.Range) => Promise<vscode.ColorPresentation[] | undefined>;
@@ -3,9 +3,12 @@ exports.register = void 0;
3
3
  const featureWorkers_1 = require("../utils/featureWorkers");
4
4
  const shared = require("@volar/shared");
5
5
  function register(context) {
6
- return (document, color, range) => {
7
- return (0, featureWorkers_1.documentArgFeatureWorker)(context, document, range, (file) => !!file.capabilities.documentSymbol, // TODO: add color capabilitie setting
8
- (range, map) => map.toGeneratedRanges(range), (plugin, document, range) => { var _a; return (_a = plugin.getColorPresentations) === null || _a === void 0 ? void 0 : _a.call(plugin, document, color, range); }, (data, map) => data.map(cp => {
6
+ return (uri, color, range) => {
7
+ return (0, featureWorkers_1.languageFeatureWorker)(context, uri, range, (range, map, file) => {
8
+ if (file.capabilities.documentSymbol) // TODO: add color capability setting
9
+ return map.toGeneratedRanges(range);
10
+ return [];
11
+ }, (plugin, document, range) => { var _a; return (_a = plugin.getColorPresentations) === null || _a === void 0 ? void 0 : _a.call(plugin, document, color, range); }, (data, map) => map ? data.map(cp => {
9
12
  if (cp.textEdit) {
10
13
  const range = map.toSourceRange(cp.textEdit.range);
11
14
  if (!range)
@@ -21,7 +24,7 @@ function register(context) {
21
24
  }
22
25
  }
23
26
  return cp;
24
- }).filter(shared.notEmpty));
27
+ }).filter(shared.notEmpty) : data);
25
28
  };
26
29
  }
27
30
  exports.register = register;
@@ -1,4 +1,3 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
1
+ import type { LanguageServiceRuntimeContext } from '../types';
3
2
  import * as vscode from 'vscode-languageserver-protocol';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument) => Promise<vscode.ColorInformation[] | undefined>;
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string) => Promise<vscode.ColorInformation[] | undefined>;
@@ -4,14 +4,14 @@ const featureWorkers_1 = require("../utils/featureWorkers");
4
4
  const vscode = require("vscode-languageserver-protocol");
5
5
  const shared = require("@volar/shared");
6
6
  function register(context) {
7
- return (document) => {
8
- return (0, featureWorkers_1.documentFeatureWorker)(context, document, (file) => !!file.capabilities.documentSymbol, // TODO: add color capabilitie setting
9
- (plugin, document) => { var _a; return (_a = plugin.findDocumentColors) === null || _a === void 0 ? void 0 : _a.call(plugin, document); }, (data, map) => data.map(color => {
7
+ return (uri) => {
8
+ return (0, featureWorkers_1.documentFeatureWorker)(context, uri, file => !!file.capabilities.documentSymbol, // TODO: add color capability setting
9
+ (plugin, document) => { var _a; return (_a = plugin.findDocumentColors) === null || _a === void 0 ? void 0 : _a.call(plugin, document); }, (data, map) => map ? data.map(color => {
10
10
  const range = map.toSourceRange(color.range);
11
11
  if (range) {
12
12
  return vscode.ColorInformation.create(range, color.color);
13
13
  }
14
- }).filter(shared.notEmpty), arr => arr.flat());
14
+ }).filter(shared.notEmpty) : data, arr => arr.flat());
15
15
  };
16
16
  }
17
17
  exports.register = register;
@@ -1,4 +1,3 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
1
+ import type { LanguageServiceRuntimeContext } from '../types';
3
2
  import * as vscode from 'vscode-languageserver-protocol';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument) => Promise<vscode.SymbolInformation[] | undefined>;
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string) => Promise<vscode.SymbolInformation[] | undefined>;
@@ -4,15 +4,15 @@ const featureWorkers_1 = require("../utils/featureWorkers");
4
4
  const transforms_1 = require("@volar/transforms");
5
5
  const vscode = require("vscode-languageserver-protocol");
6
6
  function register(context) {
7
- return (document) => {
8
- return (0, featureWorkers_1.documentFeatureWorker)(context, document, file => !!file.capabilities.documentSymbol, // TODO: add color capabilitie setting
9
- (plugin, document) => { var _a; return (_a = plugin.findDocumentSymbols) === null || _a === void 0 ? void 0 : _a.call(plugin, document); }, (data, map) => (0, transforms_1.transformSymbolInformations)(data, location => {
7
+ return (uri) => {
8
+ return (0, featureWorkers_1.documentFeatureWorker)(context, uri, file => !!file.capabilities.documentSymbol, // TODO: add color capabilitie setting
9
+ (plugin, document) => { var _a; return (_a = plugin.findDocumentSymbols) === null || _a === void 0 ? void 0 : _a.call(plugin, document); }, (data, map) => map ? (0, transforms_1.transformSymbolInformations)(data, location => {
10
10
  const range = map.toSourceRange(location.range);
11
11
  if (range) {
12
12
  // use document.uri instead of map.sourceDocument.uri to fix https://github.com/johnsoncodehk/volar/issues/1925
13
- return vscode.Location.create(document.uri, range);
13
+ return vscode.Location.create(uri, range);
14
14
  }
15
- }), arr => arr.flat());
15
+ }) : data, arr => arr.flat());
16
16
  };
17
17
  }
18
18
  exports.register = register;
@@ -1,4 +1,3 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
1
+ import type { LanguageServiceRuntimeContext } from '../types';
3
2
  import type * as _ from 'vscode-languageserver-protocol';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument) => Promise<_.FoldingRange[] | undefined>;
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string) => Promise<_.FoldingRange[] | undefined>;
@@ -3,8 +3,8 @@ exports.register = void 0;
3
3
  const featureWorkers_1 = require("../utils/featureWorkers");
4
4
  const transforms_1 = require("@volar/transforms");
5
5
  function register(context) {
6
- return (document) => {
7
- return (0, featureWorkers_1.documentFeatureWorker)(context, document, file => !!file.capabilities.foldingRange, (plugin, document) => { var _a; return (_a = plugin.getFoldingRanges) === null || _a === void 0 ? void 0 : _a.call(plugin, document); }, (data, sourceMap) => (0, transforms_1.transformFoldingRanges)(data, range => sourceMap === null || sourceMap === void 0 ? void 0 : sourceMap.toSourceRange(range)), arr => arr.flat());
6
+ return (uri) => {
7
+ return (0, featureWorkers_1.documentFeatureWorker)(context, uri, file => !!file.capabilities.foldingRange, (plugin, document) => { var _a; return (_a = plugin.getFoldingRanges) === null || _a === void 0 ? void 0 : _a.call(plugin, document); }, (data, sourceMap) => (0, transforms_1.transformFoldingRanges)(data, range => sourceMap === null || sourceMap === void 0 ? void 0 : sourceMap.toSourceRange(range)), arr => arr.flat());
8
8
  };
9
9
  }
10
10
  exports.register = register;
@@ -1,7 +1,8 @@
1
1
  import * as vscode from 'vscode-languageserver-protocol';
2
2
  import { TextDocument } from 'vscode-languageserver-textdocument';
3
- import type { DocumentServiceRuntimeContext } from '../types';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument, options: vscode.FormattingOptions, range?: vscode.Range, onTypeParams?: {
3
+ import type { LanguageServiceRuntimeContext } from '../types';
4
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string, options: vscode.FormattingOptions, range?: vscode.Range, onTypeParams?: {
5
5
  ch: string;
6
6
  position: vscode.Position;
7
7
  }) => Promise<vscode.TextEdit[] | undefined>;
8
+ export declare function isTsDocument(document: TextDocument): boolean;
@@ -8,31 +8,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.register = void 0;
11
+ exports.isTsDocument = exports.register = void 0;
12
12
  const vscode = require("vscode-languageserver-protocol");
13
13
  const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
14
+ const shared = require("@volar/shared");
15
+ const common_1 = require("../utils/common");
14
16
  function register(context) {
15
- const ts = context.typescript;
16
- return (document, options, range, onTypeParams) => __awaiter(this, void 0, void 0, function* () {
17
- var _a, _b, _c, _d;
18
- if (!range) {
19
- range = vscode.Range.create(document.positionAt(0), document.positionAt(document.getText().length));
17
+ return (uri, options, range, onTypeParams) => __awaiter(this, void 0, void 0, function* () {
18
+ var _a, _b, _c;
19
+ let document = context.getTextDocument(uri);
20
+ if (!document)
21
+ return;
22
+ range !== null && range !== void 0 ? range : (range = vscode.Range.create(document.positionAt(0), document.positionAt(document.getText().length)));
23
+ const source = context.documents.getSourceByUri(document.uri);
24
+ if (!source) {
25
+ return onTypeParams
26
+ ? yield tryFormat(document, onTypeParams.position, undefined, onTypeParams.ch)
27
+ : yield tryFormat(document, range, undefined);
20
28
  }
21
- const virtualFile = context.documents.getRootFileBySourceFileUri(document.uri);
29
+ const originalSnapshot = source[0];
30
+ const rootVirtualFile = source[1];
22
31
  const originalDocument = document;
23
- const rootEdits = onTypeParams
24
- ? yield tryFormat(document, onTypeParams.position, undefined, onTypeParams.ch)
25
- : yield tryFormat(document, range, undefined);
26
- if (!virtualFile)
27
- return rootEdits;
28
- if (rootEdits === null || rootEdits === void 0 ? void 0 : rootEdits.length) {
29
- applyEdits(rootEdits);
30
- }
32
+ const initialIndentLanguageId = (_b = yield ((_a = context.env.configurationHost) === null || _a === void 0 ? void 0 : _a.getConfiguration('volar.format.initialIndent'))) !== null && _b !== void 0 ? _b : { html: true };
31
33
  let level = 0;
32
- const initialIndentLanguageId = (_b = yield ((_a = context.pluginContext.env.configurationHost) === null || _a === void 0 ? void 0 : _a.getConfiguration('volar.format.initialIndent'))) !== null && _b !== void 0 ? _b : { html: true };
34
+ let edited = false;
33
35
  while (true) {
34
- tryUpdateVueDocument();
35
- const embeddedFiles = getEmbeddedFilesByLevel(virtualFile, level++);
36
+ const embeddedFiles = getEmbeddedFilesByLevel(rootVirtualFile, level++);
36
37
  if (embeddedFiles.length === 0)
37
38
  break;
38
39
  let edits = [];
@@ -87,32 +88,34 @@ function register(context) {
87
88
  }
88
89
  }
89
90
  if (edits.length > 0) {
90
- applyEdits(edits);
91
+ const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
92
+ document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
93
+ context.core.virtualFiles.update(shared.getPathOfUri(document.uri), (0, common_1.stringToSnapshot)(document.getText()));
94
+ edited = true;
91
95
  }
92
96
  if (toPatchIndent) {
93
- tryUpdateVueDocument();
94
- const maps = [...context.documents.getMapsByVirtualFileName(virtualFile.fileName)];
95
- const map = (_d = maps.find(map => map[1].sourceFileDocument.uri === (toPatchIndent === null || toPatchIndent === void 0 ? void 0 : toPatchIndent.sourceMapEmbeddedDocumentUri))) === null || _d === void 0 ? void 0 : _d[1];
96
- if (map) {
97
- const indentEdits = patchInterpolationIndent(context.documents.getDocumentByFileName(virtualFile.snapshot, virtualFile.fileName), map);
97
+ for (const [_, map] of context.documents.getMapsByVirtualFileUri(toPatchIndent === null || toPatchIndent === void 0 ? void 0 : toPatchIndent.sourceMapEmbeddedDocumentUri)) {
98
+ const indentEdits = patchInterpolationIndent(document, map.map);
98
99
  if (indentEdits.length > 0) {
99
- applyEdits(indentEdits);
100
+ const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits);
101
+ document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
102
+ context.core.virtualFiles.update(shared.getPathOfUri(document.uri), (0, common_1.stringToSnapshot)(document.getText()));
103
+ edited = true;
100
104
  }
101
105
  }
102
106
  }
103
107
  }
108
+ if (edited) {
109
+ // recover
110
+ context.core.virtualFiles.update(shared.getPathOfUri(document.uri), originalSnapshot);
111
+ }
104
112
  if (document.getText() === originalDocument.getText())
105
113
  return;
106
114
  const editRange = vscode.Range.create(originalDocument.positionAt(0), originalDocument.positionAt(originalDocument.getText().length));
107
115
  const textEdit = vscode.TextEdit.replace(editRange, document.getText());
108
116
  return [textEdit];
109
- function tryUpdateVueDocument() {
110
- if (virtualFile && virtualFile.snapshot.getText(0, virtualFile.snapshot.getLength()) !== document.getText()) {
111
- context.updateVirtualFile(virtualFile.fileName, ts.ScriptSnapshot.fromString(document.getText()));
112
- }
113
- }
114
117
  function getEmbeddedFilesByLevel(rootFile, level) {
115
- const embeddedFilesByLevel = [rootFile.embeddedFiles];
118
+ const embeddedFilesByLevel = [[rootFile]];
116
119
  while (true) {
117
120
  if (embeddedFilesByLevel.length > level)
118
121
  return embeddedFilesByLevel[level];
@@ -152,9 +155,39 @@ function register(context) {
152
155
  }
153
156
  }
154
157
  }
155
- context.prepareLanguageServices(formatDocument);
156
158
  for (const plugin of context.plugins) {
157
159
  let edits;
160
+ let recover;
161
+ if (formatDocument !== document && isTsDocument(formatDocument) && context.typescript) {
162
+ const formatFileName = shared.getPathOfUri(formatDocument.uri);
163
+ const formatSnapshot = (0, common_1.stringToSnapshot)(formatDocument.getText());
164
+ const host = context.typescript.languageServiceHost;
165
+ const original = {
166
+ getProjectVersion: host.getProjectVersion,
167
+ getScriptVersion: host.getScriptVersion,
168
+ getScriptSnapshot: host.getScriptSnapshot,
169
+ };
170
+ host.getProjectVersion = () => { var _a; return ((_a = original.getProjectVersion) === null || _a === void 0 ? void 0 : _a.call(original)) + '-' + formatDocument.version; };
171
+ host.getScriptVersion = (fileName) => {
172
+ var _a, _b;
173
+ if (fileName === formatFileName) {
174
+ return ((_a = original.getScriptVersion) === null || _a === void 0 ? void 0 : _a.call(original, fileName)) + '-' + formatDocument.version.toString();
175
+ }
176
+ return (_b = original.getScriptVersion) === null || _b === void 0 ? void 0 : _b.call(original, fileName);
177
+ };
178
+ host.getScriptSnapshot = (fileName) => {
179
+ var _a;
180
+ if (fileName === formatFileName) {
181
+ return formatSnapshot;
182
+ }
183
+ return (_a = original.getScriptSnapshot) === null || _a === void 0 ? void 0 : _a.call(original, fileName);
184
+ };
185
+ recover = () => {
186
+ host.getProjectVersion = original.getProjectVersion;
187
+ host.getScriptVersion = original.getScriptVersion;
188
+ host.getScriptSnapshot = original.getScriptSnapshot;
189
+ };
190
+ }
158
191
  try {
159
192
  if (ch !== undefined && vscode.Position.is(formatRange)) {
160
193
  edits = yield ((_a = plugin.formatOnType) === null || _a === void 0 ? void 0 : _a.call(plugin, formatDocument, formatRange, ch, options));
@@ -166,6 +199,7 @@ function register(context) {
166
199
  catch (err) {
167
200
  console.error(err);
168
201
  }
202
+ recover === null || recover === void 0 ? void 0 : recover();
169
203
  if (!edits)
170
204
  continue;
171
205
  if (!edits.length)
@@ -190,18 +224,12 @@ function register(context) {
190
224
  }
191
225
  });
192
226
  }
193
- function applyEdits(textEdits) {
194
- const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, textEdits);
195
- if (newText !== document.getText()) {
196
- document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
197
- }
198
- }
199
227
  });
200
228
  }
201
229
  exports.register = register;
202
230
  function patchInterpolationIndent(document, map) {
203
231
  const indentTextEdits = [];
204
- for (const mapped of map.map.mappings) {
232
+ for (const mapped of map.mappings) {
205
233
  const textRange = {
206
234
  start: document.positionAt(mapped.sourceRange[0]),
207
235
  end: document.positionAt(mapped.sourceRange[1]),
@@ -212,6 +240,8 @@ function patchInterpolationIndent(document, map) {
212
240
  const lines = text.split('\n');
213
241
  const removeIndent = getRemoveIndent(lines);
214
242
  const baseIndent = getBaseIndent(mapped.sourceRange[0]);
243
+ if (removeIndent === baseIndent)
244
+ continue;
215
245
  for (let i = 1; i < lines.length; i++) {
216
246
  const line = lines[i];
217
247
  if (line.startsWith(removeIndent)) {
@@ -237,4 +267,11 @@ function patchInterpolationIndent(document, map) {
237
267
  return startLineText.substring(0, startLineText.length - startLineText.trimStart().length);
238
268
  }
239
269
  }
270
+ function isTsDocument(document) {
271
+ return document.languageId === 'javascript' ||
272
+ document.languageId === 'typescript' ||
273
+ document.languageId === 'javascriptreact' ||
274
+ document.languageId === 'typescriptreact';
275
+ }
276
+ exports.isTsDocument = isTsDocument;
240
277
  //# sourceMappingURL=format.js.map
@@ -1,4 +1,3 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
1
+ import type { LanguageServiceRuntimeContext } from '../types';
3
2
  import * as vscode from 'vscode-languageserver-protocol';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument, position: vscode.Position) => Promise<vscode.LinkedEditingRanges | undefined>;
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string, position: vscode.Position) => Promise<vscode.LinkedEditingRanges | undefined>;
@@ -3,11 +3,11 @@ exports.register = void 0;
3
3
  const featureWorkers_1 = require("../utils/featureWorkers");
4
4
  const shared = require("@volar/shared");
5
5
  function register(context) {
6
- return (document, position) => {
7
- return (0, featureWorkers_1.documentArgFeatureWorker)(context, document, position, () => true, (position, map) => map.toGeneratedPositions(position, data => !!data.completion), (plugin, document, position) => { var _a; return (_a = plugin.findLinkedEditingRanges) === null || _a === void 0 ? void 0 : _a.call(plugin, document, position); }, (data, map) => ({
6
+ return (uri, position) => {
7
+ return (0, featureWorkers_1.languageFeatureWorker)(context, uri, position, (position, map) => map.toGeneratedPositions(position, data => !!data.completion), (plugin, document, position) => { var _a; return (_a = plugin.findLinkedEditingRanges) === null || _a === void 0 ? void 0 : _a.call(plugin, document, position); }, (data, map) => map ? ({
8
8
  wordPattern: data.wordPattern,
9
9
  ranges: data.ranges.map(range => map.toSourceRange(range)).filter(shared.notEmpty),
10
- }));
10
+ }) : data);
11
11
  };
12
12
  }
13
13
  exports.register = register;
@@ -1,4 +1,3 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
1
+ import type { LanguageServiceRuntimeContext } from '../types';
3
2
  import * as vscode from 'vscode-languageserver-protocol';
4
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument, positions: vscode.Position[]) => Promise<vscode.SelectionRange[] | undefined>;
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string, positions: vscode.Position[]) => Promise<vscode.SelectionRange[] | undefined>;
@@ -4,16 +4,18 @@ const featureWorkers_1 = require("../utils/featureWorkers");
4
4
  const transforms_1 = require("@volar/transforms");
5
5
  const shared = require("@volar/shared");
6
6
  function register(context) {
7
- return (document, positions) => {
8
- return (0, featureWorkers_1.documentArgFeatureWorker)(context, document, positions, file => !!file.capabilities.documentFormatting, (positions, map) => {
9
- const result = positions
10
- .map(position => map.toGeneratedPosition(position))
11
- .filter(shared.notEmpty);
12
- if (result.length) {
13
- return [result];
7
+ return (uri, positions) => {
8
+ return (0, featureWorkers_1.languageFeatureWorker)(context, uri, positions, (positions, map, file) => {
9
+ if (file.capabilities.documentFormatting) {
10
+ const result = positions
11
+ .map(position => map.toGeneratedPosition(position))
12
+ .filter(shared.notEmpty);
13
+ if (result.length) {
14
+ return [result];
15
+ }
14
16
  }
15
17
  return [];
16
- }, (plugin, document, positions) => { var _a; return (_a = plugin.getSelectionRanges) === null || _a === void 0 ? void 0 : _a.call(plugin, document, positions); }, (item, map) => (0, transforms_1.transformSelectionRanges)(item, range => map.toSourceRange(range)), results => {
18
+ }, (plugin, document, positions) => { var _a; return (_a = plugin.getSelectionRanges) === null || _a === void 0 ? void 0 : _a.call(plugin, document, positions); }, (item, map) => map ? (0, transforms_1.transformSelectionRanges)(item, range => map.toSourceRange(range)) : item, results => {
17
19
  for (let i = 0; i < results[0].length; i++) {
18
20
  const first = results[0][i];
19
21
  let lastParent = first;
@@ -30,6 +30,7 @@ export declare class MirrorMapWithDocument extends SourceMapWithDocuments<[Mirro
30
30
  findMirrorPositions(start: vscode.Position): Generator<readonly [import("vscode-languageserver-textdocument").Position, MirrorBehaviorCapabilities], void, unknown>;
31
31
  }
32
32
  export declare function createDocumentsAndSourceMaps(mapper: VirtualFiles): {
33
+ getSourceByUri(sourceFileUri: string): readonly [ts.IScriptSnapshot, VirtualFile] | undefined;
33
34
  getRootFileBySourceFileUri(sourceFileUri: string): VirtualFile | undefined;
34
35
  getVirtualFileByUri(virtualFileUri: string): VirtualFile | undefined;
35
36
  getMirrorMapByUri(virtualFileUri: string): [VirtualFile, MirrorMapWithDocument] | undefined;
package/out/documents.js CHANGED
@@ -122,6 +122,10 @@ function createDocumentsAndSourceMaps(mapper) {
122
122
  const _mirrorMaps = new WeakMap();
123
123
  const _documents = new WeakMap();
124
124
  return {
125
+ getSourceByUri(sourceFileUri) {
126
+ const fileName = shared.getPathOfUri(sourceFileUri);
127
+ return mapper.get(fileName);
128
+ },
125
129
  getRootFileBySourceFileUri(sourceFileUri) {
126
130
  const fileName = shared.getPathOfUri(sourceFileUri);
127
131
  const rootFile = mapper.get(fileName);
@@ -202,9 +206,13 @@ function createDocumentsAndSourceMaps(mapper) {
202
206
  };
203
207
  function getDocumentByFileName(snapshot, fileName) {
204
208
  if (!_documents.has(snapshot)) {
205
- _documents.set(snapshot, vscode_languageserver_textdocument_1.TextDocument.create(shared.getUriByPath(fileName), shared.syntaxToLanguageId(fileName.substring(fileName.lastIndexOf('.') + 1)), version++, snapshot.getText(0, snapshot.getLength())));
209
+ _documents.set(snapshot, new Map());
210
+ }
211
+ const map = _documents.get(snapshot);
212
+ if (!map.has(fileName)) {
213
+ map.set(fileName, vscode_languageserver_textdocument_1.TextDocument.create(shared.getUriByPath(fileName), shared.syntaxToLanguageId(fileName.substring(fileName.lastIndexOf('.') + 1)), version++, snapshot.getText(0, snapshot.getLength())));
206
214
  }
207
- return _documents.get(snapshot);
215
+ return map.get(fileName);
208
216
  }
209
217
  }
210
218
  exports.createDocumentsAndSourceMaps = createDocumentsAndSourceMaps;
package/out/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from '@volar/language-core';
2
- export * from './baseDocumentService';
3
2
  export * from './baseLanguageService';
4
3
  export * from './documents';
5
4
  export { executePluginCommand, ExecutePluginCommandArgs } from './languageFeatures/executeCommand';
package/out/index.js CHANGED
@@ -15,7 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.mergeWorkspaceEdits = exports.executePluginCommand = void 0;
17
17
  __exportStar(require("@volar/language-core"), exports);
18
- __exportStar(require("./baseDocumentService"), exports);
19
18
  __exportStar(require("./baseLanguageService"), exports);
20
19
  __exportStar(require("./documents"), exports);
21
20
  var executeCommand_1 = require("./languageFeatures/executeCommand");
@@ -1,4 +1,3 @@
1
1
  import * as vscode from 'vscode-languageserver-protocol';
2
- import type { LanguageServiceRuntimeContext } from '../types';
3
- import { LanguageServicePlugin } from '@volar/language-service';
4
- export declare function register(context: LanguageServiceRuntimeContext): (uri: string, position: vscode.Position, autoInsertContext: Parameters<NonNullable<LanguageServicePlugin['doAutoInsert']>>[2]) => Promise<NonNullable<string | vscode.TextEdit | null | undefined> | undefined>;
2
+ import type { LanguageServicePluginInstance, LanguageServiceRuntimeContext } from '../types';
3
+ export declare function register(context: LanguageServiceRuntimeContext): (uri: string, position: vscode.Position, autoInsertContext: Parameters<NonNullable<LanguageServicePluginInstance['doAutoInsert']>>[2]) => Promise<NonNullable<string | vscode.TextEdit | null | undefined> | undefined>;
@@ -32,7 +32,7 @@ function register(context) {
32
32
  cacheData.list.isIncomplete = false;
33
33
  continue;
34
34
  }
35
- cacheData.list = (0, transforms_1.transformCompletionList)(embeddedCompletionList, range => map.toSourceRange(range), (newItem, oldItem) => newItem.data = {
35
+ cacheData.list = (0, transforms_1.transformCompletionList)(embeddedCompletionList, range => map.toSourceRange(range), map.virtualFileDocument, (newItem, oldItem) => newItem.data = {
36
36
  uri,
37
37
  originalItem: oldItem,
38
38
  pluginId: context.plugins.indexOf(cacheData.plugin),
@@ -100,7 +100,7 @@ function register(context) {
100
100
  if (!isAdditional) {
101
101
  cache.mainCompletion = { documentUri: map.virtualFileDocument.uri };
102
102
  }
103
- const completionList = (0, transforms_1.transformCompletionList)(embeddedCompletionList, range => map.toSourceRange(range), (newItem, oldItem) => newItem.data = {
103
+ const completionList = (0, transforms_1.transformCompletionList)(embeddedCompletionList, range => map.toSourceRange(range), map.virtualFileDocument, (newItem, oldItem) => newItem.data = {
104
104
  uri,
105
105
  originalItem: oldItem,
106
106
  pluginId: context.plugins.indexOf(plugin),
@@ -41,7 +41,7 @@ function register(context) {
41
41
  if (range)
42
42
  return range;
43
43
  return map.toSourceRange(embeddedRange);
44
- });
44
+ }, map.virtualFileDocument);
45
45
  }
46
46
  }
47
47
  else {
@@ -55,7 +55,7 @@ function register(context) {
55
55
  if (!map)
56
56
  return _inlayHint;
57
57
  const position = map.toSourcePosition(_inlayHint.position);
58
- const edits = (_a = _inlayHint.textEdits) === null || _a === void 0 ? void 0 : _a.map(textEdit => (0, transforms_1.transformTextEdit)(textEdit, range => map.toSourceRange(range))).filter(shared.notEmpty);
58
+ const edits = (_a = _inlayHint.textEdits) === null || _a === void 0 ? void 0 : _a.map(textEdit => (0, transforms_1.transformTextEdit)(textEdit, range => map.toSourceRange(range), map.virtualFileDocument)).filter(shared.notEmpty);
59
59
  if (position) {
60
60
  return Object.assign(Object.assign({}, _inlayHint), { position,
61
61
  edits });
package/out/types.d.ts CHANGED
@@ -6,31 +6,18 @@ import type * as vscode from 'vscode-languageserver-protocol';
6
6
  import type { TextDocument } from 'vscode-languageserver-textdocument';
7
7
  import { URI } from 'vscode-uri';
8
8
  import { DocumentsAndSourceMaps } from './documents';
9
+ import { LanguageService } from '@volar/language-service';
9
10
  export * from 'vscode-languageserver-protocol';
10
- export interface DocumentServiceRuntimeContext {
11
- typescript: typeof import('typescript/lib/tsserverlibrary');
12
- plugins: LanguageServicePlugin[];
13
- pluginContext: LanguageServicePluginContext;
14
- documents: DocumentsAndSourceMaps;
15
- update(document: TextDocument): void;
16
- updateVirtualFile(fileName: string, snapshot: ts.IScriptSnapshot): void;
17
- prepareLanguageServices(document: TextDocument): void;
18
- }
19
- export interface LanguageServiceRuntimeContext {
20
- host: LanguageServiceHost;
11
+ export interface LanguageServiceRuntimeContext<Host extends LanguageServiceHost = LanguageServiceHost> {
12
+ host: Host;
21
13
  core: LanguageContext;
22
- typescriptLanguageService: ts.LanguageService;
23
14
  documents: DocumentsAndSourceMaps;
24
- plugins: LanguageServicePlugin[];
25
- pluginContext: LanguageServicePluginContext;
26
- getTextDocument(uri: string): TextDocument | undefined;
27
- }
28
- export interface LanguageServicePluginContext {
15
+ plugins: LanguageServicePluginInstance[];
29
16
  typescript: {
30
17
  module: typeof import('typescript/lib/tsserverlibrary');
31
18
  languageServiceHost: ts.LanguageServiceHost;
32
19
  languageService: ts.LanguageService;
33
- };
20
+ } | undefined;
34
21
  env: {
35
22
  rootUri: URI;
36
23
  configurationHost?: ConfigurationHost;
@@ -38,6 +25,7 @@ export interface LanguageServicePluginContext {
38
25
  fileSystemProvider?: FileSystemProvider;
39
26
  schemaRequestService?: SchemaRequestService;
40
27
  };
28
+ getTextDocument(uri: string): TextDocument | undefined;
41
29
  }
42
30
  export interface ConfigurationHost {
43
31
  getConfiguration: (<T>(section: string, scopeUri?: string) => Promise<T | undefined>);
@@ -65,8 +53,9 @@ export interface ExecuteCommandContext {
65
53
  }): Promise<void>;
66
54
  applyEdit(paramOrEdit: vscode.ApplyWorkspaceEditParams | vscode.WorkspaceEdit): Promise<vscode.ApplyWorkspaceEditResult>;
67
55
  }
68
- export interface LanguageServicePlugin {
69
- setup?(context: LanguageServicePluginContext): void;
56
+ export type LanguageServicePlugin<T = {}> = ((context: LanguageServiceRuntimeContext, service: LanguageService) => LanguageServicePluginInstance & T);
57
+ export interface LanguageServicePluginInstance {
58
+ setup?(context: LanguageServiceRuntimeContext): void;
70
59
  validation?: {
71
60
  onSemantic?(document: TextDocument): NullableResult<vscode.Diagnostic[]>;
72
61
  onSyntactic?(document: TextDocument): NullableResult<vscode.Diagnostic[]>;
package/out/types.js CHANGED
@@ -16,5 +16,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  __exportStar(require("vscode-languageserver-protocol"), exports);
17
17
  ;
18
18
  ;
19
- ;
20
19
  //# sourceMappingURL=types.js.map
@@ -6,3 +6,4 @@ export declare function getOverlapRange(range1Start: number, range1End: number,
6
6
  } | undefined;
7
7
  export declare function isInsideRange(parent: vscode.Range, child: vscode.Range): boolean;
8
8
  export declare function getWordRange(wordPattern: RegExp, position: vscode.Position, document: TextDocument): vscode.Range | undefined;
9
+ export declare function stringToSnapshot(str: string): ts.IScriptSnapshot;
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, "__esModule", { value: true });
2
- exports.getWordRange = exports.isInsideRange = exports.getOverlapRange = void 0;
2
+ exports.stringToSnapshot = exports.getWordRange = exports.isInsideRange = exports.getOverlapRange = void 0;
3
3
  function getOverlapRange(range1Start, range1End, range2Start, range2End) {
4
4
  const start = Math.max(range1Start, range2Start);
5
5
  const end = Math.min(range1End, range2End);
@@ -50,4 +50,12 @@ function getWordRange(wordPattern, position, document) {
50
50
  return undefined;
51
51
  }
52
52
  exports.getWordRange = getWordRange;
53
+ function stringToSnapshot(str) {
54
+ return {
55
+ getText: (start, end) => str.substring(start, end),
56
+ getLength: () => str.length,
57
+ getChangeRange: () => undefined,
58
+ };
59
+ }
60
+ exports.stringToSnapshot = stringToSnapshot;
53
61
  //# sourceMappingURL=common.js.map
@@ -1,7 +1,6 @@
1
1
  import type { TextDocument } from 'vscode-languageserver-textdocument';
2
- import type { DocumentServiceRuntimeContext, LanguageServiceRuntimeContext } from '../types';
3
- import { LanguageServicePlugin, FileRangeCapabilities, VirtualFile } from '@volar/language-service';
2
+ import type { LanguageServicePluginInstance, LanguageServiceRuntimeContext } from '../types';
3
+ import { FileRangeCapabilities, VirtualFile } from '@volar/language-service';
4
4
  import { SourceMapWithDocuments } from '../documents';
5
- export declare function documentFeatureWorker<T>(context: DocumentServiceRuntimeContext, document: TextDocument, isValidSourceMap: (file: VirtualFile, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>) => boolean, worker: (plugin: LanguageServicePlugin, document: TextDocument) => T, transform: (result: NonNullable<Awaited<T>>, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>) => T | undefined, combineResult?: (results: NonNullable<Awaited<T>>[]) => NonNullable<Awaited<T>>): Promise<NonNullable<Awaited<T>> | undefined>;
6
- export declare function documentArgFeatureWorker<T, K>(context: DocumentServiceRuntimeContext, document: TextDocument, arg: K, isValidSourceMap: (file: VirtualFile, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>) => boolean, transformArg: (arg: K, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>) => Generator<K> | K[], worker: (plugin: LanguageServicePlugin, document: TextDocument, arg: K) => T, transform: (result: NonNullable<Awaited<T>>, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>) => T | undefined, combineResult?: (results: NonNullable<Awaited<T>>[]) => NonNullable<Awaited<T>>): Promise<NonNullable<Awaited<T>> | undefined>;
7
- export declare function languageFeatureWorker<T, K>(context: LanguageServiceRuntimeContext, uri: string, arg: K, transformArg: (arg: K, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>, file: VirtualFile) => Generator<K> | K[], worker: (plugin: LanguageServicePlugin, document: TextDocument, arg: K, sourceMap: SourceMapWithDocuments<FileRangeCapabilities> | undefined, file: VirtualFile | undefined) => T, transform: (result: NonNullable<Awaited<T>>, sourceMap: SourceMapWithDocuments<FileRangeCapabilities> | undefined) => Awaited<T> | undefined, combineResult?: (results: NonNullable<Awaited<T>>[]) => NonNullable<Awaited<T>>, reportProgress?: (result: NonNullable<Awaited<T>>) => void): Promise<NonNullable<Awaited<T>> | undefined>;
5
+ export declare function documentFeatureWorker<T>(context: LanguageServiceRuntimeContext, uri: string, isValidSourceMap: (file: VirtualFile, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>) => boolean, worker: (plugin: LanguageServicePluginInstance, document: TextDocument) => T, transform: (result: NonNullable<Awaited<T>>, sourceMap: SourceMapWithDocuments<FileRangeCapabilities> | undefined) => Awaited<T> | undefined, combineResult?: (results: NonNullable<Awaited<T>>[]) => NonNullable<Awaited<T>>): Promise<NonNullable<Awaited<T>> | undefined>;
6
+ export declare function languageFeatureWorker<T, K>(context: LanguageServiceRuntimeContext, uri: string, arg: K, transformArg: (arg: K, sourceMap: SourceMapWithDocuments<FileRangeCapabilities>, file: VirtualFile) => Generator<K> | K[], worker: (plugin: LanguageServicePluginInstance, document: TextDocument, arg: K, sourceMap: SourceMapWithDocuments<FileRangeCapabilities> | undefined, file: VirtualFile | undefined) => T, transform: (result: NonNullable<Awaited<T>>, sourceMap: SourceMapWithDocuments<FileRangeCapabilities> | undefined) => Awaited<T> | undefined, combineResult?: (results: NonNullable<Awaited<T>>[]) => NonNullable<Awaited<T>>, reportProgress?: (result: NonNullable<Awaited<T>>) => void): Promise<NonNullable<Awaited<T>> | undefined>;
@@ -8,60 +8,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.languageFeatureWorker = exports.documentArgFeatureWorker = exports.documentFeatureWorker = void 0;
11
+ exports.languageFeatureWorker = exports.documentFeatureWorker = void 0;
12
12
  const definePlugin_1 = require("./definePlugin");
13
- function documentFeatureWorker(context, document, isValidSourceMap, worker, transform, combineResult) {
13
+ function documentFeatureWorker(context, uri, isValidSourceMap, worker, transform, combineResult) {
14
14
  return __awaiter(this, void 0, void 0, function* () {
15
- return documentArgFeatureWorker(context, document, true, isValidSourceMap, () => [true], worker, transform, combineResult);
16
- });
17
- }
18
- exports.documentFeatureWorker = documentFeatureWorker;
19
- function documentArgFeatureWorker(context, document, arg, isValidSourceMap, transformArg, worker, transform, combineResult) {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- context.update(document);
22
- const virtualFile = context.documents.getRootFileBySourceFileUri(document.uri);
23
- let results = [];
24
- if (virtualFile) {
25
- yield (0, definePlugin_1.visitEmbedded)(context.documents, virtualFile, (file, map) => __awaiter(this, void 0, void 0, function* () {
26
- if (!isValidSourceMap(file, map))
27
- return true;
28
- context.prepareLanguageServices(map.virtualFileDocument);
29
- for (const mappedArg of transformArg(arg, map)) {
30
- for (const plugin of context.plugins) {
31
- const embeddedResult = yield worker(plugin, map.virtualFileDocument, mappedArg);
32
- if (!embeddedResult)
33
- continue;
34
- const result = yield transform(embeddedResult, map);
35
- if (!result)
36
- continue;
37
- results.push(result);
38
- if (!combineResult)
39
- return false;
40
- }
41
- }
42
- return true;
43
- }));
44
- }
45
- else if (results.length === 0 || !!combineResult) {
46
- context.prepareLanguageServices(document);
47
- for (const plugin of context.plugins) {
48
- const result = yield worker(plugin, document, arg);
49
- if (!result)
50
- continue;
51
- results.push(result);
52
- if (!combineResult)
53
- break;
15
+ return languageFeatureWorker(context, uri, true, (_, map, file) => {
16
+ if (isValidSourceMap(file, map)) {
17
+ return [true];
54
18
  }
55
- }
56
- if (combineResult && results.length > 0) {
57
- return combineResult(results);
58
- }
59
- else if (results.length > 0) {
60
- return results[0];
61
- }
19
+ return [];
20
+ }, worker, transform, combineResult);
62
21
  });
63
22
  }
64
- exports.documentArgFeatureWorker = documentArgFeatureWorker;
23
+ exports.documentFeatureWorker = documentFeatureWorker;
65
24
  function languageFeatureWorker(context, uri, arg, transformArg, worker, transform, combineResult, reportProgress) {
66
25
  return __awaiter(this, void 0, void 0, function* () {
67
26
  const document = context.getTextDocument(uri);
@@ -89,7 +48,7 @@ function languageFeatureWorker(context, uri, arg, transformArg, worker, transfor
89
48
  return true;
90
49
  }));
91
50
  }
92
- else if (document && (results.length === 0 || !!combineResult)) {
51
+ else if (document) {
93
52
  for (const plugin of context.plugins) {
94
53
  const embeddedResult = yield worker(plugin, document, arg, undefined, undefined);
95
54
  if (!embeddedResult)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-service",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -13,16 +13,16 @@
13
13
  "directory": "packages/language-service"
14
14
  },
15
15
  "dependencies": {
16
- "@volar/language-core": "1.0.19",
17
- "@volar/shared": "1.0.19",
18
- "@volar/source-map": "1.0.19",
19
- "@volar/transforms": "1.0.19",
20
- "@volar/typescript-faster": "1.0.19",
16
+ "@volar/language-core": "1.0.21",
17
+ "@volar/shared": "1.0.21",
18
+ "@volar/source-map": "1.0.21",
19
+ "@volar/transforms": "1.0.21",
20
+ "@volar/typescript-faster": "1.0.21",
21
21
  "vscode-html-languageservice": "^5.0.3",
22
22
  "vscode-json-languageservice": "^5.1.3",
23
23
  "vscode-languageserver-protocol": "^3.17.2",
24
24
  "vscode-languageserver-textdocument": "^1.0.8",
25
25
  "vscode-uri": "^3.0.7"
26
26
  },
27
- "gitHead": "02267fe83a5567b1cae7025e834cf084c8248546"
27
+ "gitHead": "72332760759e06eed18e62026185cbad70250367"
28
28
  }
@@ -1,33 +0,0 @@
1
- import { LanguageModule } from '@volar/language-core';
2
- import { TextDocument } from 'vscode-languageserver-textdocument';
3
- import { DocumentServiceRuntimeContext, LanguageServicePlugin, LanguageServicePluginContext } from './types';
4
- import type * as _ from 'vscode-languageserver-protocol';
5
- export type DocumentService = ReturnType<typeof createDocumentService>;
6
- export declare function createDocumentServiceContext(options: {
7
- ts: typeof import('typescript/lib/tsserverlibrary');
8
- getLanguageModules(): LanguageModule[];
9
- getPlugins(): LanguageServicePlugin[];
10
- env: LanguageServicePluginContext['env'];
11
- }): DocumentServiceRuntimeContext;
12
- export declare function isTsDocument(document: TextDocument): boolean;
13
- export declare function createDocumentService(context: DocumentServiceRuntimeContext): {
14
- format: (document: TextDocument, options: _.FormattingOptions, range?: _.Range | undefined, onTypeParams?: {
15
- ch: string;
16
- position: _.Position;
17
- } | undefined) => Promise<_.TextEdit[] | undefined>;
18
- getFoldingRanges: (document: TextDocument) => Promise<_.FoldingRange[] | undefined>;
19
- getSelectionRanges: (document: TextDocument, positions: _.Position[]) => Promise<_.SelectionRange[] | undefined>;
20
- findLinkedEditingRanges: (document: TextDocument, position: _.Position) => Promise<_.LinkedEditingRanges | undefined>;
21
- findDocumentSymbols: (document: TextDocument) => Promise<_.SymbolInformation[] | undefined>;
22
- findDocumentColors: (document: TextDocument) => Promise<_.ColorInformation[] | undefined>;
23
- getColorPresentations: (document: TextDocument, color: _.Color, range: _.Range) => Promise<_.ColorPresentation[] | undefined>;
24
- doAutoInsert: (document: TextDocument, position: _.Position, options: {
25
- lastChange: {
26
- range: _.Range;
27
- rangeOffset: number;
28
- rangeLength: number;
29
- text: string;
30
- };
31
- }) => Promise<NonNullable<string | _.TextEdit | null | undefined> | undefined>;
32
- context: DocumentServiceRuntimeContext;
33
- };
@@ -1,85 +0,0 @@
1
- Object.defineProperty(exports, "__esModule", { value: true });
2
- exports.createDocumentService = exports.isTsDocument = exports.createDocumentServiceContext = void 0;
3
- const language_core_1 = require("@volar/language-core");
4
- const shared = require("@volar/shared");
5
- const autoInsert = require("./documentFeatures/autoInsert");
6
- const colorPresentations = require("./documentFeatures/colorPresentations");
7
- const documentColors = require("./documentFeatures/documentColors");
8
- const documentSymbols = require("./documentFeatures/documentSymbols");
9
- const foldingRanges = require("./documentFeatures/foldingRanges");
10
- const format = require("./documentFeatures/format");
11
- const linkedEditingRanges = require("./documentFeatures/linkedEditingRanges");
12
- const selectionRanges = require("./documentFeatures/selectionRanges");
13
- const documents_1 = require("./documents");
14
- const singleFileTypeScriptService_1 = require("./utils/singleFileTypeScriptService");
15
- function createDocumentServiceContext(options) {
16
- let plugins;
17
- const ts = options.ts;
18
- const pluginContext = {
19
- typescript: {
20
- module: ts,
21
- languageServiceHost: singleFileTypeScriptService_1.singleFileTypeScriptServiceHost,
22
- languageService: ts.createLanguageService(singleFileTypeScriptService_1.singleFileTypeScriptServiceHost),
23
- },
24
- env: options.env,
25
- };
26
- const languageModules = options.getLanguageModules();
27
- const lastUpdateVersions = new Map();
28
- const virtualFiles = (0, language_core_1.createVirtualFiles)(languageModules);
29
- const textDocumentMapper = (0, documents_1.createDocumentsAndSourceMaps)(virtualFiles);
30
- const context = {
31
- typescript: ts,
32
- get plugins() {
33
- var _a;
34
- if (!plugins) {
35
- plugins = options.getPlugins();
36
- for (const plugin of plugins) {
37
- (_a = plugin.setup) === null || _a === void 0 ? void 0 : _a.call(plugin, pluginContext);
38
- }
39
- }
40
- return plugins;
41
- },
42
- pluginContext,
43
- documents: textDocumentMapper,
44
- update(document) {
45
- let lastVersion = lastUpdateVersions.get(document.uri);
46
- if (lastVersion === undefined || lastVersion !== document.version) {
47
- const fileName = shared.getPathOfUri(document.uri);
48
- virtualFiles.update(fileName, ts.ScriptSnapshot.fromString(document.getText()));
49
- lastUpdateVersions.set(document.uri, document.version);
50
- }
51
- },
52
- updateVirtualFile(fileName, snapshot) {
53
- virtualFiles.update(fileName, snapshot);
54
- },
55
- prepareLanguageServices(document) {
56
- if (isTsDocument(document)) {
57
- (0, singleFileTypeScriptService_1.updateSingleFileTypeScriptServiceHost)(ts, document);
58
- }
59
- },
60
- };
61
- return context;
62
- }
63
- exports.createDocumentServiceContext = createDocumentServiceContext;
64
- function isTsDocument(document) {
65
- return document.languageId === 'javascript' ||
66
- document.languageId === 'typescript' ||
67
- document.languageId === 'javascriptreact' ||
68
- document.languageId === 'typescriptreact';
69
- }
70
- exports.isTsDocument = isTsDocument;
71
- function createDocumentService(context) {
72
- return {
73
- format: format.register(context),
74
- getFoldingRanges: foldingRanges.register(context),
75
- getSelectionRanges: selectionRanges.register(context),
76
- findLinkedEditingRanges: linkedEditingRanges.register(context),
77
- findDocumentSymbols: documentSymbols.register(context),
78
- findDocumentColors: documentColors.register(context),
79
- getColorPresentations: colorPresentations.register(context),
80
- doAutoInsert: autoInsert.register(context),
81
- context,
82
- };
83
- }
84
- exports.createDocumentService = createDocumentService;
85
- //# sourceMappingURL=baseDocumentService.js.map
@@ -1,5 +0,0 @@
1
- import type { DocumentServiceRuntimeContext } from '../types';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
3
- import { LanguageServicePlugin } from '@volar/language-service';
4
- import * as vscode from 'vscode-languageserver-protocol';
5
- export declare function register(context: DocumentServiceRuntimeContext): (document: TextDocument, position: vscode.Position, options: Parameters<NonNullable<LanguageServicePlugin['doAutoInsert']>>[2]) => Promise<NonNullable<string | vscode.TextEdit | null | undefined> | undefined>;
@@ -1,32 +0,0 @@
1
- Object.defineProperty(exports, "__esModule", { value: true });
2
- exports.register = void 0;
3
- const featureWorkers_1 = require("../utils/featureWorkers");
4
- function register(context) {
5
- return (document, position, options) => {
6
- return (0, featureWorkers_1.documentArgFeatureWorker)(context, document, { position, options }, () => true, function* ({ position, options }, map) {
7
- for (const mappedPos of map.toGeneratedPositions(position, data => !!data.completion)) {
8
- for (const [mappedChangeOffset] of map.map.toGeneratedOffsets(options.lastChange.rangeOffset)) {
9
- yield {
10
- position: mappedPos,
11
- options: {
12
- lastChange: Object.assign(Object.assign({}, options.lastChange), { rangeOffset: mappedChangeOffset, range: {
13
- start: map.virtualFileDocument.positionAt(mappedChangeOffset),
14
- end: map.virtualFileDocument.positionAt(mappedChangeOffset + options.lastChange.rangeLength),
15
- } })
16
- }
17
- };
18
- }
19
- }
20
- }, (plugin, document, { position, options }) => { var _a; return (_a = plugin.doAutoInsert) === null || _a === void 0 ? void 0 : _a.call(plugin, document, position, options); }, (data, map) => {
21
- if (typeof data === 'string')
22
- return data;
23
- const range = map.toSourceRange(data.range);
24
- if (range) {
25
- data.range = range;
26
- return data;
27
- }
28
- });
29
- };
30
- }
31
- exports.register = register;
32
- //# sourceMappingURL=autoInsert.js.map
@@ -1,4 +0,0 @@
1
- import type * as ts from 'typescript/lib/tsserverlibrary';
2
- import type { TextDocument } from 'vscode-languageserver-textdocument';
3
- export declare const singleFileTypeScriptServiceHost: ts.LanguageServiceHost;
4
- export declare function updateSingleFileTypeScriptServiceHost(ts: typeof import('typescript/lib/tsserverlibrary'), _doc: TextDocument): void;
@@ -1,30 +0,0 @@
1
- Object.defineProperty(exports, "__esModule", { value: true });
2
- exports.updateSingleFileTypeScriptServiceHost = exports.singleFileTypeScriptServiceHost = void 0;
3
- const shared = require("@volar/shared");
4
- let projectVersion = 0;
5
- let doc;
6
- let scriptFileName;
7
- let scriptSnapshot;
8
- exports.singleFileTypeScriptServiceHost = {
9
- readFile: () => undefined,
10
- fileExists: fileName => fileName === scriptFileName,
11
- getProjectVersion: () => projectVersion.toString(),
12
- getScriptVersion: () => projectVersion.toString(),
13
- getCompilationSettings: () => ({ allowJs: true, jsx: 1 }),
14
- getScriptFileNames: () => [scriptFileName],
15
- getScriptSnapshot: fileName => {
16
- if (fileName === scriptFileName) {
17
- return scriptSnapshot;
18
- }
19
- },
20
- getCurrentDirectory: () => '',
21
- getDefaultLibFileName: () => '',
22
- };
23
- function updateSingleFileTypeScriptServiceHost(ts, _doc) {
24
- projectVersion++;
25
- doc = _doc;
26
- scriptFileName = shared.getPathOfUri(_doc.uri);
27
- scriptSnapshot = ts.ScriptSnapshot.fromString(doc.getText());
28
- }
29
- exports.updateSingleFileTypeScriptServiceHost = updateSingleFileTypeScriptServiceHost;
30
- //# sourceMappingURL=singleFileTypeScriptService.js.map