From 3275ebc74d2ac26c3101e32351c840be27dc9211 Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Mon, 23 Jan 2023 16:21:23 -0500 Subject: [PATCH 1/2] add support for toggling line numbers on and off --- Sources/CodeEditTextView/CodeEditTextView.swift | 6 ++++++ Sources/CodeEditTextView/STTextViewController.swift | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/Sources/CodeEditTextView/CodeEditTextView.swift b/Sources/CodeEditTextView/CodeEditTextView.swift index 1b56b0d6b..f2fe0411d 100644 --- a/Sources/CodeEditTextView/CodeEditTextView.swift +++ b/Sources/CodeEditTextView/CodeEditTextView.swift @@ -22,6 +22,7 @@ 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 + /// - lineNumbers: Whether to display line numbers /// - editorOverscroll: The percentage for overscroll, between 0-1 (default: `0.0`) public init( _ text: Binding, @@ -31,6 +32,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { tabWidth: Binding, lineHeight: Binding, wrapLines: Binding, + lineNumbers: Binding, editorOverscroll: Binding = .constant(0.0), cursorPosition: Published<(Int, Int)>.Publisher? = nil, useThemeBackground: Bool = true @@ -43,6 +45,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { self._tabWidth = tabWidth self._lineHeight = lineHeight self._wrapLines = wrapLines + self._lineNumbers = lineNumbers self._editorOverscroll = editorOverscroll self.cursorPosition = cursorPosition } @@ -54,6 +57,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { @Binding private var tabWidth: Int @Binding private var lineHeight: Double @Binding private var wrapLines: Bool + @Binding private var lineNumbers: Bool @Binding private var editorOverscroll: Double private var cursorPosition: Published<(Int, Int)>.Publisher? private var useThemeBackground: Bool @@ -68,6 +72,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { theme: theme, tabWidth: tabWidth, wrapLines: wrapLines, + lineNumbers: lineNumbers, cursorPosition: cursorPosition, editorOverscroll: editorOverscroll, useThemeBackground: useThemeBackground @@ -83,6 +88,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { controller.useThemeBackground = useThemeBackground controller.lineHeightMultiple = lineHeight controller.editorOverscroll = editorOverscroll + controller.lineNumbers = lineNumbers // Updating the language and theme needlessly can cause highlights to be re-calculated. if controller.language.id != language.id { diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index d08862bb9..3e649d5c0 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -51,6 +51,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt /// Whether lines wrap to the width of the editor public var wrapLines: Bool + /// Whether to display line numbers in the editor + public var lineNumbers: Bool = true + // MARK: - Highlighting internal var highlighter: Highlighter? @@ -65,6 +68,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt theme: EditorTheme, tabWidth: Int, wrapLines: Bool, + lineNumbers: Bool, cursorPosition: Published<(Int, Int)>.Publisher? = nil, editorOverscroll: Double, useThemeBackground: Bool @@ -75,6 +79,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt self.theme = theme self.tabWidth = tabWidth self.wrapLines = wrapLines + self.lineNumbers = lineNumbers self.cursorPosition = cursorPosition self.editorOverscroll = editorOverscroll self.useThemeBackground = useThemeBackground @@ -102,6 +107,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) + rulerView.isHidden = !lineNumbers + scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true @@ -221,6 +228,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear rulerView?.separatorColor = theme.invisibles rulerView?.baselineOffset = baselineOffset + rulerView.isHidden = !lineNumbers (view as? NSScrollView)?.drawsBackground = useThemeBackground (view as? NSScrollView)?.backgroundColor = useThemeBackground ? theme.background : .clear From a4f7e4cfd10e83809e66067cc37f2ceda2f78ce6 Mon Sep 17 00:00:00 2001 From: Ben Palmer Date: Mon, 23 Jan 2023 16:28:42 -0500 Subject: [PATCH 2/2] fix broken test --- Tests/CodeEditTextViewTests/STTextViewControllerTests.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift index b866d270c..18c12359a 100644 --- a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift +++ b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift @@ -34,6 +34,7 @@ final class STTextViewControllerTests: XCTestCase { theme: theme, tabWidth: 4, wrapLines: true, + lineNumbers: true, editorOverscroll: 0.5, useThemeBackground: true )