Skip to content

Commit c3eb00c

Browse files
authored
Support a text wrapping preference in the textview (#105)
2 parents f5c485a + ef5567c commit c3eb00c

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Sources/CodeEditTextView/CodeEditTextView.swift

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
2020
/// - font: The default font
2121
/// - tabWidth: The tab width
2222
/// - lineHeight: The line height multiplier (e.g. `1.2`)
23+
/// - wrapLines: Whether lines wrap to the width of the editor
2324
/// - editorOverscroll: The percentage for overscroll, between 0-1 (default: `0.0`)
2425
public init(
2526
_ text: Binding<String>,
@@ -28,6 +29,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
2829
font: Binding<NSFont>,
2930
tabWidth: Binding<Int>,
3031
lineHeight: Binding<Double>,
32+
wrapLines: Binding<Bool>,
3133
editorOverscroll: Binding<Double> = .constant(0.0),
3234
cursorPosition: Published<(Int, Int)>.Publisher? = nil
3335
) {
@@ -37,6 +39,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
3739
self._font = font
3840
self._tabWidth = tabWidth
3941
self._lineHeight = lineHeight
42+
self._wrapLines = wrapLines
4043
self._editorOverscroll = editorOverscroll
4144
self.cursorPosition = cursorPosition
4245
}
@@ -47,6 +50,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
4750
@Binding private var font: NSFont
4851
@Binding private var tabWidth: Int
4952
@Binding private var lineHeight: Double
53+
@Binding private var wrapLines: Bool
5054
@Binding private var editorOverscroll: Double
5155
private var cursorPosition: Published<(Int, Int)>.Publisher?
5256

@@ -59,6 +63,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
5963
font: font,
6064
theme: theme,
6165
tabWidth: tabWidth,
66+
wrapLines: wrapLines,
6267
cursorPosition: cursorPosition,
6368
editorOverscroll: editorOverscroll
6469
)
@@ -71,6 +76,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
7176
controller.language = language
7277
controller.theme = theme
7378
controller.tabWidth = tabWidth
79+
controller.wrapLines = wrapLines
7480
controller.lineHeightMultiple = lineHeight
7581
controller.editorOverscroll = editorOverscroll
7682
controller.reloadUI()

Sources/CodeEditTextView/STTextViewController.swift

+9-8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
4545
/// The editorOverscroll to use for the textView over scroll
4646
public var editorOverscroll: Double
4747

48+
/// Whether lines wrap to the width of the editor
49+
public var wrapLines: Bool
50+
4851
// MARK: - Highlighting
4952

5053
internal var highlighter: Highlighter?
@@ -58,6 +61,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
5861
font: NSFont,
5962
theme: EditorTheme,
6063
tabWidth: Int,
64+
wrapLines: Bool,
6165
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
6266
editorOverscroll: Double
6367
) {
@@ -66,6 +70,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
6670
self.font = font
6771
self.theme = theme
6872
self.tabWidth = tabWidth
73+
self.wrapLines = wrapLines
6974
self.cursorPosition = cursorPosition
7075
self.editorOverscroll = editorOverscroll
7176
super.init(nibName: nil, bundle: nil)
@@ -78,23 +83,19 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
7883
// MARK: VC Lifecycle
7984

8085
public override func loadView() {
81-
let scrollView = STTextView.scrollableTextView()
82-
textView = scrollView.documentView as? STTextView
83-
84-
// By default this is always null but is required for a couple operations
85-
// during highlighting so we make a new one manually.
86-
textView.textContainer.replaceLayoutManager(NSLayoutManager())
86+
textView = STTextView()
8787

88+
let scrollView = NSScrollView()
8889
scrollView.translatesAutoresizingMaskIntoConstraints = false
8990
scrollView.hasVerticalScroller = true
91+
scrollView.documentView = textView
9092

9193
rulerView = STLineNumberRulerView(textView: textView, scrollView: scrollView)
9294
rulerView.backgroundColor = theme.background
9395
rulerView.textColor = .systemGray
9496
rulerView.drawSeparator = false
9597
rulerView.baselineOffset = baselineOffset
9698
rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular)
97-
9899
scrollView.verticalRulerView = rulerView
99100
scrollView.rulersVisible = true
100101

@@ -107,7 +108,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
107108
textView.selectionBackgroundColor = theme.selection
108109
textView.selectedLineHighlightColor = theme.lineHighlight
109110
textView.string = self.text.wrappedValue
110-
textView.widthTracksTextView = true
111+
textView.widthTracksTextView = self.wrapLines
111112
textView.highlightSelectedLine = true
112113
textView.allowsUndo = true
113114
textView.setupMenus()

Tests/CodeEditTextViewTests/STTextViewControllerTests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class STTextViewControllerTests: XCTestCase {
3333
font: .monospacedSystemFont(ofSize: 11, weight: .medium),
3434
theme: theme,
3535
tabWidth: 4,
36+
wrapLines: true,
3637
editorOverscroll: 0.5
3738
)
3839
}

0 commit comments

Comments
 (0)