diff --git a/Sources/CodeEditTextView/CodeEditTextView.swift b/Sources/CodeEditTextView/CodeEditTextView.swift index fd02c5dce..3cb9823ca 100644 --- a/Sources/CodeEditTextView/CodeEditTextView.swift +++ b/Sources/CodeEditTextView/CodeEditTextView.swift @@ -20,6 +20,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { /// - font: The default font /// - tabWidth: The tab width /// - lineHeight: The line height multiplier (e.g. `1.2`) + /// - wrapLines: Whether lines wrap to the width of the editor /// - editorOverscroll: The percentage for overscroll, between 0-1 (default: `0.0`) public init( _ text: Binding, @@ -28,6 +29,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { font: Binding, tabWidth: Binding, lineHeight: Binding, + wrapLines: Binding, editorOverscroll: Binding = .constant(0.0), cursorPosition: Published<(Int, Int)>.Publisher? = nil ) { @@ -37,6 +39,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { self._font = font self._tabWidth = tabWidth self._lineHeight = lineHeight + self._wrapLines = wrapLines self._editorOverscroll = editorOverscroll self.cursorPosition = cursorPosition } @@ -47,6 +50,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { @Binding private var font: NSFont @Binding private var tabWidth: Int @Binding private var lineHeight: Double + @Binding private var wrapLines: Bool @Binding private var editorOverscroll: Double private var cursorPosition: Published<(Int, Int)>.Publisher? @@ -59,6 +63,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { font: font, theme: theme, tabWidth: tabWidth, + wrapLines: wrapLines, cursorPosition: cursorPosition, editorOverscroll: editorOverscroll ) @@ -71,6 +76,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { controller.language = language controller.theme = theme controller.tabWidth = tabWidth + controller.wrapLines = wrapLines controller.lineHeightMultiple = lineHeight controller.editorOverscroll = editorOverscroll controller.reloadUI() diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 98b2394b1..86a168154 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -45,6 +45,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt /// The editorOverscroll to use for the textView over scroll public var editorOverscroll: Double + /// Whether lines wrap to the width of the editor + public var wrapLines: Bool + // MARK: - Highlighting internal var highlighter: Highlighter? @@ -58,6 +61,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt font: NSFont, theme: EditorTheme, tabWidth: Int, + wrapLines: Bool, cursorPosition: Published<(Int, Int)>.Publisher? = nil, editorOverscroll: Double ) { @@ -66,6 +70,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt self.font = font self.theme = theme self.tabWidth = tabWidth + self.wrapLines = wrapLines self.cursorPosition = cursorPosition self.editorOverscroll = editorOverscroll super.init(nibName: nil, bundle: nil) @@ -78,15 +83,12 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt // MARK: VC Lifecycle public override func loadView() { - let scrollView = STTextView.scrollableTextView() - textView = scrollView.documentView as? STTextView - - // By default this is always null but is required for a couple operations - // during highlighting so we make a new one manually. - textView.textContainer.replaceLayoutManager(NSLayoutManager()) + textView = STTextView() + let scrollView = NSScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.hasVerticalScroller = true + scrollView.documentView = textView rulerView = STLineNumberRulerView(textView: textView, scrollView: scrollView) rulerView.backgroundColor = theme.background @@ -94,7 +96,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) - scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true @@ -107,7 +108,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView.selectionBackgroundColor = theme.selection textView.selectedLineHighlightColor = theme.lineHighlight textView.string = self.text.wrappedValue - textView.widthTracksTextView = true + textView.widthTracksTextView = self.wrapLines textView.highlightSelectedLine = true textView.allowsUndo = true textView.setupMenus() diff --git a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift index 03a9348fb..7c527c011 100644 --- a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift +++ b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift @@ -33,6 +33,7 @@ final class STTextViewControllerTests: XCTestCase { font: .monospacedSystemFont(ofSize: 11, weight: .medium), theme: theme, tabWidth: 4, + wrapLines: true, editorOverscroll: 0.5 ) }