Skip to content

Commit

Permalink
Support a text wrapping preference in the textview (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepistrol authored Jan 11, 2023
2 parents f5c485a + ef5567c commit c3eb00c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -28,6 +29,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
font: Binding<NSFont>,
tabWidth: Binding<Int>,
lineHeight: Binding<Double>,
wrapLines: Binding<Bool>,
editorOverscroll: Binding<Double> = .constant(0.0),
cursorPosition: Published<(Int, Int)>.Publisher? = nil
) {
Expand All @@ -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
}
Expand All @@ -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?

Expand All @@ -59,6 +63,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
font: font,
theme: theme,
tabWidth: tabWidth,
wrapLines: wrapLines,
cursorPosition: cursorPosition,
editorOverscroll: editorOverscroll
)
Expand All @@ -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()
Expand Down
17 changes: 9 additions & 8 deletions Sources/CodeEditTextView/STTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
) {
Expand All @@ -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)
Expand All @@ -78,23 +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)

scrollView.verticalRulerView = rulerView
scrollView.rulersVisible = true

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class STTextViewControllerTests: XCTestCase {
font: .monospacedSystemFont(ofSize: 11, weight: .medium),
theme: theme,
tabWidth: 4,
wrapLines: true,
editorOverscroll: 0.5
)
}
Expand Down

0 comments on commit c3eb00c

Please sign in to comment.