-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathSuggestionViewModel.swift
More file actions
196 lines (169 loc) · 5.8 KB
/
SuggestionViewModel.swift
File metadata and controls
196 lines (169 loc) · 5.8 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
//
// SuggestionViewModel.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 7/22/25.
//
import AppKit
@MainActor
final class SuggestionViewModel: ObservableObject {
/// The items to be displayed in the window
@Published var items: [CodeSuggestionEntry] = []
@Published var selectedIndex: Int = 0
@Published var themeBackground: NSColor = .windowBackgroundColor
@Published var themeTextColor: NSColor = .labelColor
var itemsRequestTask: Task<Void, Never>?
weak var activeTextView: TextViewController?
weak var delegate: CodeSuggestionDelegate?
private var cursorPosition: CursorPosition?
private var syntaxHighlightedCache: [Int: NSAttributedString] = [:]
var selectedItem: CodeSuggestionEntry? {
guard selectedIndex >= 0, selectedIndex < items.count else { return nil }
return items[selectedIndex]
}
func moveUp() {
guard selectedIndex > 0 else { return }
selectedIndex -= 1
notifySelection()
}
func moveDown() {
guard selectedIndex < items.count - 1 else { return }
selectedIndex += 1
notifySelection()
}
private func notifySelection() {
if let item = selectedItem {
delegate?.completionWindowDidSelect(item: item)
}
}
func updateTheme(from textView: TextViewController) {
themeTextColor = textView.theme.text.color
switch textView.systemAppearance {
case .aqua:
let color = textView.theme.background
if color != .clear {
themeBackground = NSColor(
red: color.redComponent * 0.95,
green: color.greenComponent * 0.95,
blue: color.blueComponent * 0.95,
alpha: 1.0
)
} else {
themeBackground = .windowBackgroundColor
}
case .darkAqua:
themeBackground = textView.theme.background
default:
break
}
}
func showCompletions(
textView: TextViewController,
delegate: CodeSuggestionDelegate,
cursorPosition: CursorPosition,
showWindowOnParent: @escaping @MainActor (NSWindow, NSRect) -> Void
) {
self.activeTextView = nil
self.delegate = nil
itemsRequestTask?.cancel()
guard let targetParentWindow = textView.view.window else { return }
self.activeTextView = textView
self.delegate = delegate
itemsRequestTask = Task {
defer { itemsRequestTask = nil }
do {
guard let completionItems = await delegate.completionSuggestionsRequested(
textView: textView,
cursorPosition: cursorPosition
) else {
return
}
try Task.checkCancellation()
try await MainActor.run {
try Task.checkCancellation()
guard let cursorPosition = textView.resolveCursorPosition(completionItems.windowPosition),
let cursorRect = textView.textView.layoutManager.rectForOffset(
cursorPosition.range.location
),
let cursorRect = textView.view.window?.convertToScreen(
textView.textView.convert(cursorRect, to: nil)
) else {
return
}
self.items = completionItems.items
self.selectedIndex = 0
self.syntaxHighlightedCache = [:]
self.notifySelection()
showWindowOnParent(targetParentWindow, cursorRect)
}
} catch {
return
}
}
}
func cursorsUpdated(
textView: TextViewController,
delegate: CodeSuggestionDelegate,
position: CursorPosition,
close: () -> Void
) {
guard itemsRequestTask == nil else { return }
if activeTextView !== textView {
close()
return
}
guard let newItems = delegate.completionOnCursorMove(
textView: textView,
cursorPosition: position
),
!newItems.isEmpty else {
close()
return
}
items = newItems
selectedIndex = 0
syntaxHighlightedCache = [:]
notifySelection()
}
func didSelect(item: CodeSuggestionEntry) {
delegate?.completionWindowDidSelect(item: item)
}
func applySelectedItem(item: CodeSuggestionEntry, window: NSWindow?) {
guard let activeTextView else {
return
}
self.delegate?.completionWindowApplyCompletion(
item: item,
textView: activeTextView,
cursorPosition: activeTextView.cursorPositions.first
)
window?.close()
}
func willClose() {
itemsRequestTask?.cancel()
itemsRequestTask = nil
items.removeAll()
selectedIndex = 0
activeTextView = nil
delegate = nil
}
func syntaxHighlights(forIndex index: Int) -> NSAttributedString? {
if let cached = syntaxHighlightedCache[index] {
return cached
}
if let sourcePreview = items[index].sourcePreview,
let theme = activeTextView?.theme,
let font = activeTextView?.font,
let language = activeTextView?.language {
let string = TreeSitterClient.quickHighlight(
string: sourcePreview,
theme: theme,
font: font,
language: language
)
syntaxHighlightedCache[index] = string
return string
}
return nil
}
}