Skip to content
Merged
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
9 changes: 7 additions & 2 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
/// built-in `TreeSitterClient` highlighter.
/// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the
/// scroll view automatically adjust content insets.
/// - isEditable: A Boolean value that controls whether the text view allows the user to edit text.
public init(
_ text: Binding<String>,
language: CodeLanguage,
Expand All @@ -39,7 +40,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
useThemeBackground: Bool = true,
highlightProvider: HighlightProviding? = nil,
contentInsets: NSEdgeInsets? = nil
contentInsets: NSEdgeInsets? = nil,
isEditable: Bool = true
) {
self._text = text
self.language = language
Expand All @@ -53,6 +55,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
self.cursorPosition = cursorPosition
self.highlightProvider = highlightProvider
self.contentInsets = contentInsets
self.isEditable = isEditable
}

@Binding private var text: String
Expand All @@ -67,6 +70,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
private var useThemeBackground: Bool
private var highlightProvider: HighlightProviding?
private var contentInsets: NSEdgeInsets?
private var isEditable: Bool

public typealias NSViewControllerType = STTextViewController

Expand All @@ -82,7 +86,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
editorOverscroll: editorOverscroll,
useThemeBackground: useThemeBackground,
highlightProvider: highlightProvider,
contentInsets: contentInsets
contentInsets: contentInsets,
isEditable: isEditable
)
controller.lineHeightMultiple = lineHeight
return controller
Expand Down
17 changes: 16 additions & 1 deletion Sources/CodeEditTextView/STTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
/// Whether lines wrap to the width of the editor
public var wrapLines: Bool

/// Whether or not text view is editable by user
public var isEditable: Bool

/// Filters used when applying edits..
internal var textFilters: [TextFormation.Filter] = []

Expand Down Expand Up @@ -81,7 +84,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
editorOverscroll: Double,
useThemeBackground: Bool,
highlightProvider: HighlightProviding? = nil,
contentInsets: NSEdgeInsets? = nil
contentInsets: NSEdgeInsets? = nil,
isEditable: Bool
) {
self.text = text
self.language = language
Expand All @@ -94,6 +98,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
self.useThemeBackground = useThemeBackground
self.highlightProvider = highlightProvider
self.contentInsets = contentInsets
self.isEditable = isEditable
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -123,6 +128,11 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
rulerView.drawSeparator = false
rulerView.baselineOffset = baselineOffset
rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular)

if self.isEditable == false {
rulerView.selectedLineTextColor = nil
}

scrollView.verticalRulerView = rulerView
scrollView.rulersVisible = true

Expand All @@ -135,11 +145,13 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
textView.selectionBackgroundColor = theme.selection
textView.selectedLineHighlightColor = theme.lineHighlight
textView.string = self.text.wrappedValue
textView.isEditable = self.isEditable
textView.widthTracksTextView = self.wrapLines
textView.highlightSelectedLine = true
textView.allowsUndo = true
textView.setupMenus()
textView.delegate = self
textView.highlightSelectedLine = self.isEditable

scrollView.documentView = textView

Expand Down Expand Up @@ -223,10 +235,13 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
textView?.insertionPointColor = theme.insertionPoint
textView?.selectionBackgroundColor = theme.selection
textView?.selectedLineHighlightColor = theme.lineHighlight
textView?.isEditable = isEditable
textView.highlightSelectedLine = isEditable

rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
rulerView?.separatorColor = theme.invisibles
rulerView?.baselineOffset = baselineOffset
rulerView.highlightSelectedLine = isEditable

if let scrollView = view as? NSScrollView {
scrollView.drawsBackground = useThemeBackground
Expand Down
3 changes: 2 additions & 1 deletion Tests/CodeEditTextViewTests/STTextViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ final class STTextViewControllerTests: XCTestCase {
tabWidth: 4,
wrapLines: true,
editorOverscroll: 0.5,
useThemeBackground: true
useThemeBackground: true,
isEditable: true
)
}

Expand Down