All files / src/objects/rename renamer.ts

92.09% Statements 198/215
79.41% Branches 54/68
100% Functions 10/10
92.09% Lines 198/215

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 2161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 67x 67x 1x 1x 1x 1x 71x 71x 71x   71x     70x 71x 71x 71x 71x 71x 71x 71x 71x 12x 12x 70x 70x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 74x 74x 74x 74x 74x   74x 1x 1x 1x 73x 73x 73x 73x 1x 1x 1x 1x 86x 86x 23x 23x 23x 86x 86x 1x 1x 1x 12x 72x 12x 12x 12x 12x 12x 72x 12x 1x 1x 73x 73x 11x 73x 14x 73x 1x 73x 9x 73x 1x 73x 8x 73x 2x 73x 2x 73x 13x 73x 6x 73x 2x 73x 2x 73x 2x 73x   73x 73x 1x 1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 306x 216x 216x 210x 210x 306x 6x     6x 6x 90x 84x 84x     306x 73x 73x 84x 84x 84x 84x 73x 73x 73x 73x 73x 1x 1x 216x 216x     216x 216x 231x     231x     231x 231x 231x 231x 231x 216x 216x 216x 1x 1x 73x 84x 84x     84x 84x 84x 84x 73x 1x  
import {CreateFile, RenameFile, TextDocumentEdit, WorkspaceEdit} from "vscode-languageserver-types";
import {MemoryFile} from "../../files/memory_file";
import {IRegistry} from "../../_iregistry";
import {RenameDataElement} from "./rename_data_element";
import {RenameDomain} from "./rename_domain";
import {RenameGlobalClass} from "./rename_global_class";
import {RenameGlobalInterface} from "./rename_global_interface";
import {RenameProgram} from "./rename_program";
import {RenameTable} from "./rename_table";
import {RenameTableType} from "./rename_table_type";
import {ObjectRenamer} from "./_object_renamer";
import {RenameMessageClass} from "./rename_message_class";
import {RenameICFService} from "./rename_icf_service";
import {IRenamedURL, RenameURLs} from "./rename_urls";
import {ICFService} from "../icf_service";
import {RenameBSPApplication} from "./rename_bsp_application";
import {RenameHttpService} from "./rename_http_service";
import {RenameInboundService} from "./rename_inbound_service";
import {RenameIAMApp} from "./rename_iam_app";
 
export interface IRenameResult {
  deletedFiles: Set<string>;
  addedFiles: Set<string>;
  updatedFiles: Set<string>;
}
 
export class Renamer {
  private readonly reg: IRegistry;
  private readonly renamedURLs: IRenamedURL[] = [];
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  /** Applies the renaming to the objects and files in the registry,
   *  after renaming the registry is not parsed */
  public rename(type: string, oldName: string, newName: string): IRenameResult {
    const edits = this.buildEdits(type, oldName, newName);
 
    if (edits === undefined) {
      throw new Error("no changes could be determined");
    } else if (edits.changes) {
      throw new Error("only documentChanges expected");
    }
 
    const before = type === "SICF" ? this.findObject(type, oldName) : undefined;
    const oldURL = before instanceof ICFService ? before.getURL() : undefined;
    const oldXML = before?.getXMLFile()?.getFilename();
 
    const result = this.apply(edits);
    this.reg.findIssues(); // hmm, this builds the ddic references
 
    if (oldURL !== undefined && oldXML !== undefined) {
      this.rememberURL(edits, oldURL, oldXML);
    }
    return result;
  }
 
  /** Renames the old URLs of the ICF nodes renamed by this renamer in the pages of the BSP
   *  applications, see RenameURLs */
  public renameURLs(): IRenameResult {
    this.reg.parse();
    const result = this.apply(new RenameURLs(this.reg).buildEdits(this.renamedURLs));
    this.reg.parse();
    return result;
  }
 
  /** Builds edits, but does not apply to registry, used by LSP */
  public buildEdits(type: string, oldName: string, newName: string): WorkspaceEdit | undefined {
    this.reg.parse(); // the registry must be parsed to dermine references
 
    const obj = this.findObject(type, oldName);
 
    if (obj === undefined) {
      throw new Error("rename, object not found");
    } else if (newName.length > obj.getAllowedNaming().maxLength) {
     // todo, also do not allow strange characters and spaces
      throw new Error("Name not allowed");
    }
 
    const r = this.factory(type);
    return r.buildEdits(obj, oldName.toUpperCase(), newName);
  }
 
//////////////////
 
  private findObject(type: string, name: string) {
    let obj = this.reg.getObject(type, name);
    if (obj === undefined && type === "SICF") {
      obj = Array.from(this.reg.getObjects()).find(o =>
        o.getType() === "SICF" && o.getName().toUpperCase().startsWith(name.toUpperCase() + " "));
    }
    return obj;
  }
 
  /** the new URL is the one in the XML file the old XML file was renamed to */
  private rememberURL(edits: WorkspaceEdit, oldURL: string, oldXML: string) {
    for (const dc of edits.documentChanges || []) {
      if (RenameFile.is(dc) && dc.oldUri === oldXML) {
        const newURL = this.reg.getFileByName(dc.newUri)?.getRaw().match(/<URL>([^<]*)<\/URL>/)?.[1];
        if (newURL !== undefined && newURL.toLowerCase() !== oldURL.toLowerCase()) {
          this.renamedURLs.push({oldURL, newURL});
        }
      }
    }
  }
 
  private factory(type: string): ObjectRenamer {
    switch (type) {
      case "CLAS":
        return new RenameGlobalClass(this.reg);
      case "DTEL":
        return new RenameDataElement(this.reg);
      case "DOMA":
        return new RenameDomain(this.reg);
      case "TABL":
        return new RenameTable(this.reg);
      case "TTYP":
        return new RenameTableType(this.reg);
      case "INTF":
        return new RenameGlobalInterface(this.reg);
      case "PROG":
        return new RenameProgram(this.reg);
      case "MSAG":
        return new RenameMessageClass(this.reg);
      case "SICF":
        return new RenameICFService(this.reg);
      case "WAPA":
        return new RenameBSPApplication(this.reg);
      case "HTTP":
        return new RenameHttpService(this.reg);
      case "SCO2":
        return new RenameInboundService(this.reg);
      case "SIA6":
        return new RenameIAMApp(this.reg);
      default:
        throw new Error("Renaming of " + type + " not yet supported");
    }
  }
 
  private apply(edits: WorkspaceEdit): IRenameResult {
    const renames: RenameFile[] = [];
 
    const result: IRenameResult = {
      addedFiles: new Set<string>(),
      deletedFiles: new Set<string>(),
      updatedFiles: new Set<string>(),
    };
 
    // assumption: only renames, creates or text changes, no deletes. A created file starts empty,
    // its content is a text edit after the create
    for (const dc of edits.documentChanges || []) {
      if (TextDocumentEdit.is(dc)) {
        this.applyEdit(dc);
        if (result.addedFiles.has(dc.textDocument.uri) === false) {
          result.updatedFiles.add(dc.textDocument.uri);
        }
      } else if (CreateFile.is(dc)) {
        if (this.reg.getFileByName(dc.uri) !== undefined) {
          throw new Error("apply, file " + dc.uri + " already exists");
        }
        this.reg.addFile(new MemoryFile(dc.uri, ""));
        result.addedFiles.add(dc.uri);
      } else if (RenameFile.is(dc)) {
        renames.push(dc);
      } else {
        throw new Error("unexpected documentChange type");
      }
    }
 
    for (const rename of renames) {
      result.updatedFiles.delete(rename.oldUri);
      result.deletedFiles.add(rename.oldUri);
      result.addedFiles.add(rename.newUri);
    }
 
    this.applyRenames(renames);
 
    return result;
  }
 
  private applyEdit(dc: TextDocumentEdit) {
    const file = this.reg.getFileByName(dc.textDocument.uri);
    if (file === undefined) {
      throw new Error("file " + dc.textDocument.uri + " not found");
    }
    const rows = file.getRawRows();
    for (const e of dc.edits) {
      if ("newText" in e === false) {
        throw new Error("applyEdit, snippet edit not supported");
      }
      if (e.range.start.line !== e.range.end.line) {
        throw new Error("applyEdit, start and end line differ");
      }
      const before = rows[e.range.start.line];
      rows[e.range.start.line] = before.substring(0, e.range.start.character) +
        e.newText +
        before.substring(e.range.end.character);
    }
    const newFile = new MemoryFile(dc.textDocument.uri, rows.join("\n"));
    this.reg.updateFile(newFile);
  }
 
  private applyRenames(renames: RenameFile[]) {
    for (const r of renames) {
      const old = this.reg.getFileByName(r.oldUri);
      if (old === undefined) {
        throw new Error("applyRenames, old not found");
      }
      const newFile = new MemoryFile(r.newUri, old.getRaw());
      this.reg.removeFile(old);
      this.reg.addFile(newFile);
    }
  }
}