Skip to content

Commit 5bf097f

Browse files
committed
Started work on the reformatting guide
1 parent bb2cbf5 commit 5bf097f

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ extension TextViewController {
3131
minimapView = MinimapView(textView: textView, theme: theme)
3232
scrollView.addFloatingSubview(minimapView, for: .vertical)
3333

34+
// Add reformatting guide view
35+
let guideView = ReformattingGuideView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
36+
textView.addSubview(guideView)
37+
3438
let findViewController = FindViewController(target: self, childView: scrollView)
3539
addChild(findViewController)
3640
self.findViewController = findViewController

Sources/CodeEditSourceEditor/Controller/TextViewController+ReloadUI.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ extension TextViewController {
1919
highlighter?.invalidate()
2020
minimapView.updateContentViewHeight()
2121
minimapView.updateDocumentVisibleViewPosition()
22+
23+
// Update reformatting guide position
24+
if let guideView = textView.subviews.first(where: { $0 is ReformattingGuideView }) as? ReformattingGuideView {
25+
guideView.updatePosition(in: textView)
26+
}
2227
}
2328
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import AppKit
2+
import CodeEditTextView
3+
4+
class ReformattingGuideView: NSView {
5+
private let column: Int = 80
6+
7+
override init(frame frameRect: NSRect) {
8+
super.init(frame: frameRect)
9+
wantsLayer = true
10+
print("ReformattingGuideView initialized with frame: \(frameRect)")
11+
}
12+
13+
required init?(coder: NSCoder) {
14+
fatalError("init(coder:) has not been implemented")
15+
}
16+
17+
override func draw(_ dirtyRect: NSRect) {
18+
super.draw(dirtyRect)
19+
print("Drawing guide view with frame: \(frame)")
20+
21+
// For debugging, make the guide more visible
22+
NSColor.red.withAlphaComponent(0.3).setFill()
23+
frame.fill()
24+
25+
// Draw a border around the guide for better visibility
26+
NSColor.blue.setStroke()
27+
let borderPath = NSBezierPath(rect: frame)
28+
borderPath.lineWidth = 2.0
29+
borderPath.stroke()
30+
31+
// Get the current theme's background color to determine if we're in light or dark mode
32+
let isLightMode = NSApp.effectiveAppearance.name == .aqua
33+
34+
// Set the line color based on the theme
35+
let lineColor = isLightMode ?
36+
NSColor.black.withAlphaComponent(0.1) :
37+
NSColor.white.withAlphaComponent(0.1)
38+
39+
// Set the shaded area color (slightly more transparent)
40+
let shadedColor = isLightMode ?
41+
NSColor.black.withAlphaComponent(0.05) :
42+
NSColor.white.withAlphaComponent(0.05)
43+
44+
// Draw the vertical line (accounting for inverted Y coordinate system)
45+
lineColor.setStroke()
46+
let linePath = NSBezierPath()
47+
linePath.move(to: NSPoint(x: frame.minX, y: frame.maxY)) // Start at top
48+
linePath.line(to: NSPoint(x: frame.minX, y: frame.minY)) // Draw down to bottom
49+
linePath.lineWidth = 1.0
50+
linePath.stroke()
51+
52+
// Draw the shaded area to the right of the line
53+
shadedColor.setFill()
54+
let shadedRect = NSRect(
55+
x: frame.minX,
56+
y: frame.minY,
57+
width: frame.width,
58+
height: frame.height
59+
)
60+
shadedRect.fill()
61+
}
62+
63+
func updatePosition(in textView: TextView) {
64+
// Wait for the text view to have a valid width
65+
guard textView.frame.width > 0 else {
66+
print("Text view width is 0, skipping position update")
67+
return
68+
}
69+
70+
// Calculate the x position based on the font's character width and column number
71+
let charWidth = textView.font.boundingRectForFont.width
72+
let xPosition = CGFloat(column) * charWidth
73+
74+
print("Updating guide position:")
75+
print("- Character width: \(charWidth)")
76+
print("- Target column: \(column)")
77+
print("- Calculated x position: \(xPosition)")
78+
print("- Text view width: \(textView.frame.width)")
79+
80+
// Ensure we don't create an invalid frame
81+
let maxWidth = max(0, textView.frame.width - xPosition/2)
82+
83+
// Update the frame to be a vertical line at column 80 with a shaded area to the right
84+
let newFrame = NSRect(
85+
x: 200,
86+
y: 0,
87+
width: maxWidth,
88+
height: textView.frame.height
89+
)
90+
print("Setting new frame: \(newFrame)")
91+
92+
frame = newFrame
93+
needsDisplay = true
94+
}
95+
}

0 commit comments

Comments
 (0)