For AppKit applications, use TextViewController to manage the editor. You can initialize it with content, language settings, and a SourceEditorConfiguration.
To display the editor:
- Initialize a
TextViewController. - Add it as a child view controller to your parent controller.
- Add the
editorController.view to your view hierarchy. - Call
editorController.view.viewDidMoveToSuperview() to ensure proper setup.
TextViewController supports advanced features like custom highlightProviders, undoManager integration, completionDelegate for code suggestions, and jumpToDefinitionDelegate.
// 1. Initialize the controller
let editorController = TextViewController(
string: "let x = 10;",
language: .swift,
config: SourceEditorConfiguration(
appearance: .init(theme: theme, font: font),
behavior: .init(indentOption: .spaces(count: 4)),
layout: .init(editorOverscroll: 0.3),
peripherals: .init(showMinimap: true)
),
cursorPositions: [CursorPosition(line: 0, column: 0)],
highlightProviders: [],
undoManager: nil,
coordinators: [],
completionDelegate: nil,
jumpToDefinitionDelegate: nil
)
// 2. Add to view hierarchy
final class MyController: NSViewController {
override func loadView() {
super.loadView()
addChild(editorController)
view.addSubview(editorController.view)
editorController.view.viewDidMoveToSuperview()
}
}