From 4e433c563c91bfc7e65cd4eb48ad53d222bb32b1 Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Tue, 3 Jan 2023 18:44:48 -0500 Subject: [PATCH 1/6] Pass text wrapping configurations through to CodeEditTextView --- Sources/CodeEditTextView/CodeEditTextView.swift | 12 ++++++++++++ Sources/CodeEditTextView/STTextViewController.swift | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/CodeEditTextView.swift b/Sources/CodeEditTextView/CodeEditTextView.swift index fd02c5dce..6f9a22b3b 100644 --- a/Sources/CodeEditTextView/CodeEditTextView.swift +++ b/Sources/CodeEditTextView/CodeEditTextView.swift @@ -20,6 +20,8 @@ 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 + /// - wrappedIndent: The number of spaces to indent wrapped lines /// - editorOverscroll: The percentage for overscroll, between 0-1 (default: `0.0`) public init( _ text: Binding, @@ -28,6 +30,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable { font: Binding, tabWidth: Binding, lineHeight: Binding, + wrapLines: Binding, + wrappedIndent: Binding, editorOverscroll: Binding = .constant(0.0), cursorPosition: Published<(Int, Int)>.Publisher? = nil ) { @@ -37,6 +41,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable { self._font = font self._tabWidth = tabWidth self._lineHeight = lineHeight + self._wrapLines = wrapLines + self._wrappedIndent = wrappedIndent self._editorOverscroll = editorOverscroll self.cursorPosition = cursorPosition } @@ -47,6 +53,8 @@ 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 wrappedIndent: Int @Binding private var editorOverscroll: Double private var cursorPosition: Published<(Int, Int)>.Publisher? @@ -59,6 +67,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable { font: font, theme: theme, tabWidth: tabWidth, + wrapLines: wrapLines, + wrappedIndent: wrappedIndent, cursorPosition: cursorPosition, editorOverscroll: editorOverscroll ) @@ -71,6 +81,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable { controller.language = language controller.theme = theme controller.tabWidth = tabWidth + controller.wrapLines = wrapLines + controller.wrappedIndent = wrappedIndent controller.lineHeightMultiple = lineHeight controller.editorOverscroll = editorOverscroll controller.reloadUI() diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index fcfb51870..34437649d 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -44,6 +44,12 @@ 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 + + /// The number of spaces to indent wrapped lines + public var wrappedIndent: Int // MARK: - Highlighting @@ -58,6 +64,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt font: NSFont, theme: EditorTheme, tabWidth: Int, + wrapLines: Bool, + wrappedIndent: Int, cursorPosition: Published<(Int, Int)>.Publisher? = nil, editorOverscroll: Double ) { @@ -66,6 +74,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt self.font = font self.theme = theme self.tabWidth = tabWidth + self.wrapLines = wrapLines + self.wrappedIndent = wrappedIndent self.cursorPosition = cursorPosition self.editorOverscroll = editorOverscroll super.init(nibName: nil, bundle: nil) @@ -83,7 +93,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt // 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.textContainer.replaceLayoutManager(NSLayoutManager()) scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.hasVerticalScroller = true From 30dfff3d3e48fbe776028b4531fae28249239556 Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Wed, 4 Jan 2023 17:10:06 -0500 Subject: [PATCH 2/6] roll back addition of wrapped line stepper properties / presence in method signatures --- Sources/CodeEditTextView/CodeEditTextView.swift | 6 ------ Sources/CodeEditTextView/STTextViewController.swift | 5 ----- 2 files changed, 11 deletions(-) diff --git a/Sources/CodeEditTextView/CodeEditTextView.swift b/Sources/CodeEditTextView/CodeEditTextView.swift index 6f9a22b3b..3cb9823ca 100644 --- a/Sources/CodeEditTextView/CodeEditTextView.swift +++ b/Sources/CodeEditTextView/CodeEditTextView.swift @@ -21,7 +21,6 @@ public struct CodeEditTextView: NSViewControllerRepresentable { /// - tabWidth: The tab width /// - lineHeight: The line height multiplier (e.g. `1.2`) /// - wrapLines: Whether lines wrap to the width of the editor - /// - wrappedIndent: The number of spaces to indent wrapped lines /// - editorOverscroll: The percentage for overscroll, between 0-1 (default: `0.0`) public init( _ text: Binding, @@ -31,7 +30,6 @@ public struct CodeEditTextView: NSViewControllerRepresentable { tabWidth: Binding, lineHeight: Binding, wrapLines: Binding, - wrappedIndent: Binding, editorOverscroll: Binding = .constant(0.0), cursorPosition: Published<(Int, Int)>.Publisher? = nil ) { @@ -42,7 +40,6 @@ public struct CodeEditTextView: NSViewControllerRepresentable { self._tabWidth = tabWidth self._lineHeight = lineHeight self._wrapLines = wrapLines - self._wrappedIndent = wrappedIndent self._editorOverscroll = editorOverscroll self.cursorPosition = cursorPosition } @@ -54,7 +51,6 @@ public struct CodeEditTextView: NSViewControllerRepresentable { @Binding private var tabWidth: Int @Binding private var lineHeight: Double @Binding private var wrapLines: Bool - @Binding private var wrappedIndent: Int @Binding private var editorOverscroll: Double private var cursorPosition: Published<(Int, Int)>.Publisher? @@ -68,7 +64,6 @@ public struct CodeEditTextView: NSViewControllerRepresentable { theme: theme, tabWidth: tabWidth, wrapLines: wrapLines, - wrappedIndent: wrappedIndent, cursorPosition: cursorPosition, editorOverscroll: editorOverscroll ) @@ -82,7 +77,6 @@ public struct CodeEditTextView: NSViewControllerRepresentable { controller.theme = theme controller.tabWidth = tabWidth controller.wrapLines = wrapLines - controller.wrappedIndent = wrappedIndent controller.lineHeightMultiple = lineHeight controller.editorOverscroll = editorOverscroll controller.reloadUI() diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 34437649d..6857707b6 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -47,9 +47,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt /// Whether lines wrap to the width of the editor public var wrapLines: Bool - - /// The number of spaces to indent wrapped lines - public var wrappedIndent: Int // MARK: - Highlighting @@ -65,7 +62,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt theme: EditorTheme, tabWidth: Int, wrapLines: Bool, - wrappedIndent: Int, cursorPosition: Published<(Int, Int)>.Publisher? = nil, editorOverscroll: Double ) { @@ -75,7 +71,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt self.theme = theme self.tabWidth = tabWidth self.wrapLines = wrapLines - self.wrappedIndent = wrappedIndent self.cursorPosition = cursorPosition self.editorOverscroll = editorOverscroll super.init(nibName: nil, bundle: nil) From 91752fae429f75c540373bab0607737e53f12823 Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Fri, 6 Jan 2023 14:27:13 -0500 Subject: [PATCH 3/6] Remove invocation of convienence scrollview factory method, is more opaque and hides configs that are immediately overridden. --- .../CodeEditTextView/STTextViewController.swift | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 6857707b6..3e28391b9 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -83,22 +83,19 @@ 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 rulerView.textColor = .systemGray rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset - rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) + rulerView.font = NSFont(descriptor: NSFont.monospacedDigitSystemFont(ofSize: 11, weight: .regular).fontDescriptor, textTransform: AffineTransform(scaleByX: 1, byY: 1.1)) ?? .monospacedDigitSystemFont(ofSize: 11, weight: .regular) scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true @@ -112,7 +109,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() From a5ed347778cbe07df2ebb01995000cf2c70c24d2 Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Fri, 6 Jan 2023 14:47:05 -0500 Subject: [PATCH 4/6] roll back errant change --- Sources/CodeEditTextView/STTextViewController.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 3e28391b9..5bc07e97d 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -95,8 +95,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView.textColor = .systemGray rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset - rulerView.font = NSFont(descriptor: NSFont.monospacedDigitSystemFont(ofSize: 11, weight: .regular).fontDescriptor, textTransform: AffineTransform(scaleByX: 1, byY: 1.1)) ?? .monospacedDigitSystemFont(ofSize: 11, weight: .regular) - + rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true From b303aaeeaf0f7bf2f85222ce31f3a3d740786f5a Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Fri, 6 Jan 2023 15:01:05 -0500 Subject: [PATCH 5/6] whitespace cleanup --- Sources/CodeEditTextView/STTextViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 5bc07e97d..dc8a372d0 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -44,7 +44,7 @@ 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 @@ -84,7 +84,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt public override func loadView() { textView = STTextView() - + let scrollView = NSScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.hasVerticalScroller = true From 8f29f3cbef9f2a38ba2863373b2978f6a9c05afb Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Fri, 6 Jan 2023 16:08:51 -0500 Subject: [PATCH 6/6] fix broken tests --- Tests/CodeEditTextViewTests/STTextViewControllerTests.swift | 1 + 1 file changed, 1 insertion(+) 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 ) }