Skip to content

Commit 34387a8

Browse files
committed
Fix Reformatting Guide X Position
1 parent 091fc34 commit 34387a8

File tree

7 files changed

+22
-18
lines changed

7 files changed

+22
-18
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ struct ContentView: View {
4848
language: language,
4949
config: SourceEditorConfiguration(
5050
appearance: .init(theme: theme, font: font, wrapLines: wrapLines),
51-
behavior: .init(indentOption: indentOption),
51+
behavior: .init(
52+
indentOption: indentOption,
53+
reformatAtColumn: reformatAtColumn
54+
),
5255
layout: .init(
5356
contentInsets: NSEdgeInsets(
5457
top: proxy.safeAreaInsets.top,

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,12 @@ extension TextViewController {
124124
object: textView,
125125
queue: .main
126126
) { [weak self] _ in
127-
guard let textView = self?.textView else { return }
128-
self?.gutterView.frame.size.height = (self?.textView.frame.height ?? 0) + 10
129-
self?.gutterView.frame.origin.y = (self?.textView.frame.origin.y ?? 0.0)
130-
- (self?.scrollView.contentInsets.top ?? 0)
131-
132-
self?.gutterView.needsDisplay = true
133-
self?.reformattingGuideView?.updatePosition(in: textView)
134-
self?.scrollView.needsLayout = true
127+
guard let self else { return }
128+
self.gutterView.frame.size.height = self.textView.frame.height + 10
129+
self.gutterView.frame.origin.y = self.textView.frame.origin.y - self.scrollView.contentInsets.top
130+
self.gutterView.needsDisplay = true
131+
self.reformattingGuideView?.updatePosition(in: self)
132+
self.scrollView.needsLayout = true
135133
}
136134
}
137135

Sources/CodeEditSourceEditor/Controller/TextViewController+ReloadUI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension TextViewController {
2222

2323
// Update reformatting guide position
2424
if let guideView = textView.subviews.first(where: { $0 is ReformattingGuideView }) as? ReformattingGuideView {
25-
guideView.updatePosition(in: textView)
25+
guideView.updatePosition(in: self)
2626
}
2727
}
2828
}

Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ extension TextViewController {
8787
}
8888

8989
package func styleReformattingGuideView() {
90-
reformattingGuideView.updatePosition(in: textView)
90+
reformattingGuideView.updatePosition(in: self)
9191
reformattingGuideView.isHidden = !showReformattingGuide
9292
}
9393

Sources/CodeEditSourceEditor/ReformattingGuide/ReformattingGuideView.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ class ReformattingGuideView: NSView {
7474
shadedRect.fill()
7575
}
7676

77-
func updatePosition(in textView: TextView) {
77+
func updatePosition(in controller: TextViewController) {
7878
// Calculate the x position based on the font's character width and column number
79-
let charWidth = textView.font.boundingRectForFont.width
80-
let xPosition = CGFloat(column) * charWidth / 2 // Divide by 2 to account for coordinate system
79+
let xPosition = (
80+
CGFloat(column) * (controller.fontCharWidth / 2) // Divide by 2 to account for coordinate system
81+
+ (controller.textViewInsets.left / 2)
82+
)
8183

8284
// Get the scroll view's content size
83-
guard let scrollView = textView.enclosingScrollView else { return }
85+
guard let scrollView = controller.scrollView else { return }
8486
let contentSize = scrollView.documentVisibleRect.size
8587

8688
// Ensure we don't create an invalid frame
@@ -90,7 +92,7 @@ class ReformattingGuideView: NSView {
9092
let newFrame = NSRect(
9193
x: xPosition,
9294
y: 0, // Start above the visible area
93-
width: maxWidth + 1000,
95+
width: maxWidth,
9496
height: contentSize.height // Use extended height
9597
).pixelAligned
9698

Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Behavior.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ extension SourceEditorConfiguration {
4848

4949
if oldConfig?.reformatAtColumn != reformatAtColumn {
5050
controller.reformattingGuideView.column = reformatAtColumn
51-
controller.reformattingGuideView.updatePosition(in: controller.textView)
51+
controller.reformattingGuideView.updatePosition(in: controller)
52+
controller.view.updateConstraintsForSubtreeIfNeeded()
5253
}
5354
}
5455
}

Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Peripherals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension SourceEditorConfiguration {
4242

4343
if oldConfig?.showReformattingGuide != showReformattingGuide {
4444
controller.reformattingGuideView.isHidden = !showReformattingGuide
45-
controller.reformattingGuideView.updatePosition(in: controller.textView)
45+
controller.reformattingGuideView.updatePosition(in: controller)
4646
}
4747

4848
if shouldUpdateInsets && controller.scrollView != nil { // Check for view existence

0 commit comments

Comments
 (0)