Skip to content

Commit 71ea866

Browse files
committed
Documentation
1 parent ae2aa61 commit 71ea866

File tree

8 files changed

+153
-49
lines changed

8 files changed

+153
-49
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ struct ContentView: View {
103103
}
104104
}
105105
}
106+
107+
func bruh() {
108+
let editorController = TextViewController(
109+
string: "let x = 10;",
110+
language: .swift,
111+
config: SourceEditorConfiguration(
112+
appearance: .init(theme: theme, font: font),
113+
behavior: .init(indentOption: indentOption),
114+
layout: .init(editorOverscroll: editorOverscroll),
115+
peripherals: .init(showMinimap: showMinimap)
116+
),
117+
cursorPositions: [CursorPosition(line: 0, column: 0)],
118+
highlightProviders: [], // Use the tree-sitter provider by default
119+
undoManager: nil,
120+
coordinators: [] // Optionally inject editing behavior or other plugins.
121+
)
122+
}
106123
}
107124

108125
#Preview {

README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<p align="center">
88
<a aria-label="Follow CodeEdit on Twitter" href="https://twitter.com/CodeEditApp" target="_blank">
99
<img alt="" src="https://img.shields.io/badge/Follow%20@CodeEditApp-black.svg?style=for-the-badge&logo=Twitter">
10+
</a>
11+
<a aria-label="Follow CodeEdit on Bluesky" href="https://bsky.app/profile/codeedit.app" target="_blank">
12+
<img alt="" src="https://img.shields.io/badge/Follow%20@CodeEditApp-black.svg?style=for-the-badge&logo=Bluesky">
1013
</a>
1114
<a aria-label="Join the community on Discord" href="https://discord.gg/vChUXVf9Em" target="_blank">
1215
<img alt="" src="https://img.shields.io/badge/Join%20the%20community-black.svg?style=for-the-badge&logo=Discord">
@@ -33,45 +36,51 @@ An Xcode-inspired code editor view written in Swift powered by tree-sitter for [
3336

3437
This package is fully documented [here](https://codeeditapp.github.io/CodeEditSourceEditor/documentation/codeeditsourceeditor/).
3538

36-
## Usage
39+
## Usage (SwiftUI)
3740

3841
```swift
3942
import CodeEditSourceEditor
4043

4144
struct ContentView: View {
4245

4346
@State var text = "let x = 1.0"
47+
48+
/// Automatically updates with cursor positions, or update the binding to set the user's cursors.
49+
@State var cursorPositions: [CursorPosition] = []
50+
51+
/// Configure the editor's appearance, features, and editing behavior...
4452
@State var theme = EditorTheme(...)
4553
@State var font = NSFont.monospacedSystemFont(ofSize: 11, weight: .regular)
46-
@State var tabWidth = 4
47-
@State var lineHeight = 1.2
48-
@State var editorOverscroll = 0.3
54+
@State var indentOption = .spaces(count: 4)
4955

5056
var body: some View {
51-
CodeEditSourceEditor(
57+
SourceEditor(
5258
$text,
53-
language: .swift,
54-
theme: $theme,
55-
font: $font,
56-
tabWidth: $tabWidth,
57-
lineHeight: $lineHeight,
58-
editorOverscroll: $editorOverscroll
59+
language: language,
60+
// Tons of customization options, with good defaults to get started quickly.
61+
configuration: SourceEditorConfiguration(
62+
appearance: .init(theme: theme, font: font),
63+
behavior: .init(indentOption: indentOption)
64+
),
65+
cursorPositions: $cursorPositions
5966
)
6067
}
6168
}
6269
```
6370

71+
An AppKit API is also available.
72+
6473
## Currently Supported Languages
6574

6675
See this issue https://github.com/CodeEditApp/CodeEditLanguages/issues/10 on `CodeEditLanguages` for more information on supported languages.
6776

6877
## Dependencies
6978

70-
Special thanks to [Matt Massicotte](https://twitter.com/mattie) for the great work he's done!
79+
Special thanks to [Matt Massicotte](https://bsky.app/profile/massicotte.org) for the great work he's done!
7180

7281
| Package | Source | Author |
7382
| :- | :- | :- |
74-
| `SwiftTreeSitter` | [GitHub](https://github.com/ChimeHQ/SwiftTreeSitter) | [Matt Massicotte](https://twitter.com/mattie) |
83+
| `SwiftTreeSitter` | [GitHub](https://github.com/ChimeHQ/SwiftTreeSitter) | [Matt Massicotte](https://bsky.app/profile/massicotte.org) |
7584

7685
## License
7786

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public class TextViewController: NSViewController {
174174

175175
// MARK: Init
176176

177-
init(
177+
public init(
178178
string: String,
179179
language: CodeLanguage,
180180
config: SourceEditorConfiguration,

Sources/CodeEditSourceEditor/Documentation.docc/CodeEditSourceEditor.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

Sources/CodeEditSourceEditor/Documentation.docc/Documentation.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ See this issue [CodeEditLanguages#10](https://github.com/CodeEditApp/CodeEditLan
2020

2121
## Dependencies
2222

23-
Special thanks to [Matt Massicotte](https://twitter.com/mattie) for the great work he's done!
23+
Special thanks to [Matt Massicotte](https://bsky.app/profile/massicotte.org) for the great work he's done!
2424

2525
| Package | Source | Author |
2626
| :- | :- | :- |
27-
| `SwiftTreeSitter` | [GitHub](https://github.com/ChimeHQ/SwiftTreeSitter) | [Matt Massicotte](https://twitter.com/mattie) |
27+
| `SwiftTreeSitter` | [GitHub](https://github.com/ChimeHQ/SwiftTreeSitter) | [Matt Massicotte](https://bsky.app/profile/massicotte.org) |
2828

2929
## License
3030

@@ -34,9 +34,10 @@ Licensed under the [MIT license](https://github.com/CodeEditApp/CodeEdit/blob/ma
3434

3535
### Text View
3636

37-
- ``CodeEditSourceEditor/CodeEditSourceEditor``
38-
- ``TextViewController``
39-
- ``GutterView``
37+
- ``SourceEditor`` The SwiftUI API for the source editor.
38+
- ``SourceEditorConfiguration`` Customize the source editor's behavior, layout, appearance, etc.
39+
- ``TextViewController`` The AppKit view controller for the source editor.
40+
- ``GutterView`` A view used to display line numbers and folding regions.
4041

4142
### Themes
4243

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ``SourceEditor``
2+
3+
## Usage
4+
5+
CodeEditSourceEditor provides two APIs for creating an editor: SwiftUI and AppKit.
6+
7+
#### SwiftUI
8+
9+
```swift
10+
import CodeEditSourceEditor
11+
12+
struct ContentView: View {
13+
14+
@State var text = "let x = 1.0"
15+
// For large documents use (avoids SwiftUI inneficiency)
16+
// var text: NSTextStorage
17+
18+
/// Automatically updates with cursor positions, or update the binding to set the user's cursors.
19+
@State var cursorPositions: [CursorPosition] = []
20+
21+
/// Configure the editor's appearance, features, and editing behavior...
22+
@State var theme = EditorTheme(...)
23+
@State var font = NSFont.monospacedSystemFont(ofSize: 11, weight: .regular)
24+
@State var indentOption = .spaces(count: 4)
25+
@State var editorOverscroll = 0.3
26+
@State var showMinimap = true
27+
28+
var body: some View {
29+
SourceEditor(
30+
$text,
31+
language: language,
32+
configuration: SourceEditorConfiguration(
33+
appearance: .init(theme: theme, font: font),
34+
behavior: .init(indentOption: indentOption),
35+
layout: .init(editorOverscroll: editorOverscroll),
36+
peripherals: .init(showMinimap: showMinimap)
37+
),
38+
cursorPositions: $cursorPositions
39+
)
40+
}
41+
}
42+
```
43+
44+
#### AppKit
45+
46+
```swift
47+
var theme = EditorTheme(...)
48+
var font = NSFont.monospacedSystemFont(ofSize: 11, weight: .regular)
49+
var indentOption = .spaces(count: 4)
50+
var editorOverscroll = 0.3
51+
var showMinimap = true
52+
53+
let editorController = TextViewController(
54+
string: "let x = 10;",
55+
language: .swift,
56+
config: SourceEditorConfiguration(
57+
appearance: .init(theme: theme, font: font),
58+
behavior: .init(indentOption: indentOption),
59+
layout: .init(editorOverscroll: editorOverscroll),
60+
peripherals: .init(showMinimap: showMinimap)
61+
),
62+
cursorPositions: [CursorPosition(line: 0, column: 0)],
63+
highlightProviders: [], // Use the tree-sitter syntax highlighting provider by default
64+
undoManager: nil,
65+
coordinators: [] // Optionally inject editing behavior or other plugins.
66+
)
67+
```
68+

Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Appearance.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import AppKit
99

1010
extension SourceEditorConfiguration {
11+
/// Configure the appearance of the editor. Font, theme, line height, etc.
1112
public struct Appearance: Equatable {
1213
/// The theme for syntax highlighting.
1314
public var theme: EditorTheme
@@ -38,6 +39,20 @@ extension SourceEditorConfiguration {
3839
/// See ``BracketPairEmphasis`` for more information. Defaults to `.flash`.
3940
public var bracketPairEmphasis: BracketPairEmphasis? = .flash
4041

42+
/// Create a new appearance configuration object.
43+
/// - Parameters:
44+
/// - theme: The theme for syntax highlighting.
45+
/// - useThemeBackground: Determines whether the editor uses the theme's background color, or a transparent
46+
/// background color.
47+
/// - font: The default font.
48+
/// - lineHeightMultiple: The line height multiplier (e.g. `1.2`).
49+
/// - letterSpacing: The amount of space to use between letters, as a percent. Eg: `1.0` = no space, `1.5`
50+
/// = 1/2 of a character's width between characters, etc. Defaults to `1.0`.
51+
/// - wrapLines: Whether lines wrap to the width of the editor.
52+
/// - useSystemCursor: If true, uses the system cursor on `>=macOS 14`.
53+
/// - tabWidth: The visual tab width in number of spaces.
54+
/// - bracketPairEmphasis: The type of highlight to use to highlight bracket pairs. See
55+
/// ``BracketPairEmphasis`` for more information. Defaults to `.flash`.
4156
public init(
4257
theme: EditorTheme,
4358
useThemeBackground: Bool = true,

Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@
77

88
import AppKit
99

10+
/// Configuration object for the ``SourceEditor``. Determines appearance, behavior, layout and what features are
11+
/// enabled (peripherals).
12+
///
13+
/// To update the configuration, update the ``TextViewController/configuration`` property, or pass a value to the
14+
/// ``SourceEditor`` SwiftUI API. Both methods will call the `didSetOnController` method on this type, which will
15+
/// update the text controller as necessary for the new configuration.
1016
public struct SourceEditorConfiguration: Equatable {
17+
/// Configure the appearance of the editor. Font, theme, line height, etc.
1118
public var appearance: Appearance
19+
/// Configure the behavior of the editor. Indentation, edit-ability, select-ability, etc.
1220
public var behavior: Behavior
13-
public var peripherals: Peripherals
21+
/// Configure the layout of the editor. Content insets, etc.
1422
public var layout: Layout
23+
/// Configure enabled features on the editor. Gutter (line numbers), minimap, etc.
24+
public var peripherals: Peripherals
1525

26+
/// Create a new configuration object.
27+
/// - Parameters:
28+
/// - appearance: Configure the appearance of the editor. Font, theme, line height, etc.
29+
/// - behavior: Configure the behavior of the editor. Indentation, edit-ability, select-ability, etc.
30+
/// - layout: Configure the layout of the editor. Content insets, etc.
31+
/// - peripherals: Configure enabled features on the editor. Gutter (line numbers), minimap, etc.
1632
public init(
1733
appearance: Appearance,
1834
behavior: Behavior = .init(),
@@ -25,6 +41,13 @@ public struct SourceEditorConfiguration: Equatable {
2541
self.peripherals = peripherals
2642
}
2743

44+
/// Update the controller for a new configuration object.
45+
///
46+
/// This object is the new one, the old one is passed in as an optional, assume that it's the first setup
47+
/// when `oldConfig` is `nil`.
48+
///
49+
/// This method should try to update a minimal number of properties as possible by checking for changes
50+
/// before updating.
2851
@MainActor
2952
func didSetOnController(controller: TextViewController, oldConfig: SourceEditorConfiguration?) {
3053
appearance.didSetOnController(controller: controller, oldConfig: oldConfig?.appearance)

0 commit comments

Comments
 (0)