All files / src/objects/rename rename_global_class.ts

87.09% Statements 54/62
60% Branches 6/10
100% Functions 2/2
87.09% Lines 54/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 7x     7x 7x 7x     7x 7x 7x 7x 7x 7x 7x 34x 7x 7x     7x 34x 7x 7x     7x 7x 34x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x
import * as Statements from "../../abap/2_statements/statements";
import * as Expressions from "../../abap/2_statements/expressions";
import {WorkspaceEdit, TextDocumentEdit, CreateFile, RenameFile, DeleteFile, TextEdit} from "vscode-languageserver-types";
import {IRegistry} from "../../_iregistry";
import {Class} from "..";
import {LSPUtils} from "../../lsp/_lsp_utils";
import {ObjectRenamer} from "./_object_renamer";
import {RenamerHelper} from "./renamer_helper";
import {IObject} from "../_iobject";
 
export class RenameGlobalClass implements ObjectRenamer {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public buildEdits(obj: IObject, oldName: string, newName: string): WorkspaceEdit | undefined {
    if (!(obj instanceof Class)) {
      throw new Error("not a class");
    }
 
    const main = obj.getMainABAPFile();
    if (main === undefined) {
      throw new Error("Main file not found");
    }
 
    let changes: (TextDocumentEdit | CreateFile | RenameFile | DeleteFile)[] = [];
 
    // todo, this is actually the same as "id" ?
    {
      const edits: TextEdit[] = [];
      for (const s of main.getStatements()) {
        if (s.get() instanceof Statements.ClassDefinition) {
          const exp = s.findFirstExpression(Expressions.ClassName);
          if (exp === undefined) {
            continue;
          }
          edits.push(TextEdit.replace(LSPUtils.tokenToRange(exp.getFirstToken()), newName.toLowerCase()));
        } else if (s.get() instanceof Statements.ClassImplementation) {
          const exp = s.findFirstExpression(Expressions.ClassName);
          if (exp === undefined) {
            continue;
          }
          edits.push(TextEdit.replace(LSPUtils.tokenToRange(exp.getFirstToken()), newName.toLowerCase()));
        }
      }
      changes.push(TextDocumentEdit.create({uri: main.getFilename(), version: 1}, edits));
    }
 
    const helper = new RenamerHelper(this.reg);
    changes = changes.concat(helper.buildXMLFileEdits(obj, "CLSNAME", oldName, newName));
    changes = changes.concat(helper.renameFiles(obj, oldName, newName));
    changes = changes.concat(helper.renameReferences(obj.getIdentifier(), oldName, newName));
    changes = changes.concat(helper.renameDDICTABLReferences(obj, oldName, newName));
 
    return {
      documentChanges: changes,
    };
  }
 
}