All files / src/objects/rename renamer_helper.ts

90.62% Statements 174/192
86.36% Branches 38/44
100% Functions 10/10
90.62% Lines 174/192

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 13x     13x 13x 21x 20x     20x 20x 21x 13x 13x 13x 13x 1x 1x 10x 10x 10x 10x 13x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 10x 10x 1x 1x 17x 17x 17x 17x 17x 15x 13x 13x 13x 2x 15x     2x 2x 2x 2x 17x 17x 1x 1x 1x 1x 1x 1x                     1x 1x 1x 1x 10x 10x 10x 10x 13x 12x 12x 1x 13x     1x 1x 1x 10x 10x 1x 1x 27x 27x 27x 27x 8x 8x 19x 19x 19x 19x 19x 27x 446x 446x 22x 22x 22x 22x 446x 19x 19x 19x 1x 1x 24x 24x 24x 24x 24x 29x 29x 29x 29x 24x 24x 24x 1x 1x 1x 1x 13x 13x 13x 13x 11x 11x 11x 11x 11x 11x 11x 11x 13x 13x 1x 1x 82x 82x 82x 62x 24x 24x 24x 11x 11x 24x 62x 82x 82x 62x 62x 82x 82x 82x 1x 1x
import {Range, RenameFile, TextDocumentEdit, TextEdit} from "vscode-languageserver-types";
import {Table} from "../table";
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";
import {IObject} from "../_iobject";
import {DataElement} from "../data_element";
 
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 renameDDICCodeReferences(obj: IObject, oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
    const used = this.reg.getDDICReferences().listWhereUsed(obj);
 
    for (const u of used) {
      if (u.token === undefined || u.filename === undefined) {
        continue;
      }
      const range = Range.create(
        u.token.getStart().getRow() - 1,
        u.token.getStart().getCol() - 1,
        u.token.getStart().getRow() - 1,
        u.token.getStart().getCol() - 1 + oldName.length);
      changes.push(
        TextDocumentEdit.create({uri: u.filename, version: 1}, [TextEdit.replace(range, newName.toLowerCase())]));
    }
    return changes;
  }
 
  public renameDDICTABLReferences(obj: IObject, oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
    const used = this.reg.getDDICReferences().listWhereUsed(obj);
    const handled: {[name: string]: boolean} = {};
 
    for (const u of used) {
      if (u.type !== "TABL" || handled[u.name.toUpperCase()] === true) {
        // a TABL might reference the object multiple times, but they are all fixes in one call to buildXMLFileEdits
        continue;
      }
      const tabl = this.reg.getObject(u.type, u.name) as Table | undefined;
      if (tabl === undefined) {
        continue;
      }
 
      changes.push(...this.buildXMLFileEdits(tabl, "ROLLNAME", oldName, newName));
      handled[u.name.toUpperCase()] = true;
    }
    return changes;
  }
 
  public renameDDICDTELReferences(obj: IObject, oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
    const used = this.reg.getDDICReferences().listWhereUsed(obj);
 
    for (const u of used) {
      if (u.type !== "DTEL") {
        continue;
      }
      const tabl = this.reg.getObject(u.type, u.name) as DataElement | undefined;
      if (tabl === undefined) {
        continue;
      }

      changes.push(...this.buildXMLFileEdits(tabl, "DOMNAME", oldName, newName));
    }
    return changes;
  }
 
  public renameDDICTTYPReferences(obj: IObject, oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
    const used = this.reg.getDDICReferences().listWhereUsed(obj);
 
    for (const u of used) {
      if (u.type !== "TTYP") {
        continue;
      }
      const tabl = this.reg.getObject(u.type, u.name) as DataElement | undefined;
      if (tabl === undefined) {
        continue;
      }
 
      changes.push(...this.buildXMLFileEdits(tabl, "ROWTYPE", oldName, newName));
    }
    return changes;
  }
 
  public buildXMLFileEdits(object: AbstractObject, xmlTag: string, oldName: string, newName: string): TextDocumentEdit[] {
    const changes: TextDocumentEdit[] = [];
    const xml = object.getXMLFile();
 
    if (xml === undefined) {
      return [];
    }
 
    const tag = xmlTag.toUpperCase();
    const search = "<" + tag + ">" + oldName.toUpperCase() + "</" + tag + ">";
    const length = tag.length + 2;
    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 + length, i, index + oldName.length + length);
        changes.push(
          TextDocumentEdit.create({uri: xml.getFilename(), version: 1}, [TextEdit.replace(range, newName.toUpperCase())]));
      }
    }
 
    return changes;
  }
 
  public renameFiles(obj: IObject, 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;
  }
 
}