@volar/language-service 1.2.0-alpha.1 → 1.2.0-alpha.3

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.
@@ -55,6 +55,8 @@ function createLanguageServiceContext(host, languageContext, config, env, getLan
55
55
  const documents = new WeakMap();
56
56
  const documentVersions = new Map();
57
57
  const context = {
58
+ uriToFileName: shared.uriToFileName,
59
+ fileNameToUri: shared.fileNameToUri,
58
60
  host,
59
61
  core: languageContext,
60
62
  env: env,
@@ -2,7 +2,7 @@ import type * as vscode from 'vscode-languageserver-protocol';
2
2
  import type { LanguageServiceRuntimeContext } from '../types';
3
3
  export interface PluginCallHierarchyData {
4
4
  uri: string;
5
- originalData: any;
5
+ original: Pick<vscode.CallHierarchyItem, 'data'>;
6
6
  pluginId: string;
7
7
  map: {
8
8
  embeddedDocumentUri: string;
@@ -22,7 +22,9 @@ function register(context) {
22
22
  items === null || items === void 0 ? void 0 : items.forEach(item => {
23
23
  item.data = {
24
24
  uri,
25
- originalData: item.data,
25
+ original: {
26
+ data: item.data,
27
+ },
26
28
  pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
27
29
  map: map ? {
28
30
  embeddedDocumentUri: map.virtualFileDocument.uri,
@@ -42,7 +44,7 @@ function register(context) {
42
44
  const plugin = context.plugins[data.pluginId];
43
45
  if (!plugin.callHierarchy)
44
46
  return incomingItems;
45
- item.data = data.originalData;
47
+ Object.assign(item, data.original);
46
48
  if (data.map) {
47
49
  if (context.documents.hasVirtualFileByUri(data.map.embeddedDocumentUri)) {
48
50
  const _calls = yield plugin.callHierarchy.onIncomingCalls(item);
@@ -81,7 +83,7 @@ function register(context) {
81
83
  const plugin = context.plugins[data.pluginId];
82
84
  if (!plugin.callHierarchy)
83
85
  return items;
84
- item.data = data.originalData;
86
+ Object.assign(item, data.original);
85
87
  if (data.map) {
86
88
  if (context.documents.hasVirtualFileByUri(data.map.embeddedDocumentUri)) {
87
89
  const _calls = yield plugin.callHierarchy.onOutgoingCalls(item);
@@ -12,27 +12,34 @@ exports.register = void 0;
12
12
  const rename_1 = require("./rename");
13
13
  function register(context) {
14
14
  return (item) => __awaiter(this, void 0, void 0, function* () {
15
- var _a, _b, _c;
15
+ var _a, _b, _c, _d, _e, _f, _g;
16
16
  const data = item.data;
17
- if (data) {
17
+ if ((data === null || data === void 0 ? void 0 : data.type) === 'plugin') {
18
18
  const plugin = context.plugins[data.pluginId];
19
19
  if (!((_a = plugin.codeAction) === null || _a === void 0 ? void 0 : _a.resolve))
20
20
  return item;
21
- item.data = data.originalData;
22
- if (data.map) {
23
- if (context.documents.hasVirtualFileByUri(data.map.embeddedDocumentUri)) {
24
- const resolvedItem = yield ((_b = plugin.codeAction) === null || _b === void 0 ? void 0 : _b.resolve(item));
25
- if (resolvedItem.edit) {
26
- const edit = (0, rename_1.embeddedEditToSourceEdit)(resolvedItem.edit, context.documents);
27
- if (edit) {
28
- resolvedItem.edit = edit;
29
- return resolvedItem;
30
- }
21
+ Object.assign(item, data.original);
22
+ item = yield plugin.codeAction.resolve(item);
23
+ if (item.edit) {
24
+ item.edit = (0, rename_1.embeddedEditToSourceEdit)(item.edit, context.documents);
25
+ }
26
+ }
27
+ if ((data === null || data === void 0 ? void 0 : data.type) === 'rule') {
28
+ const fixes = (_d = (_c = (_b = context.ruleFixes) === null || _b === void 0 ? void 0 : _b[data.documentUri]) === null || _c === void 0 ? void 0 : _c[data.ruleId]) === null || _d === void 0 ? void 0 : _d[data.ruleFixIndex];
29
+ const fix = fixes === null || fixes === void 0 ? void 0 : fixes[1][data.index];
30
+ if (fix) {
31
+ let edit = (_f = yield ((_e = fix.getWorkspaceEdit) === null || _e === void 0 ? void 0 : _e.call(fix, fixes[0]))) !== null && _f !== void 0 ? _f : undefined;
32
+ if (!edit) {
33
+ const edits = yield ((_g = fix.getEdits) === null || _g === void 0 ? void 0 : _g.call(fix, fixes[0]));
34
+ if (edits) {
35
+ edit = {
36
+ changes: { [data.documentUri]: edits },
37
+ };
31
38
  }
32
39
  }
33
- }
34
- else {
35
- return yield ((_c = plugin.codeAction) === null || _c === void 0 ? void 0 : _c.resolve(item));
40
+ if (edit) {
41
+ item.edit = (0, rename_1.embeddedEditToSourceEdit)(edit, context.documents);
42
+ }
36
43
  }
37
44
  }
38
45
  return item;
@@ -2,10 +2,16 @@ import * as vscode from 'vscode-languageserver-protocol';
2
2
  import type { LanguageServiceRuntimeContext } from '../types';
3
3
  export interface PluginCodeActionData {
4
4
  uri: string;
5
- originalData: any;
5
+ type: 'plugin';
6
+ original: Pick<vscode.CodeAction, 'data' | 'edit'>;
6
7
  pluginId: string;
7
- map: {
8
- embeddedDocumentUri: string;
9
- } | undefined;
8
+ }
9
+ export interface RuleCodeActionData {
10
+ uri: string;
11
+ documentUri: string;
12
+ type: 'rule';
13
+ ruleId: string;
14
+ ruleFixIndex: number;
15
+ index: number;
10
16
  }
11
17
  export declare function register(context: LanguageServiceRuntimeContext): (uri: string, range: vscode.Range, codeActionContext: vscode.CodeActionContext) => Promise<vscode.CodeAction[] | undefined>;
@@ -18,6 +18,7 @@ const featureWorkers_1 = require("../utils/featureWorkers");
18
18
  const rename_1 = require("./rename");
19
19
  function register(context) {
20
20
  return (uri, range, codeActionContext) => __awaiter(this, void 0, void 0, function* () {
21
+ var _a, _b, _c, _d, _e;
21
22
  const document = context.getTextDocument(uri);
22
23
  if (!document)
23
24
  return;
@@ -25,7 +26,7 @@ function register(context) {
25
26
  start: document.offsetAt(range.start),
26
27
  end: document.offsetAt(range.end),
27
28
  };
28
- let codeActions = yield (0, featureWorkers_1.languageFeatureWorker)(context, uri, { range, codeActionContext }, (_arg, map, file) => {
29
+ const pluginActions = yield (0, featureWorkers_1.languageFeatureWorker)(context, uri, { range, codeActionContext }, (_arg, map, file) => {
29
30
  var _a, _b;
30
31
  if (!file.capabilities.codeAction)
31
32
  return [];
@@ -53,27 +54,29 @@ function register(context) {
53
54
  }];
54
55
  }
55
56
  return [];
56
- }, (plugin, document, { range, codeActionContext }, map) => __awaiter(this, void 0, void 0, function* () {
57
- var _a, _b;
57
+ }, (plugin, document, { range, codeActionContext }) => __awaiter(this, void 0, void 0, function* () {
58
+ var _f, _g;
58
59
  const pluginId = Object.keys(context.plugins).find(key => context.plugins[key] === plugin);
59
60
  const diagnostics = codeActionContext.diagnostics.filter(diagnostic => {
60
61
  const data = diagnostic.data;
61
62
  return (data === null || data === void 0 ? void 0 : data.type) === 'plugin' && (data === null || data === void 0 ? void 0 : data.pluginOrRuleId) === pluginId;
62
63
  }).map(diagnostic => {
63
64
  const data = diagnostic.data;
64
- return Object.assign(Object.assign({}, diagnostic), { data: data.originalData });
65
+ return Object.assign(Object.assign({}, diagnostic), data.original);
65
66
  });
66
- const codeActions = yield ((_b = (_a = plugin.codeAction) === null || _a === void 0 ? void 0 : _a.on) === null || _b === void 0 ? void 0 : _b.call(_a, document, range, Object.assign(Object.assign({}, codeActionContext), { diagnostics })));
67
- return codeActions === null || codeActions === void 0 ? void 0 : codeActions.map(_codeAction => {
68
- return Object.assign(Object.assign({}, _codeAction), { data: {
69
- uri,
70
- originalData: _codeAction.data,
71
- pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
72
- map: map ? {
73
- embeddedDocumentUri: map.virtualFileDocument.uri,
74
- } : undefined,
75
- } });
67
+ const codeActions = yield ((_g = (_f = plugin.codeAction) === null || _f === void 0 ? void 0 : _f.on) === null || _g === void 0 ? void 0 : _g.call(_f, document, range, Object.assign(Object.assign({}, codeActionContext), { diagnostics })));
68
+ codeActions === null || codeActions === void 0 ? void 0 : codeActions.forEach(codeAction => {
69
+ codeAction.data = {
70
+ uri,
71
+ type: 'plugin',
72
+ original: {
73
+ data: codeAction.data,
74
+ edit: codeAction.edit,
75
+ },
76
+ pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
77
+ };
76
78
  });
79
+ return codeActions;
77
80
  }), (_codeActions, sourceMap) => _codeActions.map(_codeAction => {
78
81
  if (!sourceMap)
79
82
  return _codeAction;
@@ -87,11 +90,69 @@ function register(context) {
87
90
  else {
88
91
  return _codeAction;
89
92
  }
90
- }).filter(shared.notEmpty), arr => arr.flat());
91
- if (codeActions) {
92
- return dedupe.withCodeAction(codeActions);
93
+ }).filter(shared.notEmpty), arr => dedupe.withCodeAction(arr.flat()));
94
+ const ruleActions = [];
95
+ for (const diagnostic of codeActionContext.diagnostics) {
96
+ const data = diagnostic.data;
97
+ if ((data === null || data === void 0 ? void 0 : data.type) === 'rule') {
98
+ const fixes = (_c = (_b = (_a = context.ruleFixes) === null || _a === void 0 ? void 0 : _a[data.documentUri]) === null || _b === void 0 ? void 0 : _b[data.pluginOrRuleId]) === null || _c === void 0 ? void 0 : _c[data.ruleFixIndex];
99
+ if (fixes) {
100
+ for (let i = 0; i < fixes[1].length; i++) {
101
+ const fix = fixes[1][i];
102
+ const matchKinds = [];
103
+ if (!codeActionContext.only) {
104
+ matchKinds.push(undefined);
105
+ }
106
+ else {
107
+ for (const kind of (_d = fix.kinds) !== null && _d !== void 0 ? _d : ['quickfix', 'source.fixAll']) {
108
+ const matchOnly = matchOnlyKind(codeActionContext.only, kind);
109
+ if (matchOnly) {
110
+ matchKinds.push(matchOnly);
111
+ }
112
+ }
113
+ }
114
+ for (const matchKind of matchKinds) {
115
+ const action = {
116
+ title: (_e = fix.title) !== null && _e !== void 0 ? _e : `Fix: ${diagnostic.message}`,
117
+ kind: matchKind,
118
+ diagnostics: [diagnostic],
119
+ data: {
120
+ uri,
121
+ type: 'rule',
122
+ ruleId: data.pluginOrRuleId,
123
+ documentUri: data.documentUri,
124
+ ruleFixIndex: data.ruleFixIndex,
125
+ index: i,
126
+ },
127
+ };
128
+ ruleActions.push(action);
129
+ }
130
+ }
131
+ }
132
+ }
93
133
  }
134
+ return [
135
+ ...pluginActions !== null && pluginActions !== void 0 ? pluginActions : [],
136
+ ...ruleActions,
137
+ ];
94
138
  });
95
139
  }
96
140
  exports.register = register;
141
+ function matchOnlyKind(only, kind) {
142
+ const b = kind.split('.');
143
+ for (const onlyKind of only) {
144
+ const a = onlyKind.split('.');
145
+ if (a.length <= b.length) {
146
+ let matchNum = 0;
147
+ for (let i = 0; i < a.length; i++) {
148
+ if (a[i] == b[i]) {
149
+ matchNum++;
150
+ }
151
+ }
152
+ if (matchNum === a.length) {
153
+ return onlyKind;
154
+ }
155
+ }
156
+ }
157
+ }
97
158
  //# sourceMappingURL=codeActions.js.map
@@ -2,7 +2,7 @@ import type { LanguageServiceRuntimeContext } from '../types';
2
2
  import type * as _ from 'vscode-languageserver-protocol';
3
3
  export interface PluginCodeLensData {
4
4
  uri: string;
5
- originalData: any;
5
+ original: Pick<_.CodeLens, 'data'>;
6
6
  pluginId: string;
7
7
  }
8
8
  export declare function register(context: LanguageServiceRuntimeContext): (uri: string) => Promise<_.CodeLens[]>;
@@ -29,7 +29,9 @@ function register(context) {
29
29
  }
30
30
  codeLens.data = {
31
31
  uri,
32
- originalData: codeLens.data,
32
+ original: {
33
+ data: codeLens.data,
34
+ },
33
35
  pluginId,
34
36
  };
35
37
  });
@@ -18,7 +18,7 @@ function register(context) {
18
18
  const plugin = context.plugins[data.pluginId];
19
19
  if (!((_a = plugin.codeLens) === null || _a === void 0 ? void 0 : _a.resolve))
20
20
  return item;
21
- item.data = data.originalData;
21
+ Object.assign(item, data.original);
22
22
  item = yield plugin.codeLens.resolve(item);
23
23
  if (item.command) {
24
24
  item.command = {
@@ -2,7 +2,7 @@ import * as vscode from 'vscode-languageserver-protocol';
2
2
  import type { LanguageServiceRuntimeContext } from '../types';
3
3
  export interface PluginCompletionData {
4
4
  uri: string;
5
- originalData: any;
5
+ original: Pick<vscode.CompletionItem, 'additionalTextEdits' | 'textEdit' | 'data'>;
6
6
  pluginId: string;
7
7
  map: {
8
8
  embeddedDocumentUri: string;
@@ -34,7 +34,11 @@ function register(context) {
34
34
  }
35
35
  cacheData.list = transformer.asCompletionList(embeddedCompletionList, range => map.toSourceRange(range), map.virtualFileDocument, (newItem, oldItem) => newItem.data = {
36
36
  uri,
37
- originalData: oldItem.data,
37
+ original: {
38
+ additionalTextEdits: oldItem.additionalTextEdits,
39
+ textEdit: oldItem.textEdit,
40
+ data: oldItem.data,
41
+ },
38
42
  pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === cacheData.plugin),
39
43
  map: {
40
44
  embeddedDocumentUri: map.virtualFileDocument.uri,
@@ -54,7 +58,11 @@ function register(context) {
54
58
  completionList.items.forEach(item => {
55
59
  item.data = {
56
60
  uri,
57
- originalData: item.data,
61
+ original: {
62
+ additionalTextEdits: item.additionalTextEdits,
63
+ textEdit: item.textEdit,
64
+ data: item.data,
65
+ },
58
66
  pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === cacheData.plugin),
59
67
  map: undefined,
60
68
  };
@@ -104,7 +112,11 @@ function register(context) {
104
112
  }
105
113
  const completionList = transformer.asCompletionList(embeddedCompletionList, range => map.toSourceRange(range), map.virtualFileDocument, (newItem, oldItem) => newItem.data = {
106
114
  uri,
107
- originalData: oldItem.data,
115
+ original: {
116
+ additionalTextEdits: oldItem.additionalTextEdits,
117
+ textEdit: oldItem.textEdit,
118
+ data: oldItem.data,
119
+ },
108
120
  pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
109
121
  map: {
110
122
  embeddedDocumentUri: map.virtualFileDocument.uri,
@@ -146,7 +158,11 @@ function register(context) {
146
158
  completionList.items.forEach(item => {
147
159
  item.data = {
148
160
  uri,
149
- originalData: item.data,
161
+ original: {
162
+ additionalTextEdits: item.additionalTextEdits,
163
+ textEdit: item.textEdit,
164
+ data: item.data,
165
+ },
150
166
  pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
151
167
  map: undefined,
152
168
  };
@@ -18,7 +18,7 @@ function register(context) {
18
18
  const plugin = context.plugins[data.pluginId];
19
19
  if (!((_a = plugin.complete) === null || _a === void 0 ? void 0 : _a.resolve))
20
20
  return item;
21
- item.data = data.originalData;
21
+ item = Object.assign(item, data.original);
22
22
  if (data.map) {
23
23
  for (const [_, map] of context.documents.getMapsByVirtualFileUri(data.map.embeddedDocumentUri)) {
24
24
  const resolvedItem = yield plugin.complete.resolve(item);
@@ -6,8 +6,10 @@ export declare function updateRange(range: vscode.Range, change: {
6
6
  }): vscode.Range | undefined;
7
7
  export interface PluginDiagnosticData {
8
8
  uri: string;
9
- originalData: any;
9
+ original: Pick<vscode.Diagnostic, 'data'>;
10
10
  type: 'plugin' | 'rule';
11
11
  pluginOrRuleId: string;
12
+ ruleFixIndex: number;
13
+ documentUri: string;
12
14
  }
13
15
  export declare function register(context: LanguageServiceRuntimeContext): (uri: string, token?: vscode.CancellationToken, response?: ((result: vscode.Diagnostic[]) => void) | undefined) => Promise<vscode.Diagnostic[]>;
@@ -92,8 +92,8 @@ function register(context) {
92
92
  syntactic: new Map(),
93
93
  suggestion: new Map(),
94
94
  semantic_rules: new Map(),
95
- syntactic_rules: new Map(),
96
- formatic_rules: new Map(),
95
+ syntax_rules: new Map(),
96
+ format_rules: new Map(),
97
97
  };
98
98
  return (uri, token, response) => __awaiter(this, void 0, void 0, function* () {
99
99
  var _a;
@@ -103,8 +103,8 @@ function register(context) {
103
103
  suggestion: { errors: [] },
104
104
  syntactic: { errors: [] },
105
105
  semantic_rules: { errors: [] },
106
- syntactic_rules: { errors: [] },
107
- formatic_rules: { errors: [] },
106
+ syntax_rules: { errors: [] },
107
+ format_rules: { errors: [] },
108
108
  }).get(uri);
109
109
  const newSnapshot = context.host.getScriptSnapshot(shared.uriToFileName(uri));
110
110
  const newDocument = context.getTextDocument(uri);
@@ -133,9 +133,9 @@ function register(context) {
133
133
  }
134
134
  }
135
135
  }
136
- yield lintWorker('onFormat', cacheMaps.formatic_rules, lastResponse.formatic_rules);
136
+ yield lintWorker('onFormat', cacheMaps.format_rules, lastResponse.format_rules);
137
137
  doResponse();
138
- yield lintWorker('onSyntax', cacheMaps.syntactic_rules, lastResponse.syntactic_rules);
138
+ yield lintWorker('onSyntax', cacheMaps.syntax_rules, lastResponse.syntax_rules);
139
139
  doResponse();
140
140
  yield worker('onSyntactic', cacheMaps.syntactic, lastResponse.syntactic);
141
141
  doResponse();
@@ -159,7 +159,8 @@ function register(context) {
159
159
  function lintWorker(api, cacheMap, cache) {
160
160
  return __awaiter(this, void 0, void 0, function* () {
161
161
  const result = yield (0, featureWorkers_1.ruleWorker)(context, uri, file => api === 'onFormat' ? !!file.capabilities.documentFormatting : !!file.capabilities.diagnostic, (ruleName, rule, ruleCtx) => __awaiter(this, void 0, void 0, function* () {
162
- var _a, _b, _c, _d;
162
+ var _a, _b, _c, _d, _e, _f, _g;
163
+ var _h, _j, _k, _l;
163
164
  if (token) {
164
165
  if (Date.now() - lastCheckCancelAt >= 5) {
165
166
  yield shared.sleep(5); // wait for LSP event polling
@@ -189,16 +190,24 @@ function register(context) {
189
190
  reportResults.push([error, ...fixes]);
190
191
  };
191
192
  yield ((_d = rule[api]) === null || _d === void 0 ? void 0 : _d.call(rule, ruleCtx));
192
- const errors = reportResults.map(reportResult => reportResult[0]);
193
- errors === null || errors === void 0 ? void 0 : errors.forEach(error => {
193
+ (_e = context.ruleFixes) !== null && _e !== void 0 ? _e : (context.ruleFixes = {});
194
+ (_f = (_h = context.ruleFixes)[_j = ruleCtx.document.uri]) !== null && _f !== void 0 ? _f : (_h[_j] = {});
195
+ (_g = (_k = context.ruleFixes[ruleCtx.document.uri])[_l = ruleCtx.ruleId]) !== null && _g !== void 0 ? _g : (_k[_l] = {});
196
+ reportResults === null || reportResults === void 0 ? void 0 : reportResults.forEach(([error, ...fixes], index) => {
197
+ context.ruleFixes[ruleCtx.document.uri][ruleCtx.ruleId][index] = [error, fixes];
194
198
  error.data = {
195
199
  uri,
196
200
  type: 'rule',
197
201
  pluginOrRuleId: ruleCtx.ruleId,
198
- originalData: error.data,
202
+ original: {
203
+ data: error.data,
204
+ },
205
+ ruleFixIndex: index,
206
+ documentUri: ruleCtx.document.uri,
199
207
  };
200
208
  });
201
209
  errorsUpdated = true;
210
+ const errors = reportResults.map(reportResult => reportResult[0]);
202
211
  pluginCache.set(ruleCtx.document.uri, {
203
212
  documentVersion: ruleCtx.document.version,
204
213
  errors,
@@ -249,7 +258,11 @@ function register(context) {
249
258
  uri,
250
259
  type: 'plugin',
251
260
  pluginOrRuleId: pluginId,
252
- originalData: error.data,
261
+ original: {
262
+ data: error.data,
263
+ },
264
+ ruleFixIndex: 0,
265
+ documentUri: document.uri,
253
266
  };
254
267
  });
255
268
  errorsUpdated = true;
package/out/types.d.ts CHANGED
@@ -21,6 +21,8 @@ export interface LanguageServiceRuntimeContext<Host extends LanguageServiceHost
21
21
  fileSystemProvider?: FileSystemProvider;
22
22
  schemaRequestService?: SchemaRequestService;
23
23
  };
24
+ uriToFileName(uri: string): string;
25
+ fileNameToUri(fileName: string): string;
24
26
  /** @private */
25
27
  host: Host;
26
28
  /** @private */
@@ -37,6 +39,14 @@ export interface LanguageServiceRuntimeContext<Host extends LanguageServiceHost
37
39
  };
38
40
  /** @private */
39
41
  getTextDocument(uri: string): TextDocument | undefined;
42
+ /** @private */
43
+ ruleFixes?: {
44
+ [uri: string]: {
45
+ [ruleId: string]: {
46
+ [ruleFixId: number]: [vscode.Diagnostic, RuleFix[]];
47
+ };
48
+ };
49
+ };
40
50
  }
41
51
  export interface ConfigurationHost {
42
52
  getConfiguration: (<T>(section: string, scopeUri?: string) => Promise<T | undefined>);
@@ -146,11 +156,15 @@ export interface Rule {
146
156
  export interface RuleContext {
147
157
  ruleId: string;
148
158
  document: TextDocument;
149
- report(error: vscode.Diagnostic, ...fixes: {
150
- kinds?: string[];
151
- getCodeActions(): NullableResult<vscode.CodeAction[]>;
152
- resolveCodeAction?(codeAction: vscode.CodeAction): NotNullableResult<vscode.CodeAction>;
153
- }[]): void;
159
+ uriToFileName(uri: string): string;
160
+ fileNameToUri(fileName: string): string;
161
+ report(error: vscode.Diagnostic, ...fixes: RuleFix[]): void;
162
+ }
163
+ export interface RuleFix {
164
+ kinds?: ('' | 'quickfix' | 'refactor' | 'refactor.extract' | 'refactor.inline' | 'refactor.rewrite' | 'source' | 'source.organizeImports' | 'source.fixAll')[];
165
+ title?: string;
166
+ getEdits?(diagnostic: vscode.Diagnostic): NullableResult<vscode.TextEdit[]>;
167
+ getWorkspaceEdit?(diagnostic: vscode.Diagnostic): NullableResult<vscode.WorkspaceEdit>;
154
168
  }
155
169
  export interface LanguageServiceConfig {
156
170
  languages?: ConfigValue<LanguageModule>;
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.ruleWorker = exports.languageFeatureWorker = exports.documentFeatureWorker = void 0;
12
12
  const definePlugin_1 = require("./definePlugin");
13
+ const shared = require("@volar/shared");
13
14
  function documentFeatureWorker(context, uri, isValidSourceMap, worker, transform, combineResult) {
14
15
  return __awaiter(this, void 0, void 0, function* () {
15
16
  return languageFeatureWorker(context, uri, undefined, (_, map, file) => {
@@ -90,6 +91,8 @@ function ruleWorker(context, uri, isValidSourceMap, worker, transform, combineRe
90
91
  const ruleCtx = {
91
92
  ruleId: '',
92
93
  document: map.virtualFileDocument,
94
+ uriToFileName: shared.uriToFileName,
95
+ fileNameToUri: shared.fileNameToUri,
93
96
  report: () => { },
94
97
  };
95
98
  for (const plugin of Object.values(context.plugins)) {
@@ -122,6 +125,8 @@ function ruleWorker(context, uri, isValidSourceMap, worker, transform, combineRe
122
125
  const ruleCtx = {
123
126
  ruleId: '',
124
127
  document,
128
+ uriToFileName: shared.uriToFileName,
129
+ fileNameToUri: shared.fileNameToUri,
125
130
  report: () => { },
126
131
  };
127
132
  for (const plugin of Object.values(context.plugins)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-service",
3
- "version": "1.2.0-alpha.1",
3
+ "version": "1.2.0-alpha.3",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -13,15 +13,15 @@
13
13
  "directory": "packages/language-service"
14
14
  },
15
15
  "dependencies": {
16
- "@volar/language-core": "1.2.0-alpha.1",
17
- "@volar/shared": "1.2.0-alpha.1",
18
- "@volar/source-map": "1.2.0-alpha.1",
19
- "@volar/typescript-faster": "1.2.0-alpha.1",
16
+ "@volar/language-core": "1.2.0-alpha.3",
17
+ "@volar/shared": "1.2.0-alpha.3",
18
+ "@volar/source-map": "1.2.0-alpha.3",
19
+ "@volar/typescript-faster": "1.2.0-alpha.3",
20
20
  "vscode-html-languageservice": "^5.0.3",
21
21
  "vscode-json-languageservice": "^5.1.3",
22
22
  "vscode-languageserver-protocol": "^3.17.2",
23
23
  "vscode-languageserver-textdocument": "^1.0.8",
24
24
  "vscode-uri": "^3.0.7"
25
25
  },
26
- "gitHead": "08f0fd7c8a0ac7ba92de978c3ee7338d1919ebd7"
26
+ "gitHead": "651fb9568432d0bfcab86fc1cd9fcfaae87e1d6d"
27
27
  }
@@ -1,7 +0,0 @@
1
- import * as vscode from 'vscode-languageserver-protocol';
2
- import type { LanguageServiceRuntimeContext } from '../types';
3
- export declare function updateRange(range: vscode.Range, change: {
4
- range: vscode.Range;
5
- newEnd: vscode.Position;
6
- }): vscode.Range | undefined;
7
- export declare function register(context: LanguageServiceRuntimeContext): (uri: string, token?: vscode.CancellationToken, response?: ((result: vscode.Diagnostic[]) => void) | undefined) => Promise<vscode.Diagnostic[]>;
@@ -1,234 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.register = exports.updateRange = void 0;
12
- const shared = require("@volar/shared");
13
- const dedupe = require("../utils/dedupe");
14
- const featureWorkers_1 = require("../utils/featureWorkers");
15
- function updateRange(range, change) {
16
- if (!updatePosition(range.start, change, false)) {
17
- return;
18
- }
19
- if (!updatePosition(range.end, change, true)) {
20
- return;
21
- }
22
- if (range.end.line === range.start.line && range.end.character <= range.start.character) {
23
- range.end.character++;
24
- }
25
- return range;
26
- }
27
- exports.updateRange = updateRange;
28
- function updatePosition(position, change, isEnd) {
29
- if (change.range.end.line > position.line) {
30
- if (change.newEnd.line > position.line) {
31
- // No change
32
- return true;
33
- }
34
- else if (change.newEnd.line === position.line) {
35
- position.character = Math.min(position.character, change.newEnd.character);
36
- return true;
37
- }
38
- else if (change.newEnd.line < position.line) {
39
- position.line = change.newEnd.line;
40
- position.character = change.newEnd.character;
41
- return true;
42
- }
43
- }
44
- else if (change.range.end.line === position.line) {
45
- const characterDiff = change.newEnd.character - change.range.end.character;
46
- if (position.character >= change.range.end.character) {
47
- if (change.newEnd.line !== change.range.end.line) {
48
- position.line = change.newEnd.line;
49
- position.character = change.newEnd.character + position.character - change.range.end.character;
50
- }
51
- else {
52
- if (isEnd ? change.range.end.character < position.character : change.range.end.character <= position.character) {
53
- position.character += characterDiff;
54
- }
55
- else {
56
- const offset = change.range.end.character - position.character;
57
- if (-characterDiff > offset) {
58
- position.character += characterDiff + offset;
59
- }
60
- }
61
- }
62
- return true;
63
- }
64
- else {
65
- if (change.newEnd.line === change.range.end.line) {
66
- const offset = change.range.end.character - position.character;
67
- if (-characterDiff > offset) {
68
- position.character += characterDiff + offset;
69
- }
70
- }
71
- else if (change.newEnd.line < change.range.end.line) {
72
- position.line = change.newEnd.line;
73
- position.character = change.newEnd.character;
74
- }
75
- else {
76
- // No change
77
- }
78
- return true;
79
- }
80
- }
81
- else if (change.range.end.line < position.line) {
82
- position.line += change.newEnd.line - change.range.end.line;
83
- return true;
84
- }
85
- return false;
86
- }
87
- function register(context) {
88
- const responseCache = new Map();
89
- const cacheMaps = {
90
- semantic: new Map(),
91
- declaration: new Map(),
92
- syntactic: new Map(),
93
- suggestion: new Map(),
94
- };
95
- return (uri, token, response) => __awaiter(this, void 0, void 0, function* () {
96
- var _a;
97
- const lastResponse = (_a = responseCache.get(uri)) !== null && _a !== void 0 ? _a : responseCache.set(uri, {
98
- semantic: { errors: [] },
99
- declaration: { errors: [] },
100
- suggestion: { errors: [] },
101
- syntactic: { errors: [] },
102
- }).get(uri);
103
- const newSnapshot = context.host.getScriptSnapshot(shared.uriToFileName(uri));
104
- const newDocument = context.getTextDocument(uri);
105
- let updateCacheRangeFailed = false;
106
- let errorsUpdated = false;
107
- let lastCheckCancelAt = 0;
108
- for (const cache of Object.values(lastResponse)) {
109
- const oldSnapshot = cache.snapshot;
110
- const oldDocument = cache.document;
111
- const change = oldSnapshot ? newSnapshot === null || newSnapshot === void 0 ? void 0 : newSnapshot.getChangeRange(oldSnapshot) : undefined;
112
- cache.snapshot = newSnapshot;
113
- cache.document = newDocument;
114
- if (!updateCacheRangeFailed && newDocument && oldSnapshot && oldDocument && newSnapshot && change) {
115
- const changeRange = {
116
- range: {
117
- start: oldDocument.positionAt(change.span.start),
118
- end: oldDocument.positionAt(change.span.start + change.span.length),
119
- },
120
- newEnd: newDocument.positionAt(change.span.start + change.newLength),
121
- };
122
- for (const error of cache.errors) {
123
- if (!updateRange(error.range, changeRange)) {
124
- updateCacheRangeFailed = true;
125
- break;
126
- }
127
- }
128
- }
129
- }
130
- yield worker('onSyntactic', cacheMaps.syntactic, lastResponse.syntactic);
131
- doResponse();
132
- yield worker('onSuggestion', cacheMaps.suggestion, lastResponse.suggestion);
133
- doResponse();
134
- yield worker('onSemantic', cacheMaps.semantic, lastResponse.semantic);
135
- doResponse();
136
- yield worker('onDeclaration', cacheMaps.declaration, lastResponse.declaration);
137
- return collectErrors();
138
- function doResponse() {
139
- if (errorsUpdated && !updateCacheRangeFailed) {
140
- response === null || response === void 0 ? void 0 : response(collectErrors());
141
- errorsUpdated = false;
142
- }
143
- }
144
- function collectErrors() {
145
- return Object.values(lastResponse).flatMap(({ errors }) => errors);
146
- }
147
- function worker(api, cacheMap, cache) {
148
- return __awaiter(this, void 0, void 0, function* () {
149
- const result = yield (0, featureWorkers_1.languageFeatureWorker)(context, uri, true, function* (arg, _, file) {
150
- if (file.capabilities.diagnostic) {
151
- yield arg;
152
- }
153
- }, (plugin, document) => __awaiter(this, void 0, void 0, function* () {
154
- var _a, _b, _c, _d, _e;
155
- if (token) {
156
- if (Date.now() - lastCheckCancelAt >= 5) {
157
- yield shared.sleep(5); // wait for LSP event polling
158
- lastCheckCancelAt = Date.now();
159
- }
160
- if (token.isCancellationRequested)
161
- return;
162
- }
163
- const pluginId = context.plugins.indexOf(plugin);
164
- const pluginCache = (_a = cacheMap.get(pluginId)) !== null && _a !== void 0 ? _a : cacheMap.set(pluginId, new Map()).get(pluginId);
165
- const cache = pluginCache.get(document.uri);
166
- const tsProjectVersion = (api === 'onDeclaration' || api === 'onSemantic') ? (_c = (_b = context.core.typescript.languageServiceHost).getProjectVersion) === null || _c === void 0 ? void 0 : _c.call(_b) : undefined;
167
- if (api === 'onDeclaration' || api === 'onSemantic') {
168
- if (cache && cache.documentVersion === document.version && cache.tsProjectVersion === tsProjectVersion) {
169
- return cache.errors;
170
- }
171
- }
172
- else {
173
- if (cache && cache.documentVersion === document.version) {
174
- return cache.errors;
175
- }
176
- }
177
- const errors = yield ((_e = (_d = plugin.validation) === null || _d === void 0 ? void 0 : _d[api]) === null || _e === void 0 ? void 0 : _e.call(_d, document));
178
- errorsUpdated = true;
179
- pluginCache.set(document.uri, {
180
- documentVersion: document.version,
181
- errors,
182
- tsProjectVersion,
183
- });
184
- return errors;
185
- }), (errors, map) => transformErrorRange(map, errors), arr => dedupe.withDiagnostics(arr.flat()));
186
- if (result) {
187
- cache.errors = result;
188
- cache.snapshot = newSnapshot;
189
- }
190
- });
191
- }
192
- });
193
- function transformErrorRange(map, errors) {
194
- const result = [];
195
- for (const error of errors) {
196
- // clone it to avoid modify cache
197
- let _error = Object.assign({}, error);
198
- if (map) {
199
- const range = map.toSourceRange(error.range, data => !!data.diagnostic);
200
- if (!range) {
201
- continue;
202
- }
203
- _error.range = range;
204
- }
205
- if (_error.relatedInformation) {
206
- const relatedInfos = [];
207
- for (const info of _error.relatedInformation) {
208
- if (context.documents.hasVirtualFileByUri(info.location.uri)) {
209
- for (const [_, map] of context.documents.getMapsByVirtualFileUri(info.location.uri)) {
210
- const range = map.toSourceRange(info.location.range, data => !!data.diagnostic);
211
- if (range) {
212
- relatedInfos.push({
213
- location: {
214
- uri: map.sourceFileDocument.uri,
215
- range,
216
- },
217
- message: info.message,
218
- });
219
- }
220
- }
221
- }
222
- else {
223
- relatedInfos.push(info);
224
- }
225
- }
226
- _error.relatedInformation = relatedInfos;
227
- }
228
- result.push(_error);
229
- }
230
- return result;
231
- }
232
- }
233
- exports.register = register;
234
- //# sourceMappingURL=validation%20copy.js.map