Skip to content

Commit 59a3dc9

Browse files
committed
feat: remove deduplication method define
1 parent cdbba62 commit 59a3dc9

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

src/services/tree-sitter/index.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import { parseMarkdown } from "./markdownParser"
77
import { RooIgnoreController } from "../../core/ignore/RooIgnoreController"
88
import { QueryCapture } from "web-tree-sitter"
99

10+
type OutputItem = {
11+
startLine: number
12+
endLine: number
13+
type: string
14+
startContent: string
15+
}
16+
17+
const METHOD_CAPTURE = ["definition.method", "definition.method.start"]
18+
1019
// Private constant
1120
const DEFAULT_MIN_COMPONENT_LINES_VALUE = 4
1221

@@ -28,7 +37,7 @@ export function setMinComponentLines(value: number): void {
2837
}
2938

3039
function passesMinLines(lineCount: number, capture: QueryCapture, language: string) {
31-
if (["definition.method", "definition.method.start"].includes(capture.name)) {
40+
if (METHOD_CAPTURE.includes(capture.name)) {
3241
// In object-oriented programming languages, method signatures are only one line and should not be ignored.
3342
return false
3443
}
@@ -291,7 +300,7 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
291300
return null
292301
}
293302

294-
let formattedOutput = ""
303+
const outputArr: OutputItem[] = []
295304

296305
// Sort captures by their start position
297306
captures.sort((a, b) => a.node.startPosition.row - b.node.startPosition.row)
@@ -341,13 +350,23 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
341350

342351
// Add component name to output regardless of HTML filtering
343352
if (!processedLines.has(lineKey) && componentName) {
344-
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
353+
outputArr.push({
354+
startLine: startLine,
355+
endLine: endLine,
356+
type: name,
357+
startContent: lines[startLine],
358+
})
345359
processedLines.add(lineKey)
346360
}
347361
}
348362
// For other component definitions
349363
else if (isNotHtmlElement(startLineContent)) {
350-
formattedOutput += `${startLine + 1}--${endLine + 1} | ${lines[startLine]}\n`
364+
outputArr.push({
365+
startLine: startLine,
366+
endLine: endLine,
367+
type: name,
368+
startContent: lines[startLine],
369+
})
351370
processedLines.add(lineKey)
352371

353372
// If this is part of a larger definition, include its non-HTML context
@@ -360,16 +379,42 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
360379
// Add the full range first
361380
const rangeKey = `${node.parent.startPosition.row}-${contextEnd}`
362381
if (!processedLines.has(rangeKey)) {
363-
formattedOutput += `${node.parent.startPosition.row + 1}--${contextEnd + 1} | ${lines[node.parent.startPosition.row]}\n`
382+
outputArr.push({
383+
startLine: node.parent.startPosition.row,
384+
endLine: contextEnd,
385+
type: name,
386+
startContent: lines[node.parent.startPosition.row],
387+
})
364388
processedLines.add(rangeKey)
365389
}
366390
}
367391
}
368392
}
369393
})
370394

371-
if (formattedOutput.length > 0) {
372-
return formattedOutput
395+
// Deduplication and filtering
396+
const dedupedArr: OutputItem[] = []
397+
const seen = new Set<string>()
398+
for (const item of outputArr) {
399+
// Only skip if there is another item with the same startLine and startContent
400+
if (item.startLine === item.endLine && METHOD_CAPTURE.includes(item.type)) {
401+
const isDuplicate = outputArr.some(
402+
(other) =>
403+
other !== item && other.startLine === item.startLine && other.startContent === item.startContent,
404+
)
405+
if (isDuplicate) {
406+
continue
407+
}
408+
}
409+
const key = `${item.startLine}-${item.endLine}-${item.type}-${item.startContent}`
410+
if (!seen.has(key)) {
411+
dedupedArr.push(item)
412+
seen.add(key)
413+
}
414+
}
415+
416+
if (dedupedArr.length > 0) {
417+
return dedupedArr.map((item) => `${item.startLine + 1}--${item.endLine + 1} | ${item.startContent}`).join("\n")
373418
}
374419

375420
return null

0 commit comments

Comments
 (0)