@volar/language-service
Advanced tools
Comparing version 1.2.0-alpha.2 to 1.2.0-alpha.3
@@ -58,2 +58,4 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
const context = { | ||
uriToFileName: shared.uriToFileName, | ||
fileNameToUri: shared.fileNameToUri, | ||
host, | ||
@@ -60,0 +62,0 @@ core: languageContext, |
@@ -15,5 +15,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return (item) => __awaiter(this, void 0, void 0, function* () { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
const data = item.data; | ||
if (data) { | ||
if ((data === null || data === void 0 ? void 0 : data.type) === 'plugin') { | ||
const plugin = context.plugins[data.pluginId]; | ||
@@ -23,17 +23,24 @@ if (!((_a = plugin.codeAction) === null || _a === void 0 ? void 0 : _a.resolve)) | ||
Object.assign(item, data.original); | ||
if (data.map) { | ||
if (context.documents.hasVirtualFileByUri(data.map.embeddedDocumentUri)) { | ||
const resolvedItem = yield ((_b = plugin.codeAction) === null || _b === void 0 ? void 0 : _b.resolve(item)); | ||
if (resolvedItem.edit) { | ||
const edit = (0, rename_1.embeddedEditToSourceEdit)(resolvedItem.edit, context.documents); | ||
if (edit) { | ||
resolvedItem.edit = edit; | ||
return resolvedItem; | ||
} | ||
item = yield plugin.codeAction.resolve(item); | ||
if (item.edit) { | ||
item.edit = (0, rename_1.embeddedEditToSourceEdit)(item.edit, context.documents); | ||
} | ||
} | ||
if ((data === null || data === void 0 ? void 0 : data.type) === 'rule') { | ||
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]; | ||
const fix = fixes === null || fixes === void 0 ? void 0 : fixes[1][data.index]; | ||
if (fix) { | ||
let edit = (_f = yield ((_e = fix.getWorkspaceEdit) === null || _e === void 0 ? void 0 : _e.call(fix, fixes[0]))) !== null && _f !== void 0 ? _f : undefined; | ||
if (!edit) { | ||
const edits = yield ((_g = fix.getEdits) === null || _g === void 0 ? void 0 : _g.call(fix, fixes[0])); | ||
if (edits) { | ||
edit = { | ||
changes: { [data.documentUri]: edits }, | ||
}; | ||
} | ||
} | ||
if (edit) { | ||
item.edit = (0, rename_1.embeddedEditToSourceEdit)(edit, context.documents); | ||
} | ||
} | ||
else { | ||
return yield ((_c = plugin.codeAction) === null || _c === void 0 ? void 0 : _c.resolve(item)); | ||
} | ||
} | ||
@@ -40,0 +47,0 @@ return item; |
@@ -5,8 +5,14 @@ import * as vscode from 'vscode-languageserver-protocol'; | ||
uri: string; | ||
original: Pick<vscode.CodeAction, 'data'>; | ||
type: 'plugin'; | ||
original: Pick<vscode.CodeAction, 'data' | 'edit'>; | ||
pluginId: string; | ||
map: { | ||
embeddedDocumentUri: string; | ||
} | undefined; | ||
} | ||
export interface RuleCodeActionData { | ||
uri: string; | ||
documentUri: string; | ||
type: 'rule'; | ||
ruleId: string; | ||
ruleFixIndex: number; | ||
index: number; | ||
} | ||
export declare function register(context: LanguageServiceRuntimeContext): (uri: string, range: vscode.Range, codeActionContext: vscode.CodeActionContext) => Promise<vscode.CodeAction[] | undefined>; |
@@ -21,2 +21,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return (uri, range, codeActionContext) => __awaiter(this, void 0, void 0, function* () { | ||
var _a, _b, _c, _d, _e; | ||
const document = context.getTextDocument(uri); | ||
@@ -29,3 +30,3 @@ if (!document) | ||
}; | ||
let codeActions = yield (0, featureWorkers_1.languageFeatureWorker)(context, uri, { range, codeActionContext }, (_arg, map, file) => { | ||
const pluginActions = yield (0, featureWorkers_1.languageFeatureWorker)(context, uri, { range, codeActionContext }, (_arg, map, file) => { | ||
var _a, _b; | ||
@@ -58,4 +59,4 @@ if (!file.capabilities.codeAction) | ||
return []; | ||
}, (plugin, document, { range, codeActionContext }, map) => __awaiter(this, void 0, void 0, function* () { | ||
var _a, _b; | ||
}, (plugin, document, { range, codeActionContext }) => __awaiter(this, void 0, void 0, function* () { | ||
var _f, _g; | ||
const pluginId = Object.keys(context.plugins).find(key => context.plugins[key] === plugin); | ||
@@ -69,15 +70,15 @@ const diagnostics = codeActionContext.diagnostics.filter(diagnostic => { | ||
}); | ||
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 }))); | ||
return codeActions === null || codeActions === void 0 ? void 0 : codeActions.map(_codeAction => { | ||
return Object.assign(Object.assign({}, _codeAction), { data: { | ||
uri, | ||
original: { | ||
data: _codeAction.data, | ||
}, | ||
pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin), | ||
map: map ? { | ||
embeddedDocumentUri: map.virtualFileDocument.uri, | ||
} : undefined, | ||
} }); | ||
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 }))); | ||
codeActions === null || codeActions === void 0 ? void 0 : codeActions.forEach(codeAction => { | ||
codeAction.data = { | ||
uri, | ||
type: 'plugin', | ||
original: { | ||
data: codeAction.data, | ||
edit: codeAction.edit, | ||
}, | ||
pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin), | ||
}; | ||
}); | ||
return codeActions; | ||
}), (_codeActions, sourceMap) => _codeActions.map(_codeAction => { | ||
@@ -96,9 +97,67 @@ if (!sourceMap) | ||
} | ||
}).filter(shared.notEmpty), arr => arr.flat()); | ||
if (codeActions) { | ||
return dedupe.withCodeAction(codeActions); | ||
}).filter(shared.notEmpty), arr => dedupe.withCodeAction(arr.flat())); | ||
const ruleActions = []; | ||
for (const diagnostic of codeActionContext.diagnostics) { | ||
const data = diagnostic.data; | ||
if ((data === null || data === void 0 ? void 0 : data.type) === 'rule') { | ||
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]; | ||
if (fixes) { | ||
for (let i = 0; i < fixes[1].length; i++) { | ||
const fix = fixes[1][i]; | ||
const matchKinds = []; | ||
if (!codeActionContext.only) { | ||
matchKinds.push(undefined); | ||
} | ||
else { | ||
for (const kind of (_d = fix.kinds) !== null && _d !== void 0 ? _d : ['quickfix', 'source.fixAll']) { | ||
const matchOnly = matchOnlyKind(codeActionContext.only, kind); | ||
if (matchOnly) { | ||
matchKinds.push(matchOnly); | ||
} | ||
} | ||
} | ||
for (const matchKind of matchKinds) { | ||
const action = { | ||
title: (_e = fix.title) !== null && _e !== void 0 ? _e : `Fix: ${diagnostic.message}`, | ||
kind: matchKind, | ||
diagnostics: [diagnostic], | ||
data: { | ||
uri, | ||
type: 'rule', | ||
ruleId: data.pluginOrRuleId, | ||
documentUri: data.documentUri, | ||
ruleFixIndex: data.ruleFixIndex, | ||
index: i, | ||
}, | ||
}; | ||
ruleActions.push(action); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return [ | ||
...pluginActions !== null && pluginActions !== void 0 ? pluginActions : [], | ||
...ruleActions, | ||
]; | ||
}); | ||
} | ||
exports.register = register; | ||
function matchOnlyKind(only, kind) { | ||
const b = kind.split('.'); | ||
for (const onlyKind of only) { | ||
const a = onlyKind.split('.'); | ||
if (a.length <= b.length) { | ||
let matchNum = 0; | ||
for (let i = 0; i < a.length; i++) { | ||
if (a[i] == b[i]) { | ||
matchNum++; | ||
} | ||
} | ||
if (matchNum === a.length) { | ||
return onlyKind; | ||
} | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=codeActions.js.map |
@@ -12,3 +12,5 @@ import * as vscode from 'vscode-languageserver-protocol'; | ||
pluginOrRuleId: string; | ||
ruleFixIndex: number; | ||
documentUri: string; | ||
} | ||
export declare function register(context: LanguageServiceRuntimeContext): (uri: string, token?: vscode.CancellationToken, response?: ((result: vscode.Diagnostic[]) => void) | undefined) => Promise<vscode.Diagnostic[]>; |
@@ -95,4 +95,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
semantic_rules: new Map(), | ||
syntactic_rules: new Map(), | ||
formatic_rules: new Map(), | ||
syntax_rules: new Map(), | ||
format_rules: new Map(), | ||
}; | ||
@@ -107,4 +107,4 @@ return (uri, token, response) => __awaiter(this, void 0, void 0, function* () { | ||
semantic_rules: { errors: [] }, | ||
syntactic_rules: { errors: [] }, | ||
formatic_rules: { errors: [] }, | ||
syntax_rules: { errors: [] }, | ||
format_rules: { errors: [] }, | ||
}).get(uri); | ||
@@ -138,5 +138,5 @@ const newSnapshot = context.host.getScriptSnapshot(shared.uriToFileName(uri)); | ||
} | ||
yield lintWorker('onFormat', cacheMaps.formatic_rules, lastResponse.formatic_rules); | ||
yield lintWorker('onFormat', cacheMaps.format_rules, lastResponse.format_rules); | ||
doResponse(); | ||
yield lintWorker('onSyntax', cacheMaps.syntactic_rules, lastResponse.syntactic_rules); | ||
yield lintWorker('onSyntax', cacheMaps.syntax_rules, lastResponse.syntax_rules); | ||
doResponse(); | ||
@@ -165,3 +165,4 @@ yield worker('onSyntactic', cacheMaps.syntactic, lastResponse.syntactic); | ||
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* () { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
var _h, _j, _k, _l; | ||
if (token) { | ||
@@ -196,4 +197,7 @@ if (Date.now() - lastCheckCancelAt >= 5) { | ||
yield ((_d = rule[api]) === null || _d === void 0 ? void 0 : _d.call(rule, ruleCtx)); | ||
const errors = reportResults.map(reportResult => reportResult[0]); | ||
errors === null || errors === void 0 ? void 0 : errors.forEach(error => { | ||
(_e = context.ruleFixes) !== null && _e !== void 0 ? _e : (context.ruleFixes = {}); | ||
(_f = (_h = context.ruleFixes)[_j = ruleCtx.document.uri]) !== null && _f !== void 0 ? _f : (_h[_j] = {}); | ||
(_g = (_k = context.ruleFixes[ruleCtx.document.uri])[_l = ruleCtx.ruleId]) !== null && _g !== void 0 ? _g : (_k[_l] = {}); | ||
reportResults === null || reportResults === void 0 ? void 0 : reportResults.forEach(([error, ...fixes], index) => { | ||
context.ruleFixes[ruleCtx.document.uri][ruleCtx.ruleId][index] = [error, fixes]; | ||
error.data = { | ||
@@ -206,5 +210,8 @@ uri, | ||
}, | ||
ruleFixIndex: index, | ||
documentUri: ruleCtx.document.uri, | ||
}; | ||
}); | ||
errorsUpdated = true; | ||
const errors = reportResults.map(reportResult => reportResult[0]); | ||
pluginCache.set(ruleCtx.document.uri, { | ||
@@ -263,2 +270,4 @@ documentVersion: ruleCtx.document.version, | ||
}, | ||
ruleFixIndex: 0, | ||
documentUri: document.uri, | ||
}; | ||
@@ -265,0 +274,0 @@ }); |
@@ -24,2 +24,4 @@ import { LanguageContext, LanguageModule, LanguageServiceHost } from '@volar/language-core'; | ||
}; | ||
uriToFileName(uri: string): string; | ||
fileNameToUri(fileName: string): string; | ||
/** @private */ | ||
@@ -41,2 +43,10 @@ host: Host; | ||
getTextDocument(uri: string): TextDocument | undefined; | ||
/** @private */ | ||
ruleFixes?: { | ||
[uri: string]: { | ||
[ruleId: string]: { | ||
[ruleFixId: number]: [vscode.Diagnostic, RuleFix[]]; | ||
}; | ||
}; | ||
}; | ||
} | ||
@@ -151,8 +161,12 @@ export interface ConfigurationHost { | ||
document: TextDocument; | ||
report(error: vscode.Diagnostic, ...fixes: { | ||
kinds?: string[]; | ||
getCodeActions(): NullableResult<vscode.CodeAction[]>; | ||
resolveCodeAction?(codeAction: vscode.CodeAction): NotNullableResult<vscode.CodeAction>; | ||
}[]): void; | ||
uriToFileName(uri: string): string; | ||
fileNameToUri(fileName: string): string; | ||
report(error: vscode.Diagnostic, ...fixes: RuleFix[]): void; | ||
} | ||
export interface RuleFix { | ||
kinds?: ('' | 'quickfix' | 'refactor' | 'refactor.extract' | 'refactor.inline' | 'refactor.rewrite' | 'source' | 'source.organizeImports' | 'source.fixAll')[]; | ||
title?: string; | ||
getEdits?(diagnostic: vscode.Diagnostic): NullableResult<vscode.TextEdit[]>; | ||
getWorkspaceEdit?(diagnostic: vscode.Diagnostic): NullableResult<vscode.WorkspaceEdit>; | ||
} | ||
export interface LanguageServiceConfig { | ||
@@ -159,0 +173,0 @@ languages?: ConfigValue<LanguageModule>; |
@@ -13,2 +13,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
const definePlugin_1 = require("./definePlugin"); | ||
const shared = require("@volar/shared"); | ||
function documentFeatureWorker(context, uri, isValidSourceMap, worker, transform, combineResult) { | ||
@@ -94,2 +95,4 @@ return __awaiter(this, void 0, void 0, function* () { | ||
document: map.virtualFileDocument, | ||
uriToFileName: shared.uriToFileName, | ||
fileNameToUri: shared.fileNameToUri, | ||
report: () => { }, | ||
@@ -127,2 +130,4 @@ }; | ||
document, | ||
uriToFileName: shared.uriToFileName, | ||
fileNameToUri: shared.fileNameToUri, | ||
report: () => { }, | ||
@@ -129,0 +134,0 @@ }; |
{ | ||
"name": "@volar/language-service", | ||
"version": "1.2.0-alpha.2", | ||
"version": "1.2.0-alpha.3", | ||
"main": "out/index.js", | ||
@@ -16,6 +16,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"@volar/language-core": "1.2.0-alpha.2", | ||
"@volar/shared": "1.2.0-alpha.2", | ||
"@volar/source-map": "1.2.0-alpha.2", | ||
"@volar/typescript-faster": "1.2.0-alpha.2", | ||
"@volar/language-core": "1.2.0-alpha.3", | ||
"@volar/shared": "1.2.0-alpha.3", | ||
"@volar/source-map": "1.2.0-alpha.3", | ||
"@volar/typescript-faster": "1.2.0-alpha.3", | ||
"vscode-html-languageservice": "^5.0.3", | ||
@@ -27,3 +27,3 @@ "vscode-json-languageservice": "^5.1.3", | ||
}, | ||
"gitHead": "cc4da3728cbbc0933c9957c298d0a36090fbedc1" | ||
"gitHead": "651fb9568432d0bfcab86fc1cd9fcfaae87e1d6d" | ||
} |
211256
3841
+ Added@volar/language-core@1.2.0-alpha.3(transitive)
+ Added@volar/shared@1.2.0-alpha.3(transitive)
+ Added@volar/source-map@1.2.0-alpha.3(transitive)
+ Added@volar/typescript-faster@1.2.0-alpha.3(transitive)
- Removed@volar/language-core@1.2.0-alpha.2(transitive)
- Removed@volar/shared@1.2.0-alpha.2(transitive)
- Removed@volar/source-map@1.2.0-alpha.2(transitive)
- Removed@volar/typescript-faster@1.2.0-alpha.2(transitive)
Updated@volar/shared@1.2.0-alpha.3