docs-coderef 0.2.0 → 0.4.0

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/cli/fix.mjs CHANGED
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env tsx
2
2
  import {
3
+ COLOR_SCHEMES,
3
4
  displayCodeDiff,
4
- displayLineRangeDiff
5
- } from "../chunk-NTTV47AI.mjs";
5
+ msg
6
+ } from "../chunk-U3LM2QL7.mjs";
6
7
  import {
7
8
  __require,
8
9
  expandMatchToScope,
@@ -177,7 +178,22 @@ ${message}`);
177
178
  if (selection >= 1 && selection <= options.length) {
178
179
  return selection - 1;
179
180
  }
180
- console.log("\u274C Invalid selection. Please try again.");
181
+ console.log(msg.error("Invalid selection. Please try again."));
182
+ }
183
+ }
184
+ function displayColoredCodeBlock(preview) {
185
+ const codeBlockMatch = /```(\w*)\n([\s\S]*?)\n```/.exec(preview);
186
+ if (codeBlockMatch) {
187
+ const language = codeBlockMatch[1] || "";
188
+ const code = codeBlockMatch[2];
189
+ console.log(COLOR_SCHEMES.dim(`\`\`\`${language}`));
190
+ const lines = code.split("\n");
191
+ lines.forEach((line) => {
192
+ console.log(COLOR_SCHEMES.success(`+ ${line}`));
193
+ });
194
+ console.log(COLOR_SCHEMES.dim("```"));
195
+ } else {
196
+ console.log(preview);
181
197
  }
182
198
  }
183
199
  function displayFixPreview(action, projectRoot) {
@@ -185,51 +201,18 @@ function displayFixPreview(action, projectRoot) {
185
201
  Changes: ${action.description}`);
186
202
  const { error } = action;
187
203
  const absolutePath = path.resolve(projectRoot, error.ref.refPath);
188
- switch (error.type) {
189
- case "CODE_LOCATION_MISMATCH": {
190
- if (error.ref.codeBlock && action.newStartLine && action.newEndLine) {
191
- const actualCode = extractLinesFromFile(
192
- absolutePath,
193
- action.newStartLine,
194
- action.newEndLine
195
- );
196
- const diff = displayLineRangeDiff(
197
- actualCode,
198
- {
199
- start: error.ref.startLine,
200
- end: error.ref.endLine
201
- },
202
- {
203
- start: action.newStartLine,
204
- end: action.newEndLine
205
- }
206
- );
207
- console.log(diff);
208
- } else {
209
- console.log(action.preview);
210
- }
211
- break;
212
- }
213
- case "CODE_CONTENT_MISMATCH": {
214
- if (error.expectedCode && action.newCodeBlock) {
215
- const diff = displayCodeDiff(error.expectedCode, action.newCodeBlock);
216
- console.log(diff);
217
- } else {
218
- console.log(action.preview);
219
- }
220
- break;
221
- }
204
+ switch (action.type) {
222
205
  case "INSERT_CODE_BLOCK": {
223
206
  if (action.newCodeBlock) {
224
- console.log("\x1B[32m+ Insert code block:\x1B[0m");
225
- console.log("\x1B[2m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\x1B[0m");
207
+ console.log(COLOR_SCHEMES.success("+ Insert code block:"));
208
+ console.log(COLOR_SCHEMES.dim("\u2501".repeat(64)));
226
209
  const lines = action.newCodeBlock.split("\n");
227
210
  lines.forEach((line) => {
228
- console.log(`\x1B[32m+ ${line}\x1B[0m`);
211
+ console.log(COLOR_SCHEMES.success(`+ ${line}`));
229
212
  });
230
- console.log("\x1B[2m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\x1B[0m");
213
+ console.log(COLOR_SCHEMES.dim("\u2501".repeat(64)));
231
214
  } else {
232
- console.log(action.preview);
215
+ displayColoredCodeBlock(action.preview);
233
216
  }
234
217
  break;
235
218
  }
@@ -244,13 +227,23 @@ Changes: ${action.description}`);
244
227
  }
245
228
  case "UPDATE_LINE_NUMBERS":
246
229
  case "UPDATE_END_LINE": {
230
+ if (error.type === "CODE_LOCATION_MISMATCH" && !error.ref.codeBlock) {
231
+ console.log(action.preview);
232
+ break;
233
+ }
247
234
  const oldComment = error.ref.fullMatch;
248
235
  const newComment = `<!-- CODE_REF: ${error.ref.refPath}:${action.newStartLine}-${action.newEndLine} -->`;
249
- console.log("\x1B[2m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\x1B[0m");
250
- console.log(`\x1B[31m- ${oldComment}\x1B[0m`);
251
- console.log(`\x1B[32m+ ${newComment}\x1B[0m`);
252
- console.log("\x1B[2m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\x1B[0m");
253
- if (action.newCodeBlock && fs2.existsSync(absolutePath)) {
236
+ console.log(COLOR_SCHEMES.dim("\u2501".repeat(64)));
237
+ console.log(COLOR_SCHEMES.error(`- ${oldComment}`));
238
+ console.log(COLOR_SCHEMES.success(`+ ${newComment}`));
239
+ console.log(COLOR_SCHEMES.dim("\u2501".repeat(64)));
240
+ if (error.type === "CODE_CONTENT_MISMATCH") {
241
+ console.log(
242
+ COLOR_SCHEMES.warning(
243
+ "\n\u26A0\uFE0F Note: Code block will remain unchanged. Please manually adjust if needed."
244
+ )
245
+ );
246
+ } else if (action.newCodeBlock && fs2.existsSync(absolutePath)) {
254
247
  console.log("\nCode block content:");
255
248
  const lines = action.newCodeBlock.split("\n").slice(0, 10);
256
249
  lines.forEach((line) => {
@@ -264,7 +257,7 @@ Changes: ${action.description}`);
264
257
  }
265
258
  default: {
266
259
  if (action.preview) {
267
- console.log(action.preview);
260
+ displayColoredCodeBlock(action.preview);
268
261
  }
269
262
  break;
270
263
  }
@@ -453,7 +446,9 @@ ${symbolCode}
453
446
  const expandedCode = extractLinesFromFile(absolutePath, expanded.start, expanded.end);
454
447
  const scopeInfo = expanded.scopeType ? ` (${expanded.scopeType})` : "";
455
448
  console.log(
456
- ` \u2139\uFE0F Expanded ${ref.startLine}-${ref.endLine} to ${expanded.start}-${expanded.end} using AST analysis${scopeInfo}`
449
+ msg.debug(
450
+ ` Expanded ${ref.startLine}-${ref.endLine} to ${expanded.start}-${expanded.end} using AST analysis${scopeInfo}`
451
+ )
457
452
  );
458
453
  const oldComment = ref.fullMatch;
459
454
  const newComment = `<!-- CODE_REF: ${ref.refPath}:${expanded.start}-${expanded.end} -->`;
@@ -657,7 +652,7 @@ async function handleMultipleMatches(error, rl, config) {
657
652
  const confidenceInfo = match.confidence === "high" && match.scopeType ? ` (${match.scopeType})` : match.confidence === "low" ? " (low confidence)" : "";
658
653
  console.log(
659
654
  `
660
- \u2713 Detected single match in ${ref.refPath}: lines ${match.start}-${match.end}${confidenceInfo}`
655
+ ${msg.success(`Detected single match in ${ref.refPath}: lines ${match.start}-${match.end}${confidenceInfo}`)}`
661
656
  );
662
657
  return {
663
658
  type: "UPDATE_LINE_NUMBERS",
@@ -679,7 +674,7 @@ async function handleMultipleMatches(error, rl, config) {
679
674
  const scopeInfo = bestMatch.scopeType ? ` (${bestMatch.scopeType})` : "";
680
675
  console.log(
681
676
  `
682
- \u2713 Auto-selected most appropriate match in ${ref.refPath}: lines ${bestMatch.start}-${bestMatch.end}${scopeInfo}`
677
+ ${msg.success(`Auto-selected most appropriate match in ${ref.refPath}: lines ${bestMatch.start}-${bestMatch.end}${scopeInfo}`)}`
683
678
  );
684
679
  return {
685
680
  type: "UPDATE_LINE_NUMBERS",
@@ -692,7 +687,7 @@ async function handleMultipleMatches(error, rl, config) {
692
687
  };
693
688
  }
694
689
  console.log(`
695
- \u26A0\uFE0F Code found in ${matches.length} locations in ${ref.refPath}:`);
690
+ ${msg.warning(`Code found in ${matches.length} locations in ${ref.refPath}:`)}`);
696
691
  const options = sortedMatches.map((m) => {
697
692
  const confidenceLabel = m.confidence === "high" ? "high" : m.confidence === "medium" ? "medium" : "low";
698
693
  const scopeInfo = m.scopeType && m.scopeType !== "unknown" ? `, ${m.scopeType}` : "";
@@ -701,10 +696,10 @@ async function handleMultipleMatches(error, rl, config) {
701
696
  const selection = await askSelectOption(rl, options, "Which position should be used?");
702
697
  const selectedMatch = sortedMatches[selection];
703
698
  if (selectedMatch.confidence === "low") {
704
- console.warn("\u26A0\uFE0F Scope detection confidence is low, please verify the result.");
699
+ console.warn(msg.warning("Scope detection confidence is low, please verify the result."));
705
700
  }
706
701
  if (selectedMatch.expansionType === "none") {
707
- console.warn("\u26A0\uFE0F Structural analysis failed, using simple string matching.");
702
+ console.warn(msg.warning("Structural analysis failed, using simple string matching."));
708
703
  }
709
704
  return {
710
705
  type: "UPDATE_LINE_NUMBERS",
@@ -720,16 +715,16 @@ async function handleMultipleMatches(error, rl, config) {
720
715
  // src/utils/styles.ts
721
716
  import chalk from "chalk";
722
717
  var colors = {
723
- // Existing colors from diff-display.ts
724
- success: chalk.green,
725
- error: chalk.red,
726
- dim: chalk.dim,
727
- // New colors for fix options
728
- primary: chalk.cyan.bold,
718
+ // Core colors from message-formatter
719
+ success: COLOR_SCHEMES.success,
720
+ error: COLOR_SCHEMES.error,
721
+ dim: COLOR_SCHEMES.dim,
722
+ // Accent colors for fix options
723
+ primary: COLOR_SCHEMES.highlight,
729
724
  // Option numbers
730
- muted: chalk.gray,
725
+ muted: COLOR_SCHEMES.debug,
731
726
  // Supplementary info
732
- code: chalk.yellow
727
+ code: COLOR_SCHEMES.code
733
728
  // Code blocks
734
729
  };
735
730
  var box = {
@@ -785,19 +780,27 @@ function extractLineInfo(preview) {
785
780
  return null;
786
781
  }
787
782
  function formatPreview(preview) {
788
- const lines = preview.split("\n");
783
+ const processedPreview = preview.replace(/\n(→\s*)/g, " $1");
784
+ const lines = processedPreview.split("\n");
789
785
  const output = [];
786
+ const processLine = (line) => {
787
+ if (line.includes("\u2192") && line.includes("<!-- CODE_REF:")) {
788
+ const parts = line.split("\u2192");
789
+ if (parts.length === 2) {
790
+ output.push(colors.error(`- ${parts[0].trim()}`));
791
+ output.push(colors.success(`+ ${parts[1].trim()}`));
792
+ return;
793
+ }
794
+ }
795
+ output.push(formatCodeLine(line));
796
+ };
790
797
  if (lines.length > MAX_PREVIEW_LINES) {
791
798
  const truncatedLines = lines.slice(0, TRUNCATE_SHOW_LINES);
792
799
  const remainingCount = lines.length - TRUNCATE_SHOW_LINES;
793
- truncatedLines.forEach((line) => {
794
- output.push(formatCodeLine(line));
795
- });
800
+ truncatedLines.forEach(processLine);
796
801
  output.push(colors.muted(`... (${remainingCount} more lines)`));
797
802
  } else {
798
- lines.forEach((line) => {
799
- output.push(formatCodeLine(line));
800
- });
803
+ lines.forEach(processLine);
801
804
  }
802
805
  return output;
803
806
  }
@@ -805,6 +808,9 @@ function formatCodeLine(line) {
805
808
  if (line.trim().startsWith("```")) {
806
809
  return colors.dim(line);
807
810
  }
811
+ if (line.includes("<!-- CODE_REF:")) {
812
+ return line.replace(/<!--/g, colors.dim("<!--")).replace(/-->/g, colors.dim("-->")).replace(/(CODE_REF:)/g, chalk.cyan.bold("$1"));
813
+ }
808
814
  const keywords = [
809
815
  "function",
810
816
  "const",
@@ -874,26 +880,31 @@ async function main() {
874
880
  backup: !options.noBackup,
875
881
  verbose: options.verbose
876
882
  });
877
- console.log("\u{1F527} Starting CODE_REF error fixes...\n");
883
+ msg.setVerbose(options.verbose);
884
+ console.log(`${msg.startFix("Starting CODE_REF error fixes...")}
885
+ `);
878
886
  if (options.dryRun) {
879
- console.log("\u26A0\uFE0F DRY RUN mode: No actual changes will be made\n");
887
+ console.log(`${msg.warning("DRY RUN mode: No actual changes will be made")}
888
+ `);
880
889
  }
881
890
  const errorGroups = collectErrors(config);
882
891
  if (errorGroups.length === 0) {
883
- console.log("\u2705 No fixable errors found");
892
+ console.log(msg.success("No fixable errors found"));
884
893
  process.exit(0);
885
894
  }
886
895
  const totalErrors = errorGroups.reduce((sum, g) => sum + g.errors.length, 0);
887
- console.log(`\u{1F4CA} Detected ${totalErrors} fixable error(s) in ${errorGroups.length} file(s)
888
- `);
896
+ console.log(
897
+ `${msg.info(`Detected ${totalErrors} fixable error(s) in ${errorGroups.length} file(s)`)}
898
+ `
899
+ );
889
900
  const rl = createPromptInterface();
890
901
  const fixResults = [];
891
902
  const backupFiles = /* @__PURE__ */ new Set();
892
903
  try {
893
904
  for (const group of errorGroups) {
894
905
  console.log(`
895
- \u{1F4C4} ${path3.relative(config.projectRoot, group.docFile)}`);
896
- console.log(` ${group.errors.length} error(s)
906
+ ${msg.file(path3.relative(config.projectRoot, group.docFile))}`);
907
+ console.log(`${msg.context(`${group.errors.length} error(s)`)}
897
908
  `);
898
909
  const sortedErrors = group.errors.sort((a, b) => {
899
910
  const lineA = a.ref.docLineNumber ?? Infinity;
@@ -902,21 +913,22 @@ async function main() {
902
913
  });
903
914
  let _lineOffset = 0;
904
915
  for (const error of sortedErrors) {
916
+ const location = `${path3.relative(config.projectRoot, error.ref.docFile)}${error.ref.docLineNumber ? `:${error.ref.docLineNumber}` : ""}`;
905
917
  console.log(`
906
- \u274C ${error.type}: ${error.message}`);
907
- console.log(
908
- ` Reference: ${path3.relative(config.projectRoot, error.ref.docFile)}${error.ref.docLineNumber ? `:${error.ref.docLineNumber}` : ""}`
909
- );
918
+ ${msg.errorDetail(error.type, error.message, location)}`);
910
919
  let action;
911
920
  if (error.type === "CODE_LOCATION_MISMATCH") {
912
921
  action = await handleMultipleMatches(error, rl, config);
913
922
  } else {
914
923
  const fixActionResult = await createFixAction(error, config, rl);
915
924
  if (Array.isArray(fixActionResult)) {
916
- console.log("\n\u{1F6E0}\uFE0F Please select a fix method:\n");
925
+ console.log(`
926
+ ${msg.info("Please select a fix method:")}
927
+ `);
917
928
  console.log(formatFixOptions(fixActionResult));
918
929
  if (options.auto) {
919
- console.log(" \u2139\uFE0F Auto-selecting option 1 in auto mode\n");
930
+ console.log(`${msg.context("Auto-selecting option 1 in auto mode")}
931
+ `);
920
932
  action = fixActionResult[0];
921
933
  } else {
922
934
  const selection = await new Promise((resolve3) => {
@@ -925,13 +937,13 @@ async function main() {
925
937
  if (num >= 1 && num <= fixActionResult.length) {
926
938
  resolve3(num - 1);
927
939
  } else {
928
- console.log(" \u26A0\uFE0F Invalid selection. Skipping.");
940
+ console.log(msg.context("Invalid selection. Skipping."));
929
941
  resolve3(-1);
930
942
  }
931
943
  });
932
944
  });
933
945
  if (selection === -1) {
934
- console.log(" \u23ED\uFE0F Skipped");
946
+ console.log(msg.context("Skipped"));
935
947
  continue;
936
948
  }
937
949
  action = fixActionResult[selection];
@@ -941,7 +953,7 @@ async function main() {
941
953
  }
942
954
  }
943
955
  if (!action) {
944
- console.log(" \u26A0\uFE0F This error cannot be fixed");
956
+ console.log(msg.context("This error cannot be fixed"));
945
957
  continue;
946
958
  }
947
959
  if (!Array.isArray(action)) {
@@ -952,11 +964,11 @@ async function main() {
952
964
  shouldFix = await askYesNo(rl, "\nApply this fix?", false);
953
965
  }
954
966
  if (!shouldFix) {
955
- console.log(" \u23ED\uFE0F Skipped");
967
+ console.log(msg.context("Skipped"));
956
968
  continue;
957
969
  }
958
970
  if (options.dryRun) {
959
- console.log(" \u2705 [DRY RUN] Simulated fix");
971
+ console.log(msg.context("[DRY RUN] Simulated fix"));
960
972
  fixResults.push({ success: true, action });
961
973
  continue;
962
974
  }
@@ -964,19 +976,19 @@ async function main() {
964
976
  if (!options.noBackup && !backupFiles.has(group.docFile)) {
965
977
  backupPath = createBackup(group.docFile);
966
978
  backupFiles.add(group.docFile);
967
- console.log(` \u{1F4BE} Backup created: ${path3.basename(backupPath)}`);
979
+ console.log(msg.context(`Backup created: ${path3.basename(backupPath)}`));
968
980
  }
969
981
  try {
970
982
  const lineDelta = applyFix(action);
971
983
  _lineOffset += lineDelta;
972
- if (lineDelta !== 0) {
973
- console.log(` \u{1F4CA} Line delta: ${lineDelta > 0 ? "+" : ""}${lineDelta}`);
984
+ if (lineDelta !== 0 && options.verbose) {
985
+ console.log(msg.debug(` Line delta: ${lineDelta > 0 ? "+" : ""}${lineDelta}`));
974
986
  }
975
- console.log(" \u2705 Fix applied");
987
+ console.log(msg.context("Fix applied"));
976
988
  fixResults.push({ success: true, action, backupPath });
977
989
  } catch (err) {
978
990
  const errorMsg = err instanceof Error ? err.message : String(err);
979
- console.log(` \u274C Fix failed: ${errorMsg}`);
991
+ console.log(msg.context(`Fix failed: ${errorMsg}`));
980
992
  fixResults.push({ success: false, action, error: errorMsg, backupPath });
981
993
  }
982
994
  }
@@ -984,26 +996,16 @@ async function main() {
984
996
  } finally {
985
997
  rl.close();
986
998
  }
987
- console.log(`
988
- ${"=".repeat(60)}`);
989
- console.log("\u{1F4CA} Fix Results Summary\n");
990
999
  const successful = fixResults.filter((r) => r.success).length;
991
1000
  const failed = fixResults.filter((r) => !r.success).length;
992
- console.log(`\u2705 Successful: ${successful}`);
993
- console.log(`\u274C Failed: ${failed}`);
994
- if (backupFiles.size > 0 && !options.noBackup) {
995
- console.log(`
996
- \u{1F4BE} Backup files: ${backupFiles.size}`);
997
- for (const file of backupFiles) {
998
- const backupPath = `${file}.backup`;
999
- console.log(` ${path3.relative(config.projectRoot, backupPath)}`);
1000
- }
1001
- }
1001
+ const backupPaths = !options.noBackup && backupFiles.size > 0 ? Array.from(backupFiles).map((file) => path3.relative(config.projectRoot, `${file}.backup`)) : [];
1002
+ console.log(msg.summary(successful, failed, backupPaths));
1002
1003
  process.exit(failed > 0 ? 1 : 0);
1003
1004
  }
1004
1005
  if (__require.main === module) {
1005
1006
  main().catch((error) => {
1006
- console.error("\n\u274C An error occurred:", error);
1007
+ console.error(`
1008
+ ${msg.error(`An error occurred: ${error}`)}`);
1007
1009
  process.exit(1);
1008
1010
  });
1009
1011
  }