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

Invalidate Visible Range, Responsive Language Update #238

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion Sources/CodeEditSourceEditor/CodeEditSourceEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
return
}

controller.font = font
if controller.font != font {
controller.font = font
}

controller.wrapLines = wrapLines
controller.useThemeBackground = useThemeBackground
controller.lineHeightMultiple = lineHeight
controller.editorOverscroll = editorOverscroll
controller.contentInsets = contentInsets

if controller.isEditable != isEditable {
controller.isEditable = isEditable
}
Expand All @@ -173,15 +177,19 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
if controller.language.id != language.id {
controller.language = language
}

if controller.theme != theme {
controller.theme = theme
}

if controller.indentOption != indentOption {
controller.indentOption = indentOption
}

if controller.tabWidth != tabWidth {
controller.tabWidth = tabWidth
}

if controller.letterSpacing != letterSpacing {
controller.letterSpacing = letterSpacing
}
Expand Down
17 changes: 11 additions & 6 deletions Sources/CodeEditSourceEditor/Highlighting/Highlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class Highlighter: NSObject {
return IndexSet(integersIn: textView?.visibleTextRange ?? NSRange())
}()

// MARK: - Tasks

private var runningTasks: [UUID: Task<Void, Never>] = [:]

// MARK: - UI

/// The text view to highlight
Expand Down Expand Up @@ -114,6 +110,15 @@ class Highlighter: NSObject {
/// - Parameter language: The language to update to.
public func setLanguage(language: CodeLanguage) {
guard let textView = self.textView else { return }
// Remove all current highlights. Makes the language setting feel snappier and tells the user we're doing
// something immediately.
textView.textStorage.setAttributes(
attributeProvider.attributesFor(nil),
range: NSRange(location: 0, length: textView.textStorage.length)
)
textView.layoutManager.invalidateLayoutForRect(textView.visibleRect)
validSet.removeAll()
pendingSet.removeAll()
highlightProvider?.setUp(textView: textView, codeLanguage: language)
invalidate()
}
Expand Down Expand Up @@ -304,11 +309,11 @@ extension Highlighter {
visibleSet.insert(range: editedRange)
}

updateVisibleSet(textView: textView)

highlightProvider?.applyEdit(textView: textView, range: range, delta: delta) { [weak self] invalidIndexSet in
let indexSet = invalidIndexSet
.union(IndexSet(integersIn: editedRange))
// Only invalidate indices that are visible.
.intersection(self?.visibleSet ?? IndexSet())

for range in indexSet.rangeView {
self?.invalidate(range: NSRange(range))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public final class TreeSitterClient: HighlightProviding {
let longDocument = textView.documentRange.length > Constants.maxSyncContentLength

if longEdit || longDocument {

throw Error.syncUnavailable
}
try performSync(operation)
} catch {
Expand Down
Loading