### Create a Custom Plugin Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/configuration.md Define a custom plugin by conforming to the STPlugin protocol and implementing the `setUp` method to handle events. ```swift class MyPlugin: STPlugin { typealias Coordinator = Void func setUp(context: any STPlugin.Context) { context.events .onDidChangeText { range, _ in // React to text change } } } ``` -------------------------------- ### Basic TextView Usage Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/TextView.md Example of initializing a SwiftUI ContentView with a TextView, binding its text and selection state. ```swift import STTextViewSwiftUI struct ContentView: View { @State private var text = AttributedString("Hello World") @State private var selection: NSRange? = nil var body: some View { TextView(text: $text, selection: $selection) } } ``` -------------------------------- ### Basic STGutterView Setup Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/STGutterView.md Configures a basic STTextView with line numbers enabled and customizes the appearance of the gutter view. ```swift let scrollView = STTextView.scrollableTextView() let textView = scrollView.documentView as! STTextView // Enable line numbers textView.showsLineNumbers = true // Customize gutter textView.gutterView?.font = NSFont.monospacedSystemFont(ofSize: 12, weight: .regular) textView.gutterView?.textColor = .systemGray textView.gutterView?.drawSeparator = true textView.gutterView?.highlightSelectedLine = true ``` -------------------------------- ### STTextView with Plugins Example Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/TextView.md Demonstrates how to integrate multiple STTextView plugins, such as SyntaxHighlightingPlugin and SpellCheckPlugin, into a SwiftUI view. Ensure STTextViewSwiftUI is imported. ```swift import STTextViewSwiftUI struct EditableView: View { @State private var text = AttributedString("Swift code here") var body: some View { let plugins: [any STPlugin] = [ SyntaxHighlightingPlugin(), SpellCheckPlugin() ] TextView( text: $text, options: [.wrapLines, .showLineNumbers], plugins: plugins ) .textViewFont(.monospacedSystemFont(ofSize: 14, weight: .regular)) } ``` -------------------------------- ### Getting STTextView Content Example Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/TextView.md Provides examples of how to retrieve the text content from an AttributedString variable bound to an STTextView, both as an AttributedString and as a plain String. ```swift let content: AttributedString = text let plainText: String = String(text.characters) ``` -------------------------------- ### STPlugin setUp Method Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/STPlugin.md Initializes the plugin when it's added to the text view. Use this for setting up event handlers, caching references, or initializing plugin state. ```swift func setUp(context: any Context) ``` ```swift class SyntaxHighlightingPlugin: STPlugin { func setUp(context: any STPlugin.Context) { context.events.onDidChangeText { range, _ in self.highlightText(in: range, textView: context.textView) } } } ``` -------------------------------- ### Custom Completion UI with STCompletionViewControllerProtocol Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/STCompletionItem.md Implement `STCompletionViewControllerProtocol` to provide a custom user interface for code completions. This example shows a basic setup for a custom view controller. ```swift class MyDelegate: STTextViewDelegate { func textViewCompletionViewController(_ textView: STTextView) -> any STCompletionViewControllerProtocol { MyCompletionViewController() } } class MyCompletionViewController: NSViewController, STCompletionViewControllerProtocol { var items: [any STCompletionItem] = [] { didSet { reloadData() } } var delegate: (any STCompletionViewControllerDelegate)? = nil var selectedItem: (any STCompletionItem)? { // Return currently selected item guard let selection = tableView.selectedRow, selection >= 0 else { return nil } return items[selection] } override func viewDidLoad() { super.viewDidLoad() setupUI() } func reloadData() { tableView.reloadData() } } ``` -------------------------------- ### STGutterView Setup with Markers and Delegate Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/api-reference/STGutterView.md Demonstrates setting up an STTextView with line numbers, assigning a delegate that conforms to STGutterViewDelegate, and adding a custom marker. ```swift class EditorDelegate: STTextViewDelegate, STGutterViewDelegate { func textViewGutterShouldAddMarker(_ gutter: STGutterView) -> Bool { true // Allow marker creation } } let textView = STTextView() textView.showsLineNumbers = true let delegate = EditorDelegate() textView.gutterView?.delegate = delegate // Add breakpoint marker on line 42 let marker = STGutterMarker( line: 42, view: NSImageView(image: NSImage(systemSymbolName: "circle.fill")!) ) textView.gutterView?.addMarker(marker) ``` -------------------------------- ### Initialize TextView with Options Source: https://github.com/krzyzanowskim/sttextview/blob/main/_autodocs/configuration.md Use this to initialize TextView with specific display options like line wrapping, line highlighting, and line number visibility. ```swift TextView( text: $text, selection: $selection, options: [ .wrapLines, .highlightSelectedLine, .showLineNumbers ] ) ```