Skip to content

Commit

Permalink
Fix Selection highlighting for non everlaping ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed Feb 20, 2022
1 parent 4b602e0 commit 067db0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Sources/STTextView/STTextView+CopyPaste.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ extension STTextView {
return
}

let nsrange = NSRange(textLayoutManager.insertionPointLocation!, in: textContentStorage)
let insertLocation = textLayoutManager.insertionPointLocation ?? textLayoutManager.documentRange.location
let nsrange = NSRange(insertLocation, in: textContentStorage)
insertText(string, replacementRange: nsrange)
}

Expand Down
11 changes: 9 additions & 2 deletions Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,21 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
}

func updateSelectionHighlights() {
guard !textLayoutManager.textSelections.isEmpty else { return }
guard !textLayoutManager.textSelections.isEmpty else {
selectionView.subviews = []
return
}

selectionView.subviews = []

for textSelection in textLayoutManager.textSelections {
for textRange in textSelection.textRanges {
textLayoutManager.enumerateTextSegments(in: textRange, type: .highlight, options: []) {(textSegmentRange, textSegmentFrame, baselinePosition, textContainer) in
var highlightFrame = textSegmentFrame.intersection(frame)
guard !highlightFrame.isNull else {
return true
}

let highlight = STTextSelectionView()
highlight.wantsLayer = true

Expand Down

0 comments on commit 067db0b

Please sign in to comment.