iubcli 4.2.16 → 4.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -837,24 +837,37 @@ async function editFile(filePath, target, replacement, signal) {
837
837
  return `Error: "${filePath}" is a directory, not a file.`;
838
838
  }
839
839
  const content = fs2.readFileSync(targetFile, "utf8");
840
- const usesCRLF = content.includes("\r\n");
841
840
  let effectiveTarget = target;
842
841
  let effectiveReplacement = replacement;
843
- let workingContent = content;
844
- let normalizedMode = false;
845
- if (!content.includes(target)) {
842
+ let searchContent = content;
843
+ let matchStart = -1;
844
+ let matchEnd = -1;
845
+ if (content.includes(target)) {
846
+ matchStart = content.indexOf(target);
847
+ matchEnd = matchStart + target.length;
848
+ } else {
846
849
  const contentLF = content.replace(/\r\n/g, "\n");
847
850
  const targetLF = target.replace(/\r\n/g, "\n");
848
- if (contentLF.includes(targetLF)) {
849
- normalizedMode = true;
850
- workingContent = contentLF;
851
- effectiveTarget = targetLF;
852
- effectiveReplacement = replacement.replace(/\r\n/g, "\n");
853
- } else {
851
+ if (!contentLF.includes(targetLF)) {
854
852
  return `Error: Target string not found in "${filePath}". Please use read_file first to verify the exact lines and whitespace before calling edit_file.`;
855
853
  }
854
+ searchContent = contentLF;
855
+ effectiveTarget = targetLF;
856
+ const indexMap = [];
857
+ for (let i = 0; i < content.length; i++) {
858
+ if (content[i] === "\r" && content[i + 1] === "\n") {
859
+ indexMap.push(i);
860
+ i++;
861
+ } else {
862
+ indexMap.push(i);
863
+ }
864
+ }
865
+ indexMap.push(content.length);
866
+ const normalizedMatch = contentLF.indexOf(targetLF);
867
+ matchStart = indexMap[normalizedMatch];
868
+ matchEnd = indexMap[normalizedMatch + targetLF.length];
856
869
  }
857
- const occurrences = workingContent.split(effectiveTarget).length - 1;
870
+ const occurrences = searchContent.split(effectiveTarget).length - 1;
858
871
  if (occurrences > 1) {
859
872
  return `Warning: Target string matches ${occurrences} times in "${filePath}". Please include more surrounding context lines in 'target' so it matches uniquely.`;
860
873
  }
@@ -870,14 +883,15 @@ With: ${effectiveReplacement.split("\n").length} line(s) (${effectiveReplacement
870
883
  console.log(chalk2.cyan("Auto-approve enabled for this session."));
871
884
  }
872
885
  }
873
- if (!normalizedMode && usesCRLF && effectiveReplacement.includes("\n")) {
874
- effectiveReplacement = effectiveReplacement.replace(/\r?\n/g, "\r\n");
875
- }
876
- const idx = workingContent.indexOf(effectiveTarget);
877
- let updatedContent = workingContent.slice(0, idx) + effectiveReplacement + workingContent.slice(idx + effectiveTarget.length);
878
- if (normalizedMode && usesCRLF) {
879
- updatedContent = updatedContent.replace(/\n/g, "\r\n");
886
+ const crlfCount = (content.match(/\r\n/g) || []).length;
887
+ const loneLfCount = (content.match(/\n/g) || []).length - crlfCount;
888
+ const fileEol = crlfCount > loneLfCount ? "\r\n" : "\n";
889
+ const matchedRegion = content.slice(matchStart, matchEnd);
890
+ const regionEol = matchedRegion.includes("\r\n") ? "\r\n" : matchedRegion.includes("\n") ? "\n" : fileEol;
891
+ if (effectiveReplacement.includes("\n")) {
892
+ effectiveReplacement = effectiveReplacement.replace(/\r\n|\n/g, regionEol);
880
893
  }
894
+ const updatedContent = content.slice(0, matchStart) + effectiveReplacement + content.slice(matchEnd);
881
895
  sessionFileHistory.push({
882
896
  filePath: path2.relative(rootDir, targetFile).replace(/\\/g, "/"),
883
897
  isNew: false,