@volar/language-service 1.2.0-alpha.2 → 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,
@@ -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
21
  Object.assign(item, data.original);
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
- }
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
- original: Pick<vscode.CodeAction, 'data'>;
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,8 +54,8 @@ 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;
@@ -63,19 +64,19 @@ function register(context) {
63
64
  const data = diagnostic.data;
64
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
- original: {
71
- data: _codeAction.data,
72
- },
73
- pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
74
- map: map ? {
75
- embeddedDocumentUri: map.virtualFileDocument.uri,
76
- } : undefined,
77
- } });
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
+ };
78
78
  });
79
+ return codeActions;
79
80
  }), (_codeActions, sourceMap) => _codeActions.map(_codeAction => {
80
81
  if (!sourceMap)
81
82
  return _codeAction;
@@ -89,11 +90,69 @@ function register(context) {
89
90
  else {
90
91
  return _codeAction;
91
92
  }
92
- }).filter(shared.notEmpty), arr => arr.flat());
93
- if (codeActions) {
94
- 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
+ }
95
133
  }
134
+ return [
135
+ ...pluginActions !== null && pluginActions !== void 0 ? pluginActions : [],
136
+ ...ruleActions,
137
+ ];
96
138
  });
97
139
  }
98
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
+ }
99
158
  //# sourceMappingURL=codeActions.js.map
@@ -9,5 +9,7 @@ export interface PluginDiagnosticData {
9
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,8 +190,11 @@ 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',
@@ -198,9 +202,12 @@ function register(context) {
198
202
  original: {
199
203
  data: error.data,
200
204
  },
205
+ ruleFixIndex: index,
206
+ documentUri: ruleCtx.document.uri,
201
207
  };
202
208
  });
203
209
  errorsUpdated = true;
210
+ const errors = reportResults.map(reportResult => reportResult[0]);
204
211
  pluginCache.set(ruleCtx.document.uri, {
205
212
  documentVersion: ruleCtx.document.version,
206
213
  errors,
@@ -254,6 +261,8 @@ function register(context) {
254
261
  original: {
255
262
  data: error.data,
256
263
  },
264
+ ruleFixIndex: 0,
265
+ documentUri: document.uri,
257
266
  };
258
267
  });
259
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.2",
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.2",
17
- "@volar/shared": "1.2.0-alpha.2",
18
- "@volar/source-map": "1.2.0-alpha.2",
19
- "@volar/typescript-faster": "1.2.0-alpha.2",
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": "cc4da3728cbbc0933c9957c298d0a36090fbedc1"
26
+ "gitHead": "651fb9568432d0bfcab86fc1cd9fcfaae87e1d6d"
27
27
  }