Skip to content
Open
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
11 changes: 9 additions & 2 deletions Sources/HighlightedTextEditor/HighlightedTextEditor.AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public struct HighlightedTextEditor: NSViewRepresentable, HighlightingTextEditor

public func updateNSView(_ view: ScrollableTextView, context: Context) {
context.coordinator.updatingNSView = true
let typingAttributes = view.textView.typingAttributes

let highlightedText = HighlightedTextEditor.getHighlightedText(
text: text,
Expand All @@ -64,7 +63,15 @@ public struct HighlightedTextEditor: NSViewRepresentable, HighlightingTextEditor
view.attributedText = highlightedText
runIntrospect(view)
view.selectedRanges = context.coordinator.selectedRanges
view.textView.typingAttributes = typingAttributes

// Update the typing attributes to match the attributes at the current insertion point.
if highlightedText.length > 0, let insertionIndex = view.selectedRanges.first?.rangeValue.location {
let insertionIndex = max(0, min(insertionIndex, highlightedText.length - 1))
view.textView.typingAttributes = highlightedText.attributes(at: insertionIndex, effectiveRange: nil)
} else {
view.textView.typingAttributes = [.font: defaultEditorFont]
}

context.coordinator.updatingNSView = false
}

Expand Down