Skip to content

Commit ae65660

Browse files
committed
Add configurable line number font
1 parent 1fa4d3c commit ae65660

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public class TextViewController: NSViewController {
108108
/// The font to use in the `textView`
109109
public var font: NSFont { configuration.appearance.font }
110110

111+
/// The optional font used for line numbers. When `nil`, the editor derives one from ``font``.
112+
public var lineNumberFont: NSFont? { configuration.appearance.lineNumberFont }
113+
111114
/// The ``EditorTheme`` used for highlighting.
112115
public var theme: EditorTheme { configuration.appearance.theme }
113116

Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Appearance.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ extension SourceEditorConfiguration {
1919
/// The default font.
2020
public var font: NSFont
2121

22+
/// An optional font for line numbers. When `nil`, a font derived from ``font`` is used.
23+
public var lineNumberFont: NSFont?
24+
2225
/// The line height multiplier (e.g. `1.2`).
2326
public var lineHeightMultiple: Double
2427

@@ -45,6 +48,7 @@ extension SourceEditorConfiguration {
4548
/// - useThemeBackground: Determines whether the editor uses the theme's background color, or a transparent
4649
/// background color.
4750
/// - font: The default font.
51+
/// - lineNumberFont: An optional font for line numbers. Defaults to a font derived from `font`.
4852
/// - lineHeightMultiple: The line height multiplier (e.g. `1.2`).
4953
/// - letterSpacing: The amount of space to use between letters, as a percent. Eg: `1.0` = no space, `1.5`
5054
/// = 1/2 of a character's width between characters, etc. Defaults to `1.0`.
@@ -57,6 +61,7 @@ extension SourceEditorConfiguration {
5761
theme: EditorTheme,
5862
useThemeBackground: Bool = true,
5963
font: NSFont,
64+
lineNumberFont: NSFont? = nil,
6065
lineHeightMultiple: Double = 1.2,
6166
letterSpacing: Double = 1.0,
6267
wrapLines: Bool,
@@ -67,6 +72,7 @@ extension SourceEditorConfiguration {
6772
self.theme = theme
6873
self.useThemeBackground = useThemeBackground
6974
self.font = font
75+
self.lineNumberFont = lineNumberFont
7076
self.lineHeightMultiple = lineHeightMultiple
7177
self.letterSpacing = letterSpacing
7278
self.wrapLines = wrapLines
@@ -86,10 +92,11 @@ extension SourceEditorConfiguration {
8692
if oldConfig?.font != font {
8793
controller.textView.font = font
8894
controller.textView.typingAttributes = controller.attributesFor(nil)
89-
controller.gutterView.font = font.rulerFont
9095
needsHighlighterInvalidation = true
9196
}
9297

98+
controller.gutterView.font = lineNumberFont ?? font.rulerFont
99+
93100
if oldConfig?.theme != theme || oldConfig?.useThemeBackground != useThemeBackground {
94101
updateControllerNewTheme(controller: controller)
95102
needsHighlighterInvalidation = true

Tests/CodeEditSourceEditorTests/Controller/TextViewControllerTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ final class TextViewControllerTests: XCTestCase {
235235
controller.configuration.appearance.letterSpacing = 1.0
236236
}
237237

238+
func test_lineNumberFont() {
239+
let defaultFont = controller.font.rulerFont
240+
XCTAssertNil(controller.lineNumberFont)
241+
XCTAssertEqual(controller.gutterView.font, defaultFont)
242+
243+
let customFont = NSFont.monospacedSystemFont(ofSize: 17, weight: .bold)
244+
controller.configuration.appearance.lineNumberFont = customFont
245+
XCTAssertEqual(controller.lineNumberFont, customFont)
246+
XCTAssertEqual(controller.gutterView.font, customFont)
247+
248+
controller.configuration.appearance.lineNumberFont = nil
249+
XCTAssertEqual(controller.gutterView.font, defaultFont)
250+
}
251+
238252
// MARK: Bracket Highlights
239253

240254
func test_bracketHighlights() throws {

0 commit comments

Comments
 (0)