Skip to content

Commit f6f2b80

Browse files
Fix Semantic Highlight Out-Of-Range Bug (#2097)
### Description Fixes a bug with semantic highlights where the returned highlights would be outside of the requested range. This fixes that by clamping all returned ranges to the requested range. ### Related Issues * N/A ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots N/A
1 parent 39b1d39 commit f6f2b80

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

CodeEdit/Features/LSP/Features/SemanticTokens/SemanticTokenHighlightProvider.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,20 @@ final class SemanticTokenHighlightProvider<
166166
let rawTokens = storage.getTokensFor(range: lspRange)
167167
let highlights = tokenMap
168168
.decode(tokens: rawTokens, using: textView)
169-
.filter({ $0.capture != nil || !$0.modifiers.isEmpty })
169+
.compactMap { highlightRange -> HighlightRange? in
170+
// Filter out empty ranges
171+
guard highlightRange.capture != nil || !highlightRange.modifiers.isEmpty,
172+
// Clamp the highlight range to the queried range.
173+
let intersection = highlightRange.range.intersection(range),
174+
intersection.isEmpty == false else {
175+
return nil
176+
}
177+
return HighlightRange(
178+
range: intersection,
179+
capture: highlightRange.capture,
180+
modifiers: highlightRange.modifiers
181+
)
182+
}
170183
completion(.success(highlights))
171184
}
172185
}

0 commit comments

Comments
 (0)