forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpToDefinitionModel.swift
More file actions
197 lines (167 loc) · 6.62 KB
/
JumpToDefinitionModel.swift
File metadata and controls
197 lines (167 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// JumpToDefinitionModel.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 7/23/25.
//
import AppKit
import CodeEditTextView
/// Manages two things:
/// - Finding a range to hover when pressing `cmd` using tree-sitter.
/// - Utilizing the `JumpToDefinitionDelegate` object to perform a jump, providing it with ranges and
/// strings as necessary.
/// - Presenting a popover when multiple options exist to jump to.
@MainActor
final class JumpToDefinitionModel {
static let emphasisId = "jumpToDefinition"
weak var delegate: JumpToDefinitionDelegate?
weak var treeSitterClient: TreeSitterClient?
private weak var controller: TextViewController?
private(set) public var hoveredRange: NSRange?
private var hoverRequestTask: Task<Void, Never>?
private var jumpRequestTask: Task<Void, Never>?
private var currentLinks: [JumpToDefinitionLink]?
private var textView: TextView? {
controller?.textView
}
init(controller: TextViewController, treeSitterClient: TreeSitterClient?, delegate: JumpToDefinitionDelegate?) {
self.controller = controller
self.treeSitterClient = treeSitterClient
self.delegate = delegate
}
// MARK: - Tree Sitter
/// Query the tree-sitter client for a valid range to query for definitions.
/// - Parameter location: The current cursor location.
/// - Returns: A range that contains a potential identifier to look up.
private func findDefinitionRange(at location: Int) async -> NSRange? {
guard let nodes = try? await treeSitterClient?.nodesAt(location: location),
let node = nodes.first(where: { $0.node.nodeType?.contains("identifier") == true }) else {
cancelHover()
return nil
}
guard !Task.isCancelled else { return nil }
return node.node.range
}
// MARK: - Jump Action
/// Performs the jump action.
/// - Parameter location: The location to query the delegate for.
func performJump(at location: NSRange) {
jumpRequestTask?.cancel()
jumpRequestTask = Task {
currentLinks = nil
guard let controller,
let links = await delegate?.queryLinks(forRange: location, textView: controller),
!links.isEmpty else {
NSSound.beep()
if let textView {
BezelNotification.show(symbolName: "questionmark", over: textView)
}
return
}
if links.count == 1 {
let link = links[0]
if link.url != nil {
delegate?.openLink(link: link)
} else {
textView?.selectionManager.setSelectedRange(link.targetRange.range)
}
textView?.scrollSelectionToVisible()
} else {
presentLinkPopover(on: location, links: links)
}
cancelHover()
}
}
// MARK: - Link Popover
private func presentLinkPopover(on range: NSRange, links: [JumpToDefinitionLink]) {
let halfway = range.location + (range.length / 2)
let range = NSRange(location: halfway, length: 0)
guard let controller,
let position = controller.resolveCursorPosition(CursorPosition(range: range)) else {
return
}
currentLinks = links
SuggestionController.shared.showCompletions(
textView: controller,
delegate: self,
cursorPosition: position,
asPopover: true
)
}
// MARK: - Local Link
private func openLocalLink(link: JumpToDefinitionLink) {
guard let controller = controller, let range = controller.resolveCursorPosition(link.targetRange) else {
return
}
controller.textView.selectionManager.setSelectedRange(range.range)
controller.textView.scrollSelectionToVisible()
}
// MARK: - Mouse Interaction
func mouseHovered(windowCoordinates: CGPoint) {
guard delegate != nil,
let textViewCoords = textView?.convert(windowCoordinates, from: nil),
let location = textView?.layoutManager.textOffsetAtPoint(textViewCoords),
location < textView?.textStorage.length ?? 0 else {
cancelHover()
return
}
if hoveredRange?.contains(location) == false {
cancelHover()
}
hoverRequestTask?.cancel()
hoverRequestTask = Task {
guard let newRange = await findDefinitionRange(at: location) else { return }
updateHoveredRange(to: newRange)
}
}
func cancelHover() {
if (textView as? SourceEditorTextView)?.additionalCursorRects.isEmpty != true {
(textView as? SourceEditorTextView)?.additionalCursorRects = []
textView?.resetCursorRects()
}
guard hoveredRange != nil else { return }
hoveredRange = nil
hoverRequestTask?.cancel()
textView?.emphasisManager?.removeEmphases(for: Self.emphasisId)
}
private func updateHoveredRange(to newRange: NSRange) {
let rects = textView?.layoutManager.rectsFor(range: newRange).map { ($0, NSCursor.pointingHand) } ?? []
(textView as? SourceEditorTextView)?.additionalCursorRects = rects
textView?.resetCursorRects()
hoveredRange = newRange
textView?.emphasisManager?.removeEmphases(for: Self.emphasisId)
let color = textView?.selectionManager.selectionBackgroundColor ?? .selectedTextBackgroundColor
textView?.emphasisManager?.addEmphasis(
Emphasis(range: newRange, style: .outline( color: color, fill: true)),
for: Self.emphasisId
)
}
}
extension JumpToDefinitionModel: CodeSuggestionDelegate {
func completionSuggestionsRequested(
textView: TextViewController,
cursorPosition: CursorPosition
) async -> (windowPosition: CursorPosition, items: [CodeSuggestionEntry])? {
guard let links = currentLinks else { return nil }
defer { self.currentLinks = nil }
return (cursorPosition, links)
}
func completionOnCursorMove(
textView: TextViewController,
cursorPosition: CursorPosition
) -> [CodeSuggestionEntry]? {
nil
}
func completionWindowApplyCompletion(
item: CodeSuggestionEntry,
textView: TextViewController,
cursorPosition: CursorPosition?
) {
guard let link = item as? JumpToDefinitionLink else { return }
if link.url != nil {
delegate?.openLink(link: link)
} else {
openLocalLink(link: link)
}
}
}