@abaplint/core
Version:
abaplint - Core API
67 lines • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenameProgram = void 0;
const Statements = require("../../abap/2_statements/statements");
const Expressions = require("../../abap/2_statements/expressions");
const vscode_languageserver_types_1 = require("vscode-languageserver-types");
const __1 = require("..");
const renamer_helper_1 = require("./renamer_helper");
const _lsp_utils_1 = require("../../lsp/_lsp_utils");
const _abap_object_1 = require("../_abap_object");
class RenameProgram {
constructor(reg) {
this.reg = reg;
}
buildEdits(obj, oldName, newName) {
if (!(obj instanceof __1.Program)) {
throw new Error("RenameProgram, not a program");
}
const main = obj.getMainABAPFile();
if (main === undefined) {
throw new Error(`Main file not found, ${obj.getType()} ${obj.getName()}`);
}
let changes = [];
const helper = new renamer_helper_1.RenamerHelper(this.reg);
changes = changes.concat(helper.buildXMLFileEdits(obj, "NAME", oldName, newName));
changes = changes.concat(helper.renameFiles(obj, oldName, newName));
const edits = [];
for (const s of main.getStatements()) {
if (s.get() instanceof Statements.Report || s.get() instanceof Statements.Program) {
const exp = s.findFirstExpression(Expressions.ReportName);
if (exp) {
edits.push(vscode_languageserver_types_1.TextEdit.replace(_lsp_utils_1.LSPUtils.tokenToRange(exp.getFirstToken()), newName.toLowerCase()));
}
}
}
if (edits.length > 0) {
changes.push(vscode_languageserver_types_1.TextDocumentEdit.create({ uri: main.getFilename(), version: 1 }, edits));
}
// Rename INCLUDE statements in all ABAP objects
for (const o of this.reg.getObjects()) {
if (o instanceof _abap_object_1.ABAPObject && o !== obj) {
for (const file of o.getABAPFiles()) {
const includeEdits = [];
for (const s of file.getStatements()) {
if (s.get() instanceof Statements.Include ||
s.get() instanceof Statements.Submit ||
s.get() instanceof Statements.Perform) {
for (const exp of s.findAllExpressions(Expressions.IncludeName)) {
if (exp && exp.getFirstToken().getStr().toUpperCase() === oldName.toUpperCase()) {
includeEdits.push(vscode_languageserver_types_1.TextEdit.replace(_lsp_utils_1.LSPUtils.tokenToRange(exp.getFirstToken()), newName.toLowerCase()));
}
}
}
}
if (includeEdits.length > 0) {
changes.push(vscode_languageserver_types_1.TextDocumentEdit.create({ uri: file.getFilename(), version: 1 }, includeEdits));
}
}
}
}
return {
documentChanges: changes,
};
}
}
exports.RenameProgram = RenameProgram;
//# sourceMappingURL=rename_program.js.map