Skip to content

Commit 8976e1f

Browse files
committed
Docs & Linter
1 parent 306eed3 commit 8976e1f

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/Toolbar.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct Toolbar: View {
2323
@Binding var language: CodeLanguage
2424
@Binding var theme: EditorTheme
2525
@Binding var showMinimap: Bool
26+
@Binding var indentOption: IndentOption
2627

2728
var body: some View {
2829
HStack {
@@ -64,6 +65,8 @@ struct Toolbar: View {
6465
.frame(height: 12)
6566
LanguagePicker(language: $language)
6667
.buttonStyle(.borderless)
68+
IndentPicker(indentOption: $indentOption, enabled: document.text.isEmpty)
69+
.buttonStyle(.borderless)
6770
}
6871
.font(.subheadline)
6972
.fontWeight(.medium)

Sources/CodeEditSourceEditor/Minimap/MinimapView+DocumentVisibleView.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ extension MinimapView {
1414
/// - Note: This is *tricky*, there's two cases for both views. If modifying, make sure to test both when the
1515
/// minimap is shorter than the container height and when the minimap should scroll.
1616
///
17-
/// The ``documentVisibleView`` uses a position that's entirely relative to the percent of the available scroll height scrolled.
18-
/// If the minimap is smaller than the container, it uses the same percent scrolled, but as a percent of the minimap height.
17+
/// The ``documentVisibleView`` uses a position that's entirely relative to the percent of the available scroll
18+
/// height scrolled. If the minimap is smaller than the container, it uses the same percent scrolled, but as a
19+
/// percent of the minimap height.
1920
///
2021
/// The height of the ``documentVisibleView`` is calculated using a ratio of the editor's height to the
2122
/// minimap's height, then applying that to the container's height.
2223
///
23-
/// The ``scrollView`` uses the scroll percentage calculated for the first case, and scrolls its content to that percentage.
24-
/// The ``scrollView`` is only modified if the minimap is longer than the container view.
24+
/// The ``scrollView`` uses the scroll percentage calculated for the first case, and scrolls its content to that
25+
/// percentage. The ``scrollView`` is only modified if the minimap is longer than the container view.
2526
func updateDocumentVisibleViewPosition() {
2627
guard let textView = textView, let editorScrollView = textView.enclosingScrollView, let layoutManager else {
2728
return

Sources/CodeEditSourceEditor/Minimap/MinimapView.swift

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MinimapView: FlippedNSView {
3737
public let separatorView: NSView
3838

3939
/// Responder for a drag gesture on the ``documentVisibleView``.
40-
var documentVisibleViewDragGesture: NSPanGestureRecognizer?
40+
var documentVisibleViewPanGesture: NSPanGestureRecognizer?
4141

4242
/// The layout manager that uses the ``lineRenderer`` to render and layout lines.
4343
var layoutManager: TextLayoutManager?
@@ -98,19 +98,36 @@ public class MinimapView: FlippedNSView {
9898

9999
super.init(frame: .zero)
100100

101-
let documentVisibleViewDragGesture = NSPanGestureRecognizer(
102-
target: self,
103-
action: #selector(documentVisibleViewDragged(_:))
104-
)
105-
documentVisibleView.addGestureRecognizer(documentVisibleViewDragGesture)
106-
self.documentVisibleViewDragGesture = documentVisibleViewDragGesture
101+
setUpPanGesture()
107102

108103
addSubview(scrollView)
109104
addSubview(documentVisibleView)
110105
addSubview(separatorView)
111106
scrollView.documentView = contentView
112107

113-
self.translatesAutoresizingMaskIntoConstraints = false
108+
translatesAutoresizingMaskIntoConstraints = false
109+
wantsLayer = true
110+
layer?.backgroundColor = theme.background.cgColor
111+
112+
setUpLayoutManager(textView: textView)
113+
setUpSelectionManager(textView: textView)
114+
115+
setUpConstraints()
116+
setUpListeners()
117+
}
118+
119+
/// Creates a pan gesture and attaches it to the ``documentVisibleView``.
120+
private func setUpPanGesture() {
121+
let documentVisibleViewPanGesture = NSPanGestureRecognizer(
122+
target: self,
123+
action: #selector(documentVisibleViewDragged(_:))
124+
)
125+
documentVisibleView.addGestureRecognizer(documentVisibleViewPanGesture)
126+
self.documentVisibleViewPanGesture = documentVisibleViewPanGesture
127+
}
128+
129+
/// Create the layout manager, using text contents from the given textview.
130+
private func setUpLayoutManager(textView: TextView) {
114131
let layoutManager = TextLayoutManager(
115132
textStorage: textView.textStorage,
116133
lineHeightMultiplier: 1.0,
@@ -121,7 +138,15 @@ public class MinimapView: FlippedNSView {
121138
)
122139
self.layoutManager = layoutManager
123140
(textView.textStorage.delegate as? MultiStorageDelegate)?.addDelegate(layoutManager)
141+
}
124142

143+
/// Set up a selection manager for drawing selections in the minimap.
144+
/// Requires ``layoutManager`` to not be `nil`.
145+
private func setUpSelectionManager(textView: TextView) {
146+
guard let layoutManager = layoutManager else {
147+
assertionFailure("No layout manager setup for the minimap.")
148+
return
149+
}
125150
self.selectionManager = TextSelectionManager(
126151
layoutManager: layoutManager,
127152
textStorage: textView.textStorage,
@@ -130,12 +155,6 @@ public class MinimapView: FlippedNSView {
130155
)
131156
contentView.textView = textView
132157
contentView.selectionManager = selectionManager
133-
134-
wantsLayer = true
135-
layer?.backgroundColor = theme.background.cgColor
136-
137-
setUpConstraints()
138-
setUpListeners()
139158
}
140159

141160
// MARK: - Constraints
@@ -166,6 +185,7 @@ public class MinimapView: FlippedNSView {
166185

167186
// MARK: - Scroll listeners
168187

188+
/// Set up listeners for relevant frame and selection updates.
169189
private func setUpListeners() {
170190
guard let editorScrollView = textView?.enclosingScrollView else { return }
171191
// Need to listen to:
@@ -213,7 +233,7 @@ public class MinimapView: FlippedNSView {
213233
}
214234

215235
override public func resetCursorRects() {
216-
// Don't use an iBeam
236+
// Don't use an iBeam in this view
217237
addCursorRect(bounds, cursor: .arrow)
218238
}
219239

@@ -235,7 +255,7 @@ public class MinimapView: FlippedNSView {
235255
}
236256
}
237257

238-
// Eat mouse events so we don't pass them on to the text view. Leads to some odd behavior.
258+
// Eat mouse events so we don't pass them on to the text view. Leads to some odd behavior otherwise.
239259

240260
override public func mouseDown(with event: NSEvent) { }
241261
override public func mouseDragged(with event: NSEvent) { }

0 commit comments

Comments
 (0)