Skip to content

Commit 7f0f2c8

Browse files
committed
fix: correctly handle Java methods with @OverRide annotations in listCodeDefinitionNames
- Skip class definition captures for Java to avoid duplication - When processing Java methods with annotations, find the actual method declaration line - Add test case to verify @OverRide methods are correctly identified Fixes #7330
1 parent b1f2d39 commit 7f0f2c8

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, it, expect } from "vitest"
2+
import { testParseSourceCodeDefinitions } from "./helpers"
3+
import { javaQuery } from "../queries"
4+
5+
describe("Simple Java @Override test", () => {
6+
it("should show what gets captured for @Override methods", async () => {
7+
const overrideTestContent = `class TestClass {
8+
@Override
9+
public void testMethod() {
10+
// Implementation goes here
11+
}
12+
}`
13+
14+
const testOptions = {
15+
language: "java",
16+
wasmFile: "tree-sitter-java.wasm",
17+
queryString: javaQuery,
18+
extKey: "java",
19+
}
20+
21+
const parseResult = await testParseSourceCodeDefinitions("/test/file.java", overrideTestContent, testOptions)
22+
23+
console.log("\n=== PARSE RESULT ===")
24+
console.log(parseResult)
25+
console.log("====================\n")
26+
27+
if (parseResult) {
28+
const lines = parseResult.split("\n").filter((line) => line.trim())
29+
console.log("\n=== INDIVIDUAL LINES ===")
30+
lines.forEach((line, i) => {
31+
console.log(`Line ${i}: ${line}`)
32+
})
33+
console.log("========================\n")
34+
35+
// Check for the issue
36+
const hasOverrideLine = lines.some((line) => line.includes("@Override") && !line.includes("testMethod"))
37+
38+
if (hasOverrideLine) {
39+
console.log("❌ BUG CONFIRMED: @Override is shown without the method name")
40+
const problematicLines = lines.filter(
41+
(line) => line.includes("@Override") && !line.includes("testMethod"),
42+
)
43+
console.log("Problematic lines:", problematicLines)
44+
} else {
45+
console.log("✅ No issue found - @Override appears with method name")
46+
}
47+
48+
// This test will fail if the bug exists
49+
expect(hasOverrideLine).toBe(false)
50+
}
51+
})
52+
})

src/services/tree-sitter/index.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,42 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
339339
}
340340
// For other component definitions
341341
else if (isNotHtmlElement(startLineContent)) {
342-
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
343-
processedLines.add(lineKey)
342+
// For Java, special handling to avoid showing @Override as a separate line
343+
// when it's part of a method declaration
344+
if (language === "java" && name === "definition.method") {
345+
// Check if the method has an annotation like @Override
346+
const methodText = definitionNode.text
347+
if (methodText.includes("@Override")) {
348+
// Find the actual method declaration line (not the annotation line)
349+
let methodDeclarationLine = startLine
350+
for (let i = startLine; i <= endLine; i++) {
351+
if (
352+
lines[i].includes("public") ||
353+
lines[i].includes("private") ||
354+
lines[i].includes("protected") ||
355+
lines[i].includes("void") ||
356+
lines[i].includes("static")
357+
) {
358+
methodDeclarationLine = i
359+
break
360+
}
361+
}
362+
// Output the method with its proper line range, but show the method declaration line
363+
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[methodDeclarationLine]}\n`
364+
processedLines.add(lineKey)
365+
} else {
366+
// Normal method without annotations
367+
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
368+
processedLines.add(lineKey)
369+
}
370+
} else if (language === "java" && name === "definition.class") {
371+
// For Java classes, skip the entire class definition to avoid duplication
372+
// The class name will be handled by name.definition.class
373+
return
374+
} else {
375+
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
376+
processedLines.add(lineKey)
377+
}
344378

345379
// If this is part of a larger definition, include its non-HTML context
346380
if (node.parent && node.parent.lastChild) {

src/services/tree-sitter/queries/java.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export default `
2929
(record_declaration
3030
name: (identifier) @name.definition.record) @definition.record
3131
32-
; Annotation declarations
32+
; Annotation type declarations (e.g., @interface MyAnnotation)
33+
; Note: This captures annotation type declarations, not annotation usages like @Override
3334
(annotation_type_declaration
3435
name: (identifier) @name.definition.annotation) @definition.annotation
3536

0 commit comments

Comments
 (0)