Skip to content

Commit 8dc31c3

Browse files
committed
Merge Trailing Line when Adding Attachment
1 parent 69282e2 commit 8dc31c3

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Sources/CodeEditTextView/TextLayoutManager/TextAttachments/TextAttachment.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public protocol TextAttachment: AnyObject {
1818
/// This type cannot be initialized outside of `CodeEditTextView`, but will be received when interrogating
1919
/// the ``TextAttachmentManager``.
2020
public struct AnyTextAttachment: Equatable {
21-
var range: NSRange
22-
let attachment: any TextAttachment
21+
package(set) public var range: NSRange
22+
public let attachment: any TextAttachment
2323

2424
var width: CGFloat {
2525
attachment.width

Sources/CodeEditTextView/TextLayoutManager/TextAttachments/TextAttachmentManager.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,28 @@ public final class TextAttachmentManager {
2323
let attachment = AnyTextAttachment(range: range, attachment: attachment)
2424
let insertIndex = findInsertionIndex(for: range.location)
2525
orderedAttachments.insert(attachment, at: insertIndex)
26+
27+
// This is ugly, but if our attachment meets the end of the next line, we need to merge that line with this
28+
// one.
29+
var getNextOne = false
2630
layoutManager?.lineStorage.linesInRange(range).dropFirst().forEach {
2731
if $0.height != 0 {
2832
layoutManager?.lineStorage.update(atOffset: $0.range.location, delta: 0, deltaHeight: -$0.height)
2933
}
34+
35+
// Only do this if it's not the end of the document
36+
if range.max == $0.range.max && range.max != layoutManager?.lineStorage.length {
37+
getNextOne = true
38+
}
3039
}
40+
41+
if getNextOne,
42+
let trailingLine = layoutManager?.lineStorage.getLine(atOffset: range.max),
43+
trailingLine.height != 0 {
44+
// Update the one trailing line.
45+
layoutManager?.lineStorage.update(atOffset: range.max, delta: 0, deltaHeight: -trailingLine.height)
46+
}
47+
3148
layoutManager?.setNeedsLayout()
3249
}
3350

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Layout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ extension TextLayoutManager {
250250
let view = viewReuseQueue.getOrCreateView(forKey: lineFragment.data.id) {
251251
renderDelegate?.lineFragmentView(for: lineFragment.data) ?? LineFragmentView()
252252
}
253-
view.translatesAutoresizingMaskIntoConstraints = false
253+
view.translatesAutoresizingMaskIntoConstraints = true // Small optimization for lots of subviews
254254
view.setLineFragment(lineFragment.data)
255255
view.frame.origin = CGPoint(x: edgeInsets.left, y: yPos)
256256
layoutView?.addSubview(view)

0 commit comments

Comments
 (0)