Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix scrollSelectionToVisible, Scroll to Undone Text
  • Loading branch information
thecoolwinter committed Jun 18, 2025
commit 892839ee4606b6ec77ddd922a19e05261bd0981d
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@ extension TextView {

/// Scrolls the upmost selection to the visible rect if `scrollView` is not `nil`.
public func scrollSelectionToVisible() {
guard let scrollView, let selection = getSelection() else {
guard let scrollView else {
return
}

let offsetToScrollTo = offsetNotPivot(selection)

// There's a bit of a chicken-and-the-egg issue going on here. We need to know the rect to scroll to, but we
// can't know the exact rect to make visible without laying out the text. Then, once text is laid out the
// selection rect may be different again. To solve this, we loop until the frame doesn't change after a layout
// pass and scroll to that rect.

var lastFrame: CGRect = .zero
while let boundingRect = layoutManager.rectForOffset(offsetToScrollTo), lastFrame != boundingRect {
while let boundingRect = getSelection()?.boundingRect, lastFrame != boundingRect {
lastFrame = boundingRect
layoutManager.layoutLines()
selectionManager.updateSelectionViews()
selectionManager.drawSelections(in: visibleRect)
}
if lastFrame != .zero {
scrollView.contentView.scrollToVisible(lastFrame)
scrollView.reflectScrolledClipView(scrollView.contentView)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/CodeEditTextView/Utils/CEUndoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class CEUndoManager: UndoManager {
textView.textStorage.endEditing()

updateSelectionsForMutations(mutations: item.mutations.map { $0.mutation })
textView.scrollSelectionToVisible()

NotificationCenter.default.post(name: .NSUndoManagerDidUndoChange, object: self)
redoStack.append(item)
Expand Down Expand Up @@ -112,6 +113,7 @@ public class CEUndoManager: UndoManager {
textView.textStorage.endEditing()

updateSelectionsForMutations(mutations: item.mutations.map { $0.inverse })
textView.scrollSelectionToVisible()

NotificationCenter.default.post(name: .NSUndoManagerDidRedoChange, object: self)
undoStack.append(item)
Expand Down