Skip to content

Commit 46a7d67

Browse files
AutoCompleteCoordinator
1 parent 3ee6962 commit 46a7d67

File tree

5 files changed

+36
-31
lines changed

5 files changed

+36
-31
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// AutoCompleteCoordinatorProtocol.swift
3+
// CodeEditSourceEditor
4+
//
5+
// Created by Abe Malla on 4/8/25.
6+
//
7+
8+
import LanguageServerProtocol
9+
10+
public protocol AutoCompleteCoordinatorProtocol: TextViewCoordinator {
11+
func fetchCompletions() async throws -> [CompletionItem]
12+
func showAutocompleteWindow()
13+
}

Sources/CodeEditSourceEditor/CodeSuggestion/SuggestionController.swift

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public final class SuggestionController: NSWindowController {
4646

4747
let tableView = NSTableView()
4848
let scrollView = NSScrollView()
49-
let popover = NSPopover()
5049
/// Tracks when the window is placed above the cursor
5150
var isWindowAboveCursor = false
5251

@@ -82,9 +81,15 @@ public final class SuggestionController: NSWindowController {
8281
fatalError("init(coder:) has not been implemented")
8382
}
8483

84+
deinit {
85+
removeEventMonitors()
86+
}
87+
8588
/// Opens the window as a child of another window.
86-
public func showWindow(attachedTo parentWindow: NSWindow) {
87-
guard let window = window else { return }
89+
public func showWindow() {
90+
guard let window = window,
91+
let parentWindow = NSApplication.shared.keyWindow
92+
else { return }
8893

8994
parentWindow.addChildWindow(window, ordered: .above)
9095
window.orderFront(nil)
@@ -176,9 +181,9 @@ public final class SuggestionController: NSWindowController {
176181

177182
case 125, 126: // Down/Up Arrow
178183
self.tableView.keyDown(with: event)
179-
guard tableView.selectedRow >= 0 else { return event }
180-
let selectedItem = items[tableView.selectedRow]
181-
self.delegate?.onItemSelect(item: selectedItem)
184+
let row = tableView.selectedRow
185+
guard row >= 0, row < items.count else { return event }
186+
self.delegate?.onItemSelect(item: items[row])
182187
return nil
183188

184189
case 124: // Right Arrow
@@ -192,34 +197,13 @@ public final class SuggestionController: NSWindowController {
192197
guard tableView.selectedRow >= 0 else { return event }
193198
let selectedItem = items[tableView.selectedRow]
194199
self.delegate?.applyCompletionItem(item: selectedItem)
195-
self.close()
196200
return nil
197201

198202
default:
199203
return event
200204
}
201205
}
202206

203-
private func handleRightArrow() {
204-
guard let window = self.window,
205-
let selectedRow = tableView.selectedRowIndexes.first,
206-
selectedRow < items.count,
207-
!popover.isShown else {
208-
return
209-
}
210-
let rowRect = tableView.rect(ofRow: selectedRow)
211-
let rowRectInWindow = tableView.convert(rowRect, to: nil)
212-
let popoverPoint = NSPoint(
213-
x: window.frame.maxX,
214-
y: window.frame.minY + rowRectInWindow.midY
215-
)
216-
popover.show(
217-
relativeTo: NSRect(x: popoverPoint.x, y: popoverPoint.y, width: 1, height: 1),
218-
of: window.contentView!,
219-
preferredEdge: .maxX
220-
)
221-
}
222-
223207
private func resetScrollPosition() {
224208
guard let clipView = scrollView.contentView as? NSClipView else { return }
225209

@@ -246,8 +230,4 @@ public final class SuggestionController: NSWindowController {
246230
cursorPositionObserver = nil
247231
}
248232
}
249-
250-
deinit {
251-
removeEventMonitors()
252-
}
253233
}

Sources/CodeEditSourceEditor/CodeSuggestion/SuggestionControllerDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77

88
public protocol SuggestionControllerDelegate: AnyObject {
9+
var currentFilterText: String { get }
10+
911
func applyCompletionItem(item: CodeSuggestionEntry)
1012
func onClose()
1113
func onCompletion()

Sources/CodeEditSourceEditor/Controller/TextViewController+Lifecycle.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ extension TextViewController {
212212

213213
func handleCommand(event: NSEvent, modifierFlags: UInt) -> NSEvent? {
214214
let commandKey = NSEvent.ModifierFlags.command.rawValue
215+
let controlKey = NSEvent.ModifierFlags.control.rawValue
215216

216217
switch (modifierFlags, event.charactersIgnoringModifiers) {
217218
case (commandKey, "/"):
@@ -230,6 +231,12 @@ extension TextViewController {
230231
case (0, "\u{1b}"): // Escape key
231232
self.findViewController?.hideFindPanel()
232233
return nil
234+
case (controlKey, " "):
235+
// suggestionController.showWindow()
236+
let autocompleteCoordinators = textCoordinators.map {
237+
($0.val as? AutoCompleteCoordinatorProtocol)?.showAutocompleteWindow()
238+
}
239+
return nil
233240
case (_, _):
234241
return event
235242
}

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ public class TextViewController: NSViewController {
194194
)
195195
}
196196

197+
/// The `SuggestionController` lets us display the autocomplete items
198+
public lazy var suggestionController: SuggestionController = SuggestionController()
199+
197200
// MARK: Init
198201

199202
public init(

0 commit comments

Comments
 (0)