73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
|
|
import type { CliMatch, AnalyzeResult } from "./types"
|
||
|
|
|
||
|
|
export function formatSearchResult(matches: CliMatch[]): string {
|
||
|
|
if (matches.length === 0) {
|
||
|
|
return "No matches found"
|
||
|
|
}
|
||
|
|
|
||
|
|
const lines: string[] = [`Found ${matches.length} match(es):\n`]
|
||
|
|
|
||
|
|
for (const match of matches) {
|
||
|
|
const loc = `${match.file}:${match.range.start.line + 1}:${match.range.start.column + 1}`
|
||
|
|
lines.push(`${loc}`)
|
||
|
|
lines.push(` ${match.lines.trim()}`)
|
||
|
|
lines.push("")
|
||
|
|
}
|
||
|
|
|
||
|
|
return lines.join("\n")
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatReplaceResult(matches: CliMatch[], isDryRun: boolean): string {
|
||
|
|
if (matches.length === 0) {
|
||
|
|
return "No matches found to replace"
|
||
|
|
}
|
||
|
|
|
||
|
|
const prefix = isDryRun ? "[DRY RUN] " : ""
|
||
|
|
const lines: string[] = [`${prefix}${matches.length} replacement(s):\n`]
|
||
|
|
|
||
|
|
for (const match of matches) {
|
||
|
|
const loc = `${match.file}:${match.range.start.line + 1}:${match.range.start.column + 1}`
|
||
|
|
lines.push(`${loc}`)
|
||
|
|
lines.push(` ${match.text}`)
|
||
|
|
lines.push("")
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isDryRun) {
|
||
|
|
lines.push("Use dryRun=false to apply changes")
|
||
|
|
}
|
||
|
|
|
||
|
|
return lines.join("\n")
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatAnalyzeResult(results: AnalyzeResult[], extractedMetaVars: boolean): string {
|
||
|
|
if (results.length === 0) {
|
||
|
|
return "No matches found"
|
||
|
|
}
|
||
|
|
|
||
|
|
const lines: string[] = [`Found ${results.length} match(es):\n`]
|
||
|
|
|
||
|
|
for (const result of results) {
|
||
|
|
const loc = `L${result.range.start.line + 1}:${result.range.start.column + 1}`
|
||
|
|
lines.push(`[${loc}] (${result.kind})`)
|
||
|
|
lines.push(` ${result.text}`)
|
||
|
|
|
||
|
|
if (extractedMetaVars && result.metaVariables.length > 0) {
|
||
|
|
lines.push(" Meta-variables:")
|
||
|
|
for (const mv of result.metaVariables) {
|
||
|
|
lines.push(` $${mv.name} = "${mv.text}" (${mv.kind})`)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lines.push("")
|
||
|
|
}
|
||
|
|
|
||
|
|
return lines.join("\n")
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatTransformResult(original: string, transformed: string, editCount: number): string {
|
||
|
|
if (editCount === 0) {
|
||
|
|
return "No matches found to transform"
|
||
|
|
}
|
||
|
|
|
||
|
|
return `Transformed (${editCount} edit(s)):\n\`\`\`\n${transformed}\n\`\`\``
|
||
|
|
}
|