Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ambiguous Highlights #275

Merged
merged 28 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
62c1731
Add Object Scaffolding, RangeStore, VisualRange Tests, Range Store Tests
thecoolwinter Oct 16, 2024
6214f2d
I've Since Realized This Is Incorrect
thecoolwinter Oct 17, 2024
f313e7c
Finalize `StyledRangeStore` and Tests
thecoolwinter Oct 25, 2024
1fa046c
Add Multi-Run Method, Fix Tests, Update Tests
thecoolwinter Oct 26, 2024
0713d2c
Begin `StyledRangeContainer`
thecoolwinter Nov 4, 2024
f09822a
First Attempt At Overlapping Ranges
thecoolwinter Nov 4, 2024
5c1a696
Finish `StyledRangeContainer`
thecoolwinter Nov 4, 2024
f01eeb0
Apply Styles, Update Init Params, Update Capture Representation
thecoolwinter Nov 6, 2024
957b859
Add VisibleRangeProviderTests
thecoolwinter Nov 8, 2024
8d3cf5a
Docs, Docs, Docs
thecoolwinter Nov 8, 2024
3e21e21
Remove .swiftpm folder
thecoolwinter Nov 8, 2024
88b928e
Lint Errors (darn whitespace)
thecoolwinter Nov 8, 2024
fec3ab1
Remove Swift 6 Fixes (will replace in future)
thecoolwinter Nov 8, 2024
f042b38
Fix Warnings
thecoolwinter Nov 8, 2024
9102955
Fix Docs Typo
thecoolwinter Nov 8, 2024
0b00fb9
Merge branch 'main' into feat/multiple-highlighters
thecoolwinter Nov 9, 2024
053a7d4
Highlight Invalidation Fix
thecoolwinter Nov 10, 2024
aaef5f2
Merge branch 'feat/multiple-highlighters' of https://github.com/theco…
thecoolwinter Nov 10, 2024
09e5cac
Rename CaptureModifiers -> CaptureModifier
thecoolwinter Nov 10, 2024
ba1dadb
Fix Compile Error
thecoolwinter Nov 11, 2024
2271437
Update CaptureModifier.swift
thecoolwinter Nov 11, 2024
b7bd8fc
Add `insert` Method To Modifiers Set
thecoolwinter Nov 11, 2024
fc55634
Typo
thecoolwinter Nov 12, 2024
327fc72
Code Style, Add Docs, Clean Tests
thecoolwinter Nov 12, 2024
72c15c9
Merge branch 'feat/multiple-highlighters' of https://github.com/theco…
thecoolwinter Nov 12, 2024
53605e4
Slight Doc Rewrite
thecoolwinter Nov 12, 2024
7db1e66
Filter Ambiguous TreeSitter Highlights
thecoolwinter Nov 17, 2024
aeead60
Merge branch 'main' into fix/ambiguous-highlights
thecoolwinter Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/CodeEditSourceEditor/Highlighting/HighlightRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// This struct represents a range to highlight, as well as the capture name for syntax coloring.
public struct HighlightRange: Sendable {
public struct HighlightRange: Hashable, Sendable {
public let range: NSRange
public let capture: CaptureName?
public let modifiers: CaptureModifierSet
Expand All @@ -19,3 +19,13 @@ public struct HighlightRange: Sendable {
self.modifiers = modifiers
}
}

extension HighlightRange: CustomDebugStringConvertible {
public var debugDescription: String {
if capture == nil && modifiers.isEmpty {
"\(range) (empty)"
} else {
"\(range) (\(capture?.stringValue ?? "No Capture")) \(modifiers.values.map({ $0.stringValue }))"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,35 @@ extension TreeSitterClient {
cursor: QueryCursor,
includedRange: NSRange
) -> [HighlightRange] {
guard let readCallback else { return [] }
var ranges: [NSRange: Int] = [:]
return cursor
.resolve(with: .init(textProvider: readCallback)) // Resolve our cursor against the query
.flatMap { $0.captures }
.compactMap {
// Sometimes `cursor.setRange` just doesn't work :( so we have to do a redundant check for a valid range
// in the included range
let intersectionRange = $0.range.intersection(includedRange) ?? .zero
// Check that the capture name is one CESE can parse. If not, ignore it completely.
if intersectionRange.length > 0, let captureName = CaptureName.fromString($0.name) {
return HighlightRange(range: intersectionRange, capture: captureName)
.reversed() // SwiftTreeSitter returns captures in the reverse order of what we need to filter with.
.compactMap { capture in
let range = capture.range
let index = capture.index

// Lower indexed captures are favored over higher, this is why we reverse it above
if let existingLevel = ranges[range], existingLevel <= index {
return nil
}

guard let captureName = CaptureName.fromString(capture.name) else {
return nil
}
return nil

// Update the filter level to the current index since it's lower and a 'valid' capture
ranges[range] = index

// Validate range and capture name
let intersectionRange = range.intersection(includedRange) ?? .zero
guard intersectionRange.length > 0 else {
return nil
}

return HighlightRange(range: intersectionRange, capture: captureName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public final class TreeSitterClient: HighlightProviding {
completion: @escaping @MainActor (Result<[HighlightRange], Error>) -> Void
) {
let operation = { [weak self] in
return self?.queryHighlightsForRange(range: range) ?? []
return (self?.queryHighlightsForRange(range: range) ?? []).sorted { $0.range.location < $1.range.location }
}

let longQuery = range.length > Constants.maxSyncQueryLength
Expand Down
Loading