Skip to content

Commit 953a9c7

Browse files
KJ7LNWEric Wheeler
andauthored
refactor: align tree-sitter output with other tools (#1859)
Use compact line number format matching read_file/search_files to: - Reduce token usage in AI responses - Simplify output parsing Signed-off-by: Eric Wheeler <[email protected]> Co-authored-by: Eric Wheeler <[email protected]>
1 parent 8ad4fff commit 953a9c7

File tree

2 files changed

+6
-31
lines changed

2 files changed

+6
-31
lines changed

src/services/tree-sitter/__tests__/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ describe("Tree-sitter Service", () => {
122122

123123
expect(result).toContain("class TestClass")
124124
expect(result).toContain("testMethod()")
125-
expect(result).toContain("|----")
126125
})
127126

128127
it("should handle parsing errors gracefully", async () => {

src/services/tree-sitter/index.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function parseSourceCodeDefinitionsForFile(
5555
// Parse the file if we have a parser for it
5656
const definitions = await parseFile(filePath, languageParsers, rooIgnoreController)
5757
if (definitions) {
58-
return `${path.basename(filePath)}\n${definitions}`
58+
return `# ${path.basename(filePath)}\n${definitions}`
5959
}
6060

6161
return undefined
@@ -90,7 +90,7 @@ export async function parseSourceCodeForDefinitionsTopLevel(
9090
for (const file of allowedFilesToParse) {
9191
const definitions = await parseFile(file, languageParsers, rooIgnoreController)
9292
if (definitions) {
93-
result += `${path.relative(dirPath, file).toPosix()}\n${definitions}\n`
93+
result += `# ${path.relative(dirPath, file).toPosix()}\n${definitions}\n`
9494
}
9595
// else {
9696
// filesWithoutDefinitions.push(file)
@@ -177,10 +177,6 @@ async function parseFile(
177177
return null
178178
}
179179

180-
// Add a header with file information and definition count
181-
// Make sure to normalize path separators to forward slashes for consistency
182-
formattedOutput += `// File: ${path.basename(filePath).replace(/\\/g, "/")} (${captures.length} definitions)\n`
183-
184180
// Sort captures by their start position
185181
captures.sort((a, b) => a.node.startPosition.row - b.node.startPosition.row)
186182

@@ -224,15 +220,10 @@ async function parseFile(
224220
const lineKey = `${startLine}-${lines[startLine]}`
225221
const nodeLineKey = `${nodeLine}-${lines[nodeLine]}`
226222

227-
// Add separator if there's a gap between captures
228-
if (lastLine !== -1 && startLine > lastLine + 1) {
229-
formattedOutput += "|| ||----\n"
230-
}
231-
232223
// Always show the class definition line
233224
if (name.includes("class") || (name.includes("name") && name.includes("class"))) {
234225
if (!processedLines.has(lineKey)) {
235-
formattedOutput += `│| ${startLine} - ${endLine} ||${lines[startLine]}\n`
226+
formattedOutput += `${startLine}--${endLine} | ${lines[startLine]}\n`
236227
processedLines.add(lineKey)
237228
}
238229
}
@@ -243,7 +234,7 @@ async function parseFile(
243234
// For function definitions, we need to show the actual line with the function/method name
244235
// This handles the test case mocks where nodeLine is 2 (for "testMethod()")
245236
if (!processedLines.has(nodeLineKey) && lines[nodeLine]) {
246-
formattedOutput += `│| ${nodeLine} - ${node.endPosition.row} ||${lines[nodeLine]}\n`
237+
formattedOutput += `${nodeLine}--${node.endPosition.row} | ${lines[nodeLine]}\n`
247238
processedLines.add(nodeLineKey)
248239
}
249240
}
@@ -256,7 +247,7 @@ async function parseFile(
256247
!name.includes("method")
257248
) {
258249
if (!processedLines.has(lineKey)) {
259-
formattedOutput += `│| ${startLine} - ${endLine} ||${lines[startLine]}\n`
250+
formattedOutput += `${startLine}--${endLine} | ${lines[startLine]}\n`
260251
processedLines.add(lineKey)
261252
}
262253
}
@@ -270,22 +261,7 @@ async function parseFile(
270261
}
271262

272263
if (formattedOutput.length > 0) {
273-
// Create categorized summary of definitions
274-
const classCount = formattedOutput.split("class").length - 1
275-
const functionCount =
276-
formattedOutput.split("function").length - 1 + (formattedOutput.split("method").length - 1)
277-
const variableCount =
278-
formattedOutput.split("const").length -
279-
1 +
280-
formattedOutput.split("let").length -
281-
1 +
282-
formattedOutput.split("var").length -
283-
1
284-
285-
// Add a footer with a summary of definitions
286-
const summary = `// Summary: ${classCount > 0 ? `${classCount} classes, ` : ""}${functionCount > 0 ? `${functionCount} functions/methods, ` : ""}${variableCount > 0 ? `${variableCount} variables` : ""}`
287-
288-
return `|----\n${formattedOutput}|----\n${summary}\n`
264+
return formattedOutput
289265
}
290266
return null
291267
}

0 commit comments

Comments
 (0)