All files / src/objects/rename renamer_helper.ts

96.46% Statements 109/113
86.96% Branches 20/23
100% Functions 6/6
96.46% Lines 109/113

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 11x     11x 11x 17x 17x     17x 17x 17x 11x 11x 11x 11x 1x 1x 11x 11x 11x 11x 6x 6x 5x 5x 5x 5x 11x 78x 78x 5x 5x 5x 5x 78x 5x 5x 5x 1x 1x 11x 11x 11x 11x 11x 16x 16x 16x 16x 11x 11x 11x 1x 1x 1x 1x 11x 11x 11x 11x 10x 10x 10x 10x 10x 10x 10x 10x 11x 11x 1x 1x 66x 66x 66x 49x 21x 21x 21x 10x 10x 21x 49x 66x 66x 49x 49x 66x 66x 66x 1x 1x
import {Range, RenameFile, TextDocumentEdit, TextEdit} from "vscode-languageserver-types";
import {ReferenceType} from "../..";
import {Identifier} from "../../abap/4_file_information/_identifier";
import {SyntaxLogic} from "../../abap/5_syntax/syntax";
import {ScopeType} from "../../abap/5_syntax/_scope_type";
import {ISpaghettiScopeNode} from "../../abap/5_syntax/_spaghetti_scope";
import {VirtualPosition} from "../../position";
import {IRegistry} from "../../_iregistry";
import {ABAPObject} from "../_abap_object";
import {AbstractObject} from "../_abstract_object";
 
 
export class RenamerHelper {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public renameReferences(id: Identifier | undefined, oldName: string, newName: string): TextDocumentEdit[] {
    if (id === undefined) {
      throw new Error("renameReferences, no main identifier found");
    }
    let refs: Identifier[] = [];
    for (const o of this.reg.getObjects()) {
      if (o instanceof ABAPObject) {
        if (this.reg.isDependency(o)) {
          continue; // do not search in dependencies
        }
        refs = refs.concat(this.findReferences(new SyntaxLogic(this.reg, o).run().spaghetti.getTop(), id));
      }
    }
 
    // start with the last reference in the file first, if there are multiple refs per line
    return this.replaceRefs(refs, oldName, newName).reverse();
  }
 
  public buildXMLFileEdits(clas: AbstractObject, xmlTag: string, oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
    const xml = clas.getXMLFile();
 
    if (xml === undefined) {
      return [];
    }
 
    const tag = xmlTag.toUpperCase();
    const search = "<" + tag + ">" + oldName.toUpperCase() + "</" + tag + ">";
    const rows = xml.getRawRows();
    for (let i = 0; i < rows.length; i++) {
      const index = rows[i].indexOf(search);
      if (index >= 0) {
        const range = Range.create(i, index + 9, i, index + oldName.length + 9);
        changes.push(
          TextDocumentEdit.create({uri: xml.getFilename(), version: 1}, [TextEdit.replace(range, newName.toUpperCase())]));
      }
    }
 
    return changes;
  }
 
  public renameFiles(obj: ABAPObject, oldName: string, name: string): RenameFile[] {
    const list: RenameFile[] = [];
 
    const newName = name.toLowerCase().replace(/\//g, "#");
 
    for (const f of obj.getFiles()) {
// todo, this is not completely correct, ie. if the URI contains the same directory name as the object name
      const newFilename = f.getFilename().replace(oldName.toLowerCase(), newName.toLowerCase());
      list.push(RenameFile.create(f.getFilename(), newFilename));
    }
 
    return list;
  }
 
////////////////////////
 
  private replaceRefs(refs: Identifier[], oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
 
    // "zif_abapgit_auth~is_allowed" is a single token so only replace the first part of a token
    for (const r of refs) {
      const range = Range.create(
        r.getStart().getRow() - 1,
        r.getStart().getCol() - 1,
        r.getStart().getRow() - 1,
        r.getStart().getCol() - 1 + oldName.length);
      changes.push(
        TextDocumentEdit.create({uri: r.getFilename(), version: 1}, [TextEdit.replace(range, newName.toLowerCase())]));
    }
    return changes;
  }
 
  private findReferences(node: ISpaghettiScopeNode, identifier: Identifier): Identifier[] {
    let ret: Identifier[] = [];
 
    if (node.getIdentifier().stype !== ScopeType.BuiltIn) {
      for (const r of node.getData().references) {
        if (r.resolved?.equals(identifier)
            && r.referenceType !== ReferenceType.InferredType
            && !(r.position.getStart() instanceof VirtualPosition)) {
          ret.push(r.position);
        }
      }
    }
 
    for (const c of node.getChildren()) {
      ret = ret.concat(this.findReferences(c, identifier));
    }
 
    return ret;
  }
 
}