@@ -44,23 +44,28 @@ extension TreeSitterClient {
44
44
cursor. setRange ( range)
45
45
cursor. matchLimit = Constants . treeSitterMatchLimit
46
46
47
- return highlightsFromCursor ( cursor: ResolvingQueryCursor ( cursor: cursor) )
47
+ return highlightsFromCursor ( cursor: ResolvingQueryCursor ( cursor: cursor) , includedRange : range )
48
48
}
49
49
50
50
/// Resolves a query cursor to the highlight ranges it contains.
51
51
/// **Must be called on the main thread**
52
- /// - Parameter cursor: The cursor to resolve.
52
+ /// - Parameters:
53
+ /// - cursor: The cursor to resolve.
54
+ /// - includedRange: The range to include highlights from.
53
55
/// - Returns: Any highlight ranges contained in the cursor.
54
- internal func highlightsFromCursor( cursor: ResolvingQueryCursor ) -> [ HighlightRange ] {
56
+ internal func highlightsFromCursor( cursor: ResolvingQueryCursor , includedRange : NSRange ) -> [ HighlightRange ] {
55
57
cursor. prepare ( with: self . textProvider)
56
58
return cursor
57
59
. flatMap { $0. captures }
58
60
. compactMap {
61
+ // Sometimes `cursor.setRange` just doesnt work :( so we have to do a redundant check for a valid range
62
+ // in the included range
63
+ let intersectionRange = $0. range. intersection ( includedRange) ?? . zero
59
64
// Some languages add an "@spell" capture to indicate a portion of text that should be spellchecked
60
65
// (usually comments). But this causes other captures in the same range to be overriden. So we ignore
61
66
// that specific capture type.
62
- if $0. name != " spell " && $0. name != " injection.content " {
63
- return HighlightRange ( range: $0 . range , capture: CaptureName . fromString ( $0. name ?? " " ) )
67
+ if intersectionRange . length > 0 && $0. name != " spell " && $0. name != " injection.content " {
68
+ return HighlightRange ( range: intersectionRange , capture: CaptureName . fromString ( $0. name ?? " " ) )
64
69
}
65
70
return nil
66
71
}
0 commit comments