All files / src/objects/rename rename_icf_service.ts

80% Statements 48/60
30% Branches 3/10
100% Functions 2/2
80% Lines 48/60

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 611x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {IRegistry} from "../../_iregistry";
import {WorkspaceEdit, TextDocumentEdit, CreateFile, RenameFile, DeleteFile} from "vscode-languageserver-types";
import {ICFService} from "..";
import {ObjectRenamer} from "./_object_renamer";
import {IObject} from "../_iobject";
import {RenamerHelper} from "./renamer_helper";
 
export class RenameICFService 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 ICFService)) {
      throw new Error("RenameICFService, not a ICF Service");
    }
 
    // Preserve GUID suffix from the stored object/file name for the filename rename
    // SICF files follow pattern: servicename.sicf or servicename {GUID}.sicf
    const fileNewName = (() => {
      // Look for pattern: space + GUID (32 hex chars in braces) + optional extension
      const guidPattern = / \{[0-9A-Fa-f]{16,32}\}/;
      const match = oldName.match(guidPattern);
 
      if (match) {
        // Extract everything from the GUID onwards (includes .sicf extension if present)
        const guidIndex = match.index!;
        const suffix = oldName.substring(guidIndex);

        // Only append suffix if newName doesn't already contain it
        return newName.includes(suffix) ? newName : newName + suffix;
      }
 
      // Fallback: preserve any suffix after first space (legacy behavior)
      const space = oldName.indexOf(" ");
      if (space > -1) {
        const suffix = oldName.substring(space);
        return newName.includes(suffix) ? newName : newName + suffix;
      }
 
      return newName;
    })();
 
    const cleanOldName = oldName.match(/^[^ ]+/)?.[0] ?? oldName;
    const cleanNewName = newName.match(/^[^ ]+/)?.[0] ?? newName;
 
    let changes: (TextDocumentEdit | CreateFile | RenameFile | DeleteFile)[] = [];
    const helper = new RenamerHelper(this.reg);
    changes = changes.concat(helper.buildURLFileEdits(obj, cleanOldName, cleanNewName));
    changes = changes.concat(helper.buildXMLFileEdits(obj, "ICF_NAME", cleanOldName, cleanNewName));
    changes = changes.concat(helper.buildXMLFileEdits(obj, "ORIG_NAME", cleanOldName, cleanNewName, true));
    changes = changes.concat(helper.renameFiles(obj, oldName, fileNewName));
    return {
      documentChanges: changes,
    };
  }
 
}