@abaplint/core
Version:
abaplint - Core API
93 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeActions = void 0;
const LServer = require("vscode-languageserver-types");
const diagnostics_1 = require("./diagnostics");
const position_1 = require("../position");
const _edit_1 = require("./_edit");
class CodeActions {
constructor(reg) {
this.reg = reg;
}
find(params) {
const diag = new diagnostics_1.Diagnostics(this.reg);
const issues = diag.findIssues(params.textDocument);
const totals = {};
const shown = new Set();
const ret = [];
for (const i of issues) {
const fix = i.getDefaultFix();
if (fix !== undefined) {
if (totals[i.getKey()] === undefined) {
totals[i.getKey()] = 1;
}
else {
totals[i.getKey()]++;
}
if (this.inRange(i, params.range) === true) {
ret.push({
title: "Apply fix, " + i.getKey(),
kind: LServer.CodeActionKind.QuickFix,
diagnostics: [diagnostics_1.Diagnostics.mapDiagnostic(i)],
isPreferred: true,
edit: _edit_1.LSPEdit.mapEdit(fix),
});
shown.add(i.getKey());
}
}
for (const alternative of i.getAlternativeFixes() || []) {
if (this.inRange(i, params.range) === true) {
ret.push({
title: alternative.description,
kind: LServer.CodeActionKind.QuickFix,
diagnostics: [diagnostics_1.Diagnostics.mapDiagnostic(i)],
isPreferred: true,
edit: _edit_1.LSPEdit.mapEdit(alternative.edit),
});
shown.add(i.getKey());
}
}
}
for (const s of shown) {
if (totals[s] > 1) {
const foo = this.fixAlls(s, issues);
ret.push(foo);
}
}
return ret;
}
//////////////////////
fixAlls(key, issues) {
const diagnostics = [];
const fixes = [];
for (const i of issues) {
if (i.getKey() !== key) {
continue;
}
const fix = i.getDefaultFix();
if (fix === undefined) {
continue;
}
fixes.push(fix);
diagnostics.push(diagnostics_1.Diagnostics.mapDiagnostic(i));
}
return {
title: "Fix all, " + key,
kind: LServer.CodeActionKind.QuickFix,
diagnostics,
isPreferred: true,
edit: _edit_1.LSPEdit.mapEdits(fixes),
};
}
inRange(i, range) {
const start = new position_1.Position(range.start.line + 1, range.start.character + 1);
const end = new position_1.Position(range.end.line + 1, range.end.character + 1);
return i.getStart().isBetween(start, end)
|| i.getEnd().isBetween(start, end)
|| start.isBetween(i.getStart(), i.getEnd())
|| end.isBetween(i.getStart(), i.getEnd())
|| end.equals(i.getEnd());
}
}
exports.CodeActions = CodeActions;
//# sourceMappingURL=code_actions.js.map