mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 12:21:13 -05:00
Fix highlight algorithm
This commit is contained in:
@@ -443,17 +443,50 @@ export const highlight = (
|
|||||||
obj[pathValue[i]] = value
|
obj[pathValue[i]] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to merge overlapping regions
|
||||||
|
const mergeRegions = (regions: [number, number][]): [number, number][] => {
|
||||||
|
if (regions.length === 0) return regions
|
||||||
|
|
||||||
|
// Sort regions by start index
|
||||||
|
regions.sort((a, b) => a[0] - b[0])
|
||||||
|
|
||||||
|
const merged: [number, number][] = [regions[0]]
|
||||||
|
|
||||||
|
for (let i = 1; i < regions.length; i++) {
|
||||||
|
const last = merged[merged.length - 1]
|
||||||
|
const current = regions[i]
|
||||||
|
|
||||||
|
if (current[0] <= last[1] + 1) {
|
||||||
|
// Overlapping or adjacent regions
|
||||||
|
last[1] = Math.max(last[1], current[1])
|
||||||
|
} else {
|
||||||
|
merged.push(current)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
||||||
const generateHighlightedText = (inputText: string, regions: [number, number][] = []) => {
|
const generateHighlightedText = (inputText: string, regions: [number, number][] = []) => {
|
||||||
|
if (regions.length === 0) {
|
||||||
|
return inputText
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort and merge overlapping regions
|
||||||
|
const mergedRegions = mergeRegions(regions)
|
||||||
|
|
||||||
let content = ""
|
let content = ""
|
||||||
let nextUnhighlightedRegionStartingIndex = 0
|
let nextUnhighlightedRegionStartingIndex = 0
|
||||||
|
|
||||||
regions.forEach((region) => {
|
mergedRegions.forEach((region) => {
|
||||||
const lastRegionNextIndex = region[1] + 1
|
const start = region[0]
|
||||||
|
const end = region[1]
|
||||||
|
const lastRegionNextIndex = end + 1
|
||||||
|
|
||||||
content += [
|
content += [
|
||||||
inputText.substring(nextUnhighlightedRegionStartingIndex, region[0]),
|
inputText.substring(nextUnhighlightedRegionStartingIndex, start),
|
||||||
`<span class="${highlightClassName}">`,
|
`<span class="${highlightClassName}">`,
|
||||||
inputText.substring(region[0], lastRegionNextIndex),
|
inputText.substring(start, lastRegionNextIndex),
|
||||||
"</span>",
|
"</span>",
|
||||||
].join("")
|
].join("")
|
||||||
|
|
||||||
@@ -471,8 +504,10 @@ export const highlight = (
|
|||||||
const highlightedItem = { ...item }
|
const highlightedItem = { ...item }
|
||||||
|
|
||||||
matches?.forEach((match) => {
|
matches?.forEach((match) => {
|
||||||
if (match.key && typeof match.value === "string") {
|
if (match.key && typeof match.value === "string" && match.indices) {
|
||||||
set(highlightedItem, match.key, generateHighlightedText(match.value, [...match.indices]))
|
// Merge overlapping regions before generating highlighted text
|
||||||
|
const mergedIndices = mergeRegions([...match.indices])
|
||||||
|
set(highlightedItem, match.key, generateHighlightedText(match.value, mergedIndices))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user