Skip to content

Commit 091fc34

Browse files
committed
Rename to SourceEditor, SourceEditorConfiguration
1 parent b97f9f3 commit 091fc34

File tree

16 files changed

+123
-115
lines changed

16 files changed

+123
-115
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,73 @@ struct ContentView: View {
1919

2020
@State private var language: CodeLanguage = .default
2121
@State private var theme: EditorTheme = .light
22+
@State private var cursorPositions: [CursorPosition] = [.init(line: 1, column: 1)]
23+
2224
@State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .medium)
2325
@AppStorage("wrapLines") private var wrapLines: Bool = true
24-
@State private var cursorPositions: [CursorPosition] = [.init(line: 1, column: 1)]
2526
@AppStorage("systemCursor") private var useSystemCursor: Bool = false
26-
@State private var isInLongParse = false
27-
@State private var settingsIsPresented: Bool = false
28-
@State private var treeSitterClient = TreeSitterClient()
29-
@AppStorage("showMinimap") private var showMinimap: Bool = true
3027
@State private var indentOption: IndentOption = .spaces(count: 4)
3128
@AppStorage("reformatAtColumn") private var reformatAtColumn: Int = 80
29+
30+
@AppStorage("showGutter") private var showGutter: Bool = true
31+
@AppStorage("showMinimap") private var showMinimap: Bool = true
3232
@AppStorage("showReformattingGuide") private var showReformattingGuide: Bool = false
3333

34+
@State private var isInLongParse = false
35+
@State private var settingsIsPresented: Bool = false
36+
37+
@State private var treeSitterClient = TreeSitterClient()
38+
3439
init(document: Binding<CodeEditSourceEditorExampleDocument>, fileURL: URL?) {
3540
self._document = document
3641
self.fileURL = fileURL
3742
}
3843

3944
var body: some View {
4045
GeometryReader { proxy in
41-
CodeEditSourceEditor(
46+
SourceEditor(
4247
document.text,
4348
language: language,
44-
theme: theme,
45-
font: font,
46-
tabWidth: 4,
47-
indentOption: indentOption,
48-
lineHeight: 1.2,
49-
wrapLines: wrapLines,
50-
editorOverscroll: 0.3,
51-
cursorPositions: $cursorPositions,
52-
useThemeBackground: true,
53-
highlightProviders: [treeSitterClient],
54-
contentInsets: NSEdgeInsets(top: proxy.safeAreaInsets.top, left: 0, bottom: 28.0, right: 0),
55-
additionalTextInsets: NSEdgeInsets(top: 1, left: 0, bottom: 1, right: 0),
56-
useSystemCursor: useSystemCursor,
57-
showMinimap: showMinimap,
58-
reformatAtColumn: reformatAtColumn,
59-
showReformattingGuide: showReformattingGuide
49+
config: SourceEditorConfiguration(
50+
appearance: .init(theme: theme, font: font, wrapLines: wrapLines),
51+
behavior: .init(indentOption: indentOption),
52+
layout: .init(
53+
contentInsets: NSEdgeInsets(
54+
top: proxy.safeAreaInsets.top,
55+
left: showGutter ? 0 : 1,
56+
bottom: 28.0,
57+
right: 0
58+
)
59+
),
60+
peripherals: .init(
61+
showGutter: showGutter,
62+
showMinimap: showMinimap,
63+
showReformattingGuide: showReformattingGuide
64+
)
65+
),
66+
cursorPositions: $cursorPositions
6067
)
68+
// SourceEditor(
69+
// document.text,
70+
// language: language,
71+
// font: font,
72+
// theme: theme,
73+
// font: font,
74+
// tabWidth: 4,
75+
// indentOption: indentOption,
76+
// lineHeight: 1.2,
77+
// wrapLines: wrapLines,
78+
// editorOverscroll: 0.3,
79+
// cursorPositions: $cursorPositions,
80+
// useThemeBackground: true,
81+
// highlightProviders: [treeSitterClient],
82+
// contentInsets: NSEdgeInsets(top: proxy.safeAreaInsets.top, left: 0, bottom: 28.0, right: 0),
83+
// additionalTextInsets: NSEdgeInsets(top: 1, left: 0, bottom: 1, right: 0),
84+
// useSystemCursor: useSystemCursor,
85+
// showMinimap: showMinimap,
86+
// reformatAtColumn: reformatAtColumn,
87+
// showReformattingGuide: showReformattingGuide
88+
// )
6189
.overlay(alignment: .bottom) {
6290
StatusBar(
6391
fileURL: fileURL,
@@ -68,6 +96,7 @@ struct ContentView: View {
6896
isInLongParse: $isInLongParse,
6997
language: $language,
7098
theme: $theme,
99+
showGutter: $showGutter,
71100
showMinimap: $showMinimap,
72101
indentOption: $indentOption,
73102
reformatAtColumn: $reformatAtColumn,

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/StatusBar.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct StatusBar: View {
2222
@Binding var isInLongParse: Bool
2323
@Binding var language: CodeLanguage
2424
@Binding var theme: EditorTheme
25+
@Binding var showGutter: Bool
2526
@Binding var showMinimap: Bool
2627
@Binding var indentOption: IndentOption
2728
@Binding var reformatAtColumn: Int
@@ -33,6 +34,7 @@ struct StatusBar: View {
3334
IndentPicker(indentOption: $indentOption, enabled: document.text.length == 0)
3435
.buttonStyle(.borderless)
3536
Toggle("Wrap Lines", isOn: $wrapLines)
37+
Toggle("Show Gutter", isOn: $showGutter)
3638
Toggle("Show Minimap", isOn: $showMinimap)
3739
Toggle("Show Reformatting Guide", isOn: $showReformattingGuide)
3840
Picker("Reformat column at column", selection: $reformatAtColumn) {

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ extension TextViewController {
6262
}
6363
setUpKeyBindings(eventMonitor: &self.localEvenMonitor)
6464
updateContentInsets()
65+
66+
config.didSetOnController(controller: self, oldConfig: nil)
6567
}
6668

6769
func setUpConstraints() {

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class TextViewController: NSViewController {
6262

6363
/// The configuration for the editor, when updated will automatically update the controller to reflect the new
6464
/// configuration.
65-
public var config: EditorConfig {
65+
public var config: SourceEditorConfiguration {
6666
didSet {
6767
config.didSetOnController(controller: self, oldConfig: oldValue)
6868
}
@@ -177,7 +177,7 @@ public class TextViewController: NSViewController {
177177
init(
178178
string: String,
179179
language: CodeLanguage,
180-
config: EditorConfig,
180+
config: SourceEditorConfiguration,
181181
cursorPositions: [CursorPosition],
182182
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
183183
undoManager: CEUndoManager? = nil,

Sources/CodeEditSourceEditor/Documentation.docc/Documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ``CodeEditSourceEditor``
1+
# ``SourceEditor``
22

33
A code editor with syntax highlighting powered by tree-sitter.
44

Sources/CodeEditSourceEditor/Gutter/GutterView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public protocol GutterViewDelegate: AnyObject {
1919
///
2020
/// If the gutter needs more space (when the number of digits in the numbers increases eg. adding a line after line 99),
2121
/// it will notify it's delegate via the ``GutterViewDelegate/gutterViewWidthDidUpdate(newWidth:)`` method. In
22-
/// `CodeEditSourceEditor`, this notifies the ``TextViewController``, which in turn updates the textview's edge insets
22+
/// `SourceEditor`, this notifies the ``TextViewController``, which in turn updates the textview's edge insets
2323
/// to adjust for the new leading inset.
2424
///
2525
/// This view also listens for selection updates, and draws a selected background on selected lines to keep the illusion
@@ -95,7 +95,11 @@ public class GutterView: NSView {
9595
true
9696
}
9797

98-
public convenience init(config: EditorConfig, textView: TextView, delegate: GutterViewDelegate? = nil) {
98+
public convenience init(
99+
config: SourceEditorConfiguration,
100+
textView: TextView,
101+
delegate: GutterViewDelegate? = nil
102+
) {
99103
self.init(
100104
font: config.appearance.font,
101105
textColor: config.appearance.theme.text.color,

Sources/CodeEditSourceEditor/ReformattingGuide/ReformattingGuideView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ReformattingGuideView: NSView {
1616
didSet { needsDisplay = true }
1717
}
1818

19-
convenience init(config: borrowing EditorConfig) {
19+
convenience init(config: borrowing SourceEditorConfiguration) {
2020
self.init(
2121
column: config.behavior.reformatAtColumn,
2222
theme: config.appearance.theme

Sources/CodeEditSourceEditor/CodeEditSourceEditor/CodeEditSourceEditor+Coordinator.swift renamed to Sources/CodeEditSourceEditor/SourceEditor/SourceEditor+Coordinator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CodeEditSourceEditor+Coordinator.swift
2+
// SourceEditor+Coordinator.swift
33
// CodeEditSourceEditor
44
//
55
// Created by Khan Winter on 5/20/24.
@@ -9,7 +9,7 @@ import Foundation
99
import SwiftUI
1010
import CodeEditTextView
1111

12-
extension CodeEditSourceEditor {
12+
extension SourceEditor {
1313
@MainActor
1414
public class Coordinator: NSObject {
1515
weak var controller: TextViewController?

Sources/CodeEditSourceEditor/CodeEditSourceEditor/CodeEditSourceEditor.swift renamed to Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CodeEditSourceEditor.swift
2+
// SourceEditor.swift
33
// CodeEditSourceEditor
44
//
55
// Created by Lukas Pistrol on 24.05.22.
@@ -11,7 +11,7 @@ import CodeEditTextView
1111
import CodeEditLanguages
1212

1313
/// A SwiftUI View that provides source editing functionality.
14-
public struct CodeEditSourceEditor: NSViewControllerRepresentable {
14+
public struct SourceEditor: NSViewControllerRepresentable {
1515
package enum TextAPI {
1616
case binding(Binding<String>)
1717
case storage(NSTextStorage)
@@ -21,7 +21,8 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
2121
/// - Parameters:
2222
/// - text: The text content
2323
/// - language: The language for syntax highlighting
24-
/// - config: A configuration object, determining appearance, layout, behaviors and more. See ``EditorConfig``.
24+
/// - config: A configuration object, determining appearance, layout, behaviors and more.
25+
/// See ``SourceEditorConfiguration``.
2526
/// - cursorPositions: The cursor's position in the editor, measured in `(lineNum, columnNum)`
2627
/// - highlightProviders: A set of classes you provide to perform syntax highlighting. Leave this as `nil` to use
2728
/// the default `TreeSitterClient` highlighter.
@@ -30,7 +31,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
3031
public init(
3132
_ text: Binding<String>,
3233
language: CodeLanguage,
33-
config: EditorConfig,
34+
config: SourceEditorConfiguration,
3435
cursorPositions: Binding<[CursorPosition]>,
3536
highlightProviders: [any HighlightProviding]? = nil,
3637
undoManager: CEUndoManager? = nil,
@@ -49,7 +50,8 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
4950
/// - Parameters:
5051
/// - text: The text content
5152
/// - language: The language for syntax highlighting
52-
/// - config: A configuration object, determining appearance, layout, behaviors and more. See ``EditorConfig``.
53+
/// - config: A configuration object, determining appearance, layout, behaviors and more.
54+
/// See ``SourceEditorConfiguration``.
5355
/// - cursorPositions: The cursor's position in the editor, measured in `(lineNum, columnNum)`
5456
/// - highlightProviders: A set of classes you provide to perform syntax highlighting. Leave this as `nil` to use
5557
/// the default `TreeSitterClient` highlighter.
@@ -58,7 +60,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
5860
public init(
5961
_ text: NSTextStorage,
6062
language: CodeLanguage,
61-
config: EditorConfig,
63+
config: SourceEditorConfiguration,
6264
cursorPositions: Binding<[CursorPosition]>,
6365
highlightProviders: [any HighlightProviding]? = nil,
6466
undoManager: CEUndoManager? = nil,
@@ -75,7 +77,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
7577

7678
package var text: TextAPI
7779
private var language: CodeLanguage
78-
private var config: EditorConfig
80+
private var config: SourceEditorConfiguration
7981
package var cursorPositions: Binding<[CursorPosition]>
8082
private var highlightProviders: [any HighlightProviding]?
8183
private var undoManager: CEUndoManager?
@@ -135,6 +137,9 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
135137
return
136138
}
137139

140+
if controller.language != language {
141+
controller.language = language
142+
}
138143
controller.config = config
139144
updateHighlighting(controller, coordinator: context.coordinator)
140145

@@ -162,38 +167,3 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
162167
== coordinator.highlightProviders.map { ObjectIdentifier($0) }
163168
}
164169
}
165-
166-
// swiftlint:disable:next line_length
167-
@available(*, unavailable, renamed: "CodeEditSourceEditor", message: "CodeEditTextView has been renamed to CodeEditSourceEditor.")
168-
public struct CodeEditTextView: View {
169-
public init(
170-
_ text: Binding<String>,
171-
language: CodeLanguage,
172-
theme: EditorTheme,
173-
font: NSFont,
174-
tabWidth: Int,
175-
indentOption: IndentOption = .spaces(count: 4),
176-
lineHeight: Double,
177-
wrapLines: Bool,
178-
editorOverscroll: CGFloat = 0,
179-
cursorPositions: Binding<[CursorPosition]>,
180-
useThemeBackground: Bool = true,
181-
highlightProvider: HighlightProviding? = nil,
182-
contentInsets: NSEdgeInsets? = nil,
183-
isEditable: Bool = true,
184-
isSelectable: Bool = true,
185-
letterSpacing: Double = 1.0,
186-
bracketPairEmphasis: BracketPairEmphasis? = nil,
187-
undoManager: CEUndoManager? = nil,
188-
coordinators: [any TextViewCoordinator] = []
189-
) {
190-
191-
}
192-
193-
public var body: some View {
194-
EmptyView()
195-
}
196-
}
197-
198-
// swiftlint:enable type_body_length
199-
// swiftlint:enable file_length

0 commit comments

Comments
 (0)