Skip to content

Commit 4f45ada

Browse files
committed
Add public jumpToDefinitionDelegate API
1 parent 2dd772f commit 4f45ada

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class TextViewController: NSViewController {
2424

2525
// MARK: - Views and Child VCs
2626

27-
// MARK: - Views and Child VCs
28-
2927
weak var findViewController: FindViewController?
3028

3129
internal(set) public var scrollView: NSScrollView!
@@ -89,6 +87,18 @@ public class TextViewController: NSViewController {
8987

9088
public weak var completionDelegate: CodeSuggestionDelegate?
9189

90+
/// A delegate object that responds to requests for jump to definition actions. see ``JumpToDefinitionDelegate``.
91+
/// - Note: The ``TextViewController`` keeps only a `weak` reference to this object. To function properly, ensure a
92+
/// strong reference to the delegate is kept *outside* of this variable.
93+
public var jumpToDefinitionDelegate: JumpToDefinitionDelegate? {
94+
get {
95+
jumpToDefinitionModel.delegate
96+
}
97+
set {
98+
jumpToDefinitionModel.delegate = newValue
99+
}
100+
}
101+
92102
// MARK: - Config Helpers
93103

94104
/// The font to use in the `textView`
@@ -177,7 +187,7 @@ public class TextViewController: NSViewController {
177187
/// This will be `nil` if another highlighter provider is passed to the source editor.
178188
internal(set) public var treeSitterClient: TreeSitterClient? {
179189
didSet {
180-
jumpToDefinitionModel?.treeSitterClient = treeSitterClient
190+
jumpToDefinitionModel.treeSitterClient = treeSitterClient
181191
}
182192
}
183193

@@ -186,7 +196,7 @@ public class TextViewController: NSViewController {
186196
/// Filters used when applying edits..
187197
var textFilters: [TextFormation.Filter] = []
188198

189-
var jumpToDefinitionModel: JumpToDefinitionModel?
199+
var jumpToDefinitionModel: JumpToDefinitionModel
190200

191201
var cancellables = Set<AnyCancellable>()
192202

@@ -214,7 +224,8 @@ public class TextViewController: NSViewController {
214224
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
215225
foldProvider: LineFoldProvider? = nil,
216226
undoManager: CEUndoManager? = nil,
217-
coordinators: [TextViewCoordinator] = []
227+
coordinators: [TextViewCoordinator] = [],
228+
jumpToDefinitionDelegate: JumpToDefinitionDelegate? = nil
218229
) {
219230
self.language = language
220231
self.configuration = configuration
@@ -223,9 +234,15 @@ public class TextViewController: NSViewController {
223234
self.foldProvider = foldProvider ?? LineIndentationFoldProvider()
224235
self._undoManager = undoManager
225236
self.invisibleCharactersCoordinator = InvisibleCharactersCoordinator(configuration: configuration)
237+
self.jumpToDefinitionModel = JumpToDefinitionModel(
238+
controller: nil,
239+
treeSitterClient: treeSitterClient,
240+
delegate: jumpToDefinitionDelegate
241+
)
226242

227243
super.init(nibName: nil, bundle: nil)
228244

245+
jumpToDefinitionModel.controller = self
229246
suggestionTriggerModel.controller = self
230247

231248
if let idx = highlightProviders.firstIndex(where: { $0 is TreeSitterClient }),
@@ -253,11 +270,6 @@ public class TextViewController: NSViewController {
253270
}
254271
self.textCoordinators = coordinators.map { WeakCoordinator($0) }
255272

256-
jumpToDefinitionModel = JumpToDefinitionModel(
257-
controller: self,
258-
treeSitterClient: treeSitterClient,
259-
delegate: nil
260-
)
261273
}
262274

263275
required init?(coder: NSCoder) {

Sources/CodeEditSourceEditor/Documentation.docc/SourceEditorView.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,28 @@ let editorController = TextViewController(
8686
cursorPositions: [CursorPosition(line: 0, column: 0)],
8787
highlightProviders: [], // Use the tree-sitter syntax highlighting provider by default
8888
undoManager: nil,
89-
coordinators: [] // Optionally inject editing behavior or other plugins.
89+
coordinators: [], // Optionally inject editing behavior or other plugins.
90+
jumpToDefinitionDelegate // Allow users to perform the 'jump to definition' using a delegate object.
9091
)
9192
```
9293

94+
To add the controller to your view, add it as a child view controller and add the editor's view to your view hierarchy.
95+
96+
```swift
97+
final class MyController: NSViewController {
98+
override func loadView() {
99+
super.loadView()
100+
let editorController: TextViewController = /**/
101+
102+
addChild(editorController)
103+
view.addSubview(editorController.view)
104+
editorController.view.viewDidMoveToSuperview()
105+
}
106+
}
107+
```
108+
109+
For more AppKit API options, see the documentation on ``TextViewController``.
110+
93111
## Topics
94112

95113
- ``SourceEditor``

Sources/CodeEditSourceEditor/JumpToDefinition/JumpToDefinitionModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class JumpToDefinitionModel {
2020
weak var delegate: JumpToDefinitionDelegate?
2121
weak var treeSitterClient: TreeSitterClient?
2222

23-
private weak var controller: TextViewController?
23+
weak var controller: TextViewController?
2424

2525
private(set) public var hoveredRange: NSRange?
2626

@@ -33,7 +33,7 @@ final class JumpToDefinitionModel {
3333
controller?.textView
3434
}
3535

36-
init(controller: TextViewController, treeSitterClient: TreeSitterClient?, delegate: JumpToDefinitionDelegate?) {
36+
init(controller: TextViewController?, treeSitterClient: TreeSitterClient?, delegate: JumpToDefinitionDelegate?) {
3737
self.controller = controller
3838
self.treeSitterClient = treeSitterClient
3939
self.delegate = delegate

0 commit comments

Comments
 (0)