Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for toggling line numbers on and off #124

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -31,6 +32,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
tabWidth: Binding<Int>,
lineHeight: Binding<Double>,
wrapLines: Binding<Bool>,
lineNumbers: Binding<Bool>,
editorOverscroll: Binding<Double> = .constant(0.0),
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
useThemeBackground: Bool = true
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -68,6 +72,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
theme: theme,
tabWidth: tabWidth,
wrapLines: wrapLines,
lineNumbers: lineNumbers,
cursorPosition: cursorPosition,
editorOverscroll: editorOverscroll,
useThemeBackground: useThemeBackground
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions Sources/CodeEditTextView/STTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
ben-p-commits marked this conversation as resolved.
Show resolved Hide resolved

scrollView.verticalRulerView = rulerView
scrollView.rulersVisible = true

Expand Down Expand Up @@ -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
ben-p-commits marked this conversation as resolved.
Show resolved Hide resolved

(view as? NSScrollView)?.drawsBackground = useThemeBackground
(view as? NSScrollView)?.backgroundColor = useThemeBackground ? theme.background : .clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class STTextViewControllerTests: XCTestCase {
theme: theme,
tabWidth: 4,
wrapLines: true,
lineNumbers: true,
editorOverscroll: 0.5,
useThemeBackground: true
)
Expand Down