Skip to content

Commit c0dbbaf

Browse files
committed
feat: enhance Java method declaration parsing to include type information
1 parent 75670f3 commit c0dbbaf

File tree

2 files changed

+10
-59
lines changed

2 files changed

+10
-59
lines changed

src/services/tree-sitter/index.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import { QueryCapture } from "web-tree-sitter"
1010
// Private constant
1111
const DEFAULT_MIN_COMPONENT_LINES_VALUE = 4
1212

13-
// Language-specific rules
14-
const LANGUAGE_SKIP_RULES: Record<string, string[]> = {
15-
java: ["definition.method"],
16-
// Future languages can be added here
17-
}
18-
1913
// Getter function for MIN_COMPONENT_LINES (for easier testing)
2014
let currentMinComponentLines = DEFAULT_MIN_COMPONENT_LINES_VALUE
2115

@@ -34,8 +28,8 @@ export function setMinComponentLines(value: number): void {
3428
}
3529

3630
function shouldSkipLineCountCheck(lineCount: number, capture: QueryCapture, language: string) {
37-
const skipRules = LANGUAGE_SKIP_RULES[language]
38-
if (skipRules && skipRules.includes(capture.name)) {
31+
if (["definition.method", "definition.method.start"].includes(capture.name)) {
32+
// In object-oriented programming languages, method signatures are only one line and should not be ignored.
3933
return false
4034
}
4135
return lineCount < getMinComponentLines()
@@ -337,14 +331,8 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
337331
return
338332
}
339333

340-
let outputLineIdx = startLine
341-
// For Java method definitions, find the actual method signature line
342-
if (language === "java" && name === "definition.method") {
343-
outputLineIdx = findJavaMethodSignatureLine(lines, startLine, endLine)
344-
}
345-
346334
// Check if this is a valid component definition (not an HTML element)
347-
const startLineContent = lines[outputLineIdx].trim()
335+
const startLineContent = lines[startLine].trim()
348336

349337
// Special handling for component name definitions
350338
if (name.includes("name.definition")) {
@@ -353,13 +341,13 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
353341

354342
// Add component name to output regardless of HTML filtering
355343
if (!processedLines.has(lineKey) && componentName) {
356-
formattedOutput += `${outputLineIdx + 1}--${endLine + 1} | ${lines[outputLineIdx]}\n`
344+
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
357345
processedLines.add(lineKey)
358346
}
359347
}
360348
// For other component definitions
361349
else if (isNotHtmlElement(startLineContent)) {
362-
formattedOutput += `${outputLineIdx + 1}--${endLine + 1} | ${lines[outputLineIdx]}\n`
350+
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
363351
processedLines.add(lineKey)
364352

365353
// If this is part of a larger definition, include its non-HTML context
@@ -433,45 +421,3 @@ async function parseFile(
433421
return null
434422
}
435423
}
436-
437-
/**
438-
* Find the line containing the actual Java method signature
439-
* by looking for lines that match method declaration patterns
440-
*
441-
* @param lines - Array of lines from the file
442-
* @param startLine - Starting line index
443-
* @param endLine - Ending line index
444-
* @returns The line index of the method signature
445-
*/
446-
function findJavaMethodSignatureLine(lines: string[], startLine: number, endLine: number): number {
447-
// Java method signature pattern - avoiding backtracking issues
448-
// Split into separate checks to avoid complex alternations
449-
const accessModifiers = /^\s*(?:public|private|protected)\s+/
450-
const otherModifiers = /^\s*(?:static|final|abstract|synchronized|native|strictfp)\s+/
451-
const methodPattern = /^\s*(?:\w+(?:\[\])*(?:\s*<[^>]+>)?\s+)+\w+\s*\(/
452-
453-
for (let i = startLine; i <= endLine; i++) {
454-
const line = lines[i].trim()
455-
456-
// Skip empty lines and annotation lines
457-
if (line.length === 0 || line.startsWith("@")) {
458-
continue
459-
}
460-
461-
// Check if this looks like a method signature using multiple simple patterns
462-
// This avoids complex alternation that can cause backtracking
463-
if (accessModifiers.test(line) || otherModifiers.test(line) || methodPattern.test(line)) {
464-
return i
465-
}
466-
}
467-
468-
// If no method signature found, return the first non-annotation, non-empty line
469-
for (let i = startLine; i <= endLine; i++) {
470-
const line = lines[i].trim()
471-
if (line.length > 0 && !line.startsWith("@")) {
472-
return i
473-
}
474-
}
475-
476-
return startLine
477-
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export default `
4141
(method_declaration
4242
name: (identifier) @name.definition.method) @definition.method
4343
44+
; Method declarations with type - capture from type start
45+
(method_declaration
46+
type: (_) @definition.method.start
47+
name: (identifier) @name.definition.method)
48+
4449
; Inner class declarations
4550
(class_declaration
4651
(class_body

0 commit comments

Comments
 (0)