Skip to content

Commit

Permalink
better invalidation control with TextTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 25, 2022
1 parent 086fc6e commit 0ab3dd3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
34 changes: 24 additions & 10 deletions Sources/Neon/Highlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import Foundation
import Rearrange
import os.log

public enum TextTarget {
case set(IndexSet)
case range(NSRange)
case all

public func indexSet(with fullSet: IndexSet) -> IndexSet {
let set: IndexSet

switch self {
case .set(let indexSet):
set = indexSet
case .range(let range):
set = IndexSet(integersIn: range)
case .all:
set = fullSet
}

return set
}
}

public class Highlighter {
public var textInterface: TextSystemInterface
Expand All @@ -22,9 +42,11 @@ public class Highlighter {
}

extension Highlighter {
public func invalidate(_ set: IndexSet) {
public func invalidate(_ target: TextTarget = .all) {
dispatchPrecondition(condition: .onQueue(.main))

let set = target.indexSet(with: fullTextSet)

if set.isEmpty {
return
}
Expand All @@ -34,21 +56,13 @@ extension Highlighter {

makeNextTokenRequest()
}

public func invalidate(_ range: NSRange) {
invalidate(IndexSet(integersIn: range))
}

public func invalidate() {
invalidate(fullTextSet)
}
}

extension Highlighter {
public func visibleContentDidChange() {
let set = invalidSet.intersection(visibleSet)

invalidate(set)
invalidate(.set(set))
}

public func didChangeContent(in range: NSRange, delta: Int, limit: Int) {
Expand Down
18 changes: 6 additions & 12 deletions Sources/Neon/HighlighterInvalidationBuffer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public class HighlighterInvalidationBuffer {
public final class HighlighterInvalidationBuffer {
private enum State: Hashable {
case idle
case buffering
Expand All @@ -15,10 +15,12 @@ public class HighlighterInvalidationBuffer {
self.highlighter = highlighter
}

public func invalidate(_ set: IndexSet) {
public func invalidate(_ target: TextTarget = .all) {
let set = target.indexSet(with: highlighter.fullTextSet)

switch state {
case .idle:
highlighter.invalidate(set)
highlighter.invalidate(target)
case .buffering:
self.state = .pendingInvalidation(set)
case .pendingInvalidation(let oldSet):
Expand All @@ -27,14 +29,6 @@ public class HighlighterInvalidationBuffer {
self.state = .pendingInvalidation(newSet)
}
}

public func invalidate(_ range: NSRange) {
invalidate(IndexSet(integersIn: range))
}

public func invalidate() {
invalidate(highlighter.fullTextSet)
}
}

extension HighlighterInvalidationBuffer {
Expand All @@ -48,7 +42,7 @@ extension HighlighterInvalidationBuffer {
switch state {
case .pendingInvalidation(let set):
self.state = .idle
highlighter.invalidate(set)
highlighter.invalidate(.set(set))
case .buffering:
self.state = .idle
case .idle:
Expand Down
4 changes: 4 additions & 0 deletions Sources/Neon/TextSystemInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public extension TextSystemInterface {
}
}

func clearAllStyles() {
clearStyle(in: NSRange(0..<length))
}

func applyStyles(to tokens: [Token]) {
for token in tokens {
applyStyle(to: token)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Neon/TreeSitterClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class TreeSitterClient {
///
/// This function will only be invoked if `computeInvalidations`
/// was true at the time an edit was applied.
public var invalidationHandler: (IndexSet) -> Void
public var invalidationHandler: (TextTarget) -> Void

public init(language: Language, transformer: @escaping Point.LocationTransformer, synchronousLengthThreshold: Int = 1024) throws {
self.parser = Parser()
Expand Down Expand Up @@ -249,7 +249,7 @@ extension TreeSitterClient {
return
}

self.invalidationHandler(transformedSet)
self.invalidationHandler(.set(transformedSet))
}
}

Expand Down

0 comments on commit 0ab3dd3

Please sign in to comment.