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
7 changes: 7 additions & 0 deletions Textream/Textream/HighlightingTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ struct HighlightingTextEditor: NSViewRepresentable {
func updateNSView(_ scrollView: NSScrollView, context: Context) {
guard let textView = scrollView.documentView as? NSTextView else { return }

if textView.hasMarkedText() {
return
}

if textView.string != text {
let selectedRanges = textView.selectedRanges
textView.string = text
Expand Down Expand Up @@ -106,12 +110,14 @@ struct HighlightingTextEditor: NSViewRepresentable {

func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
guard !textView.hasMarkedText() else { return }
parent.text = textView.string
applyHighlighting(textView)
}

func textViewDidChangeSelection(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
guard !textView.hasMarkedText() else { return }
let pos = textView.selectedRange().location
if parent.editorCaretPosition != pos {
DispatchQueue.main.async { [weak self] in
Expand Down Expand Up @@ -141,6 +147,7 @@ struct HighlightingTextEditor: NSViewRepresentable {

func applyHighlighting(_ textView: NSTextView) {
guard let textStorage = textView.textStorage else { return }
guard !textView.hasMarkedText() else { return }
let fullRange = NSRange(location: 0, length: textStorage.length)
let text = textStorage.string

Expand Down