feat(tools): add ast-grep tools for AST-aware code search and transformation
- Add ast_grep_search for pattern-based code search (25 languages) - Add ast_grep_replace for AST-aware code rewriting with dry-run - Add ast_grep_analyze for in-memory code analysis (NAPI) - Add ast_grep_transform for in-memory code transformation - Add ast_grep_languages to list supported languages
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
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\`\`\``
|
||||
}
|
||||
Reference in New Issue
Block a user