-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathCodeEditTextView.swift
More file actions
116 lines (107 loc) · 4.39 KB
/
CodeEditTextView.swift
File metadata and controls
116 lines (107 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// CodeEditTextView.swift
// CodeEditTextView
//
// Created by Lukas Pistrol on 24.05.22.
//
import SwiftUI
import STTextView
import CodeEditLanguages
/// A `SwiftUI` wrapper for a ``STTextViewController``.
public struct CodeEditTextView: NSViewControllerRepresentable {
/// Initializes a Text Editor
/// - Parameters:
/// - text: The text content
/// - language: The language for syntax highlighting
/// - theme: The theme for syntax highlighting
/// - useThemeBackground: Whether CodeEditTextView uses theme background color or is transparent
/// - 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`)
/// - highlightProvider: A class you provide to perform syntax highlighting. Leave this as `nil` to use the
/// 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,
theme: Binding<EditorTheme>,
font: Binding<NSFont>,
tabWidth: Binding<Int>,
lineHeight: Binding<Double>,
wrapLines: Binding<Bool>,
editorOverscroll: Binding<Double> = .constant(0.0),
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
useThemeBackground: Bool = true,
highlightProvider: HighlightProviding? = nil,
contentInsets: NSEdgeInsets? = nil,
isEditable: Bool = true
) {
self._text = text
self.language = language
self._theme = theme
self.useThemeBackground = useThemeBackground
self._font = font
self._tabWidth = tabWidth
self._lineHeight = lineHeight
self._wrapLines = wrapLines
self._editorOverscroll = editorOverscroll
self.cursorPosition = cursorPosition
self.highlightProvider = highlightProvider
self.contentInsets = contentInsets
self.isEditable = isEditable
}
@Binding private var text: String
private var language: CodeLanguage
@Binding private var theme: EditorTheme
@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?
private var useThemeBackground: Bool
private var highlightProvider: HighlightProviding?
private var contentInsets: NSEdgeInsets?
private var isEditable: Bool
public typealias NSViewControllerType = STTextViewController
public func makeNSViewController(context: Context) -> NSViewControllerType {
let controller = NSViewControllerType(
text: $text,
language: language,
font: font,
theme: theme,
tabWidth: tabWidth,
wrapLines: wrapLines,
cursorPosition: cursorPosition,
editorOverscroll: editorOverscroll,
useThemeBackground: useThemeBackground,
highlightProvider: highlightProvider,
contentInsets: contentInsets,
isEditable: isEditable
)
controller.lineHeightMultiple = lineHeight
return controller
}
public func updateNSViewController(_ controller: NSViewControllerType, context: Context) {
controller.font = font
controller.tabWidth = tabWidth
controller.wrapLines = wrapLines
controller.useThemeBackground = useThemeBackground
controller.lineHeightMultiple = lineHeight
controller.editorOverscroll = editorOverscroll
controller.contentInsets = contentInsets
// Updating the language and theme needlessly can cause highlights to be re-calculated.
if controller.language.id != language.id {
controller.language = language
}
if controller.theme != theme {
controller.theme = theme
}
controller.reloadUI()
return
}
}