@@ -14,10 +14,12 @@ class AutoCompleteCoordinator: TextViewCoordinator {
1414 private weak var textViewController : TextViewController ?
1515 private var localEventMonitor : Any ?
1616
17- private let itemBoxController = ItemBoxWindowController ( )
17+ private var itemBoxController : ItemBoxWindowController ?
1818
1919 func prepareCoordinator( controller: TextViewController ) {
20- itemBoxController. close ( )
20+ itemBoxController = ItemBoxWindowController ( )
21+ itemBoxController? . delegate = self
22+ itemBoxController? . close ( )
2123 self . textViewController = controller
2224
2325 localEventMonitor = NSEvent . addLocalMonitorForEvents ( matching: . keyDown) { event in
@@ -31,84 +33,79 @@ class AutoCompleteCoordinator: TextViewCoordinator {
3133 }
3234
3335 func showAutocompleteWindow( ) {
34- guard let cursorPos = textViewController? . cursorPositions. last ,
36+ guard let cursorPos = textViewController? . cursorPositions. first ,
3537 let textView = textViewController? . textView,
3638 let window = NSApplication . shared. keyWindow,
39+ let itemBoxController = itemBoxController,
3740 !itemBoxController. isVisible
3841 else {
3942 return
4043 }
4144
45+ @Service var lspService : LSPService
46+
47+ // lspService.
48+
4249 itemBoxController. items = [
43- CompletionItem ( label: " item1 " , kind: . class) ,
44- CompletionItem ( label: " item2 " , kind: . enum) ,
45- CompletionItem ( label: " item3 " , kind: . function) ,
46- CompletionItem ( label: " item4 " , kind: . color) ,
47- CompletionItem ( label: " item5 " , kind: . constant) ,
48- CompletionItem ( label: " item6 " , kind: . constructor) ,
49- CompletionItem ( label: " item7 " , kind: . enumMember) ,
50- CompletionItem ( label: " item8 " , kind: . field) ,
51- CompletionItem ( label: " item9 " , kind: . file) ,
52- CompletionItem ( label: " item10 " , kind: . folder) ,
53- CompletionItem ( label: " item11 " , kind: . snippet) ,
54- CompletionItem ( label: " item12 " , kind: . reference) ,
50+ CompletionItem ( label: " CETable " , kind: . class) ,
51+ CompletionItem ( label: " CETask " , kind: . enum) ,
52+ CompletionItem ( label: " CETarget " , kind: . function) ,
53+ CompletionItem ( label: " CEItem " , kind: . color) ,
54+ CompletionItem ( label: " tableView " , kind: . constant) ,
55+ CompletionItem ( label: " itemBoxController " , kind: . constructor) ,
56+ CompletionItem ( label: " showAutocompleteWindow " , kind: . enumMember) ,
57+ CompletionItem ( label: " NSApplication " , kind: . field) ,
58+ CompletionItem ( label: " CECell " , kind: . file) ,
59+ CompletionItem ( label: " Item10 " , kind: . folder) ,
60+ CompletionItem ( label: " Item11 " , kind: . snippet) ,
61+ CompletionItem ( label: " Item12 " , kind: . reference) ,
5562 ]
5663
57- // Reset the size of the window
58- let windowSize = ItemBoxWindowController . DEFAULT_SIZE
59- itemBoxController. window? . setContentSize ( windowSize)
60-
6164 let cursorRect = textView. firstRect ( forCharacterRange: cursorPos. range, actualRange: nil )
62- let screenFrame = window. screen!. visibleFrame
63- let padding : CGFloat = 22
64- var autocompleteWindowOrigin = NSPoint (
65- x: cursorRect. origin. x,
66- y: cursorRect. origin. y
67- )
68-
69- // Keep the horizontal position within the screen and some padding
70- let minX = screenFrame. minX + padding
71- let maxX = screenFrame. maxX - windowSize. width - padding
72-
73- if autocompleteWindowOrigin. x < minX {
74- autocompleteWindowOrigin. x = minX
75- } else if autocompleteWindowOrigin. x > maxX {
76- autocompleteWindowOrigin. x = maxX
77- }
78-
79- // Check if the window will go below the screen
80- // We determine whether the window drops down or upwards by choosing which
81- // corner of the window we will position: `setFrameOrigin` or `setFrameTopLeftPoint`
82- if autocompleteWindowOrigin. y - windowSize. height < screenFrame. minY {
83- // If the cursor itself if below the screen, then position the window
84- // at the bottom of the screen with some padding
85- if autocompleteWindowOrigin. y < screenFrame. minY {
86- autocompleteWindowOrigin. y = screenFrame. minY + padding
87- } else {
88- // Place above the cursor
89- autocompleteWindowOrigin. y += cursorRect. height
90- }
91-
92- itemBoxController. window? . setFrameOrigin ( autocompleteWindowOrigin)
93- } else {
94- // If the window goes above the screen, position it below the screen with padding
95- let maxY = screenFrame. maxY - padding
96- if autocompleteWindowOrigin. y > maxY {
97- autocompleteWindowOrigin. y = maxY
98- }
99-
100- itemBoxController. window? . setFrameTopLeftPoint ( autocompleteWindowOrigin)
101- }
102-
65+ itemBoxController. constrainWindowToScreenEdges ( cursorRect: cursorRect)
10366 itemBoxController. showWindow ( attachedTo: window)
10467 }
10568
10669 deinit {
107- print ( " Destroyed AutoCompleteCoordinator " )
108- itemBoxController. close ( )
70+ itemBoxController? . close ( )
10971 if let localEventMonitor = localEventMonitor {
11072 NSEvent . removeMonitor ( localEventMonitor)
11173 self . localEventMonitor = nil
11274 }
11375 }
11476}
77+
78+ extension MarkupContent {
79+ public init ( kind: MarkupKind , value: String ) {
80+ do {
81+ let dictionary : [ String : Any ] = [ " kind " : kind. rawValue, " value " : value]
82+ let data = try JSONSerialization . data ( withJSONObject: dictionary)
83+ self = try JSONDecoder ( ) . decode ( MarkupContent . self, from: data)
84+ } catch {
85+ print ( " Failed to create MarkupContent: \( error) " )
86+ // swiftlint:disable:next force_try
87+ self = try ! JSONDecoder ( ) . decode ( MarkupContent . self, from: """
88+ { " kind " : " plaintext " , " value " : " " }
89+ """ . data ( using: . utf8) !)
90+ }
91+ }
92+ }
93+
94+ extension AutoCompleteCoordinator : ItemBoxDelegate {
95+ func applyCompletionItem( _ item: CompletionItem ) {
96+ guard let cursorPos = textViewController? . cursorPositions. first else {
97+ return
98+ }
99+
100+ do {
101+ let token = try textViewController? . treeSitterClient? . nodesAt ( range: cursorPos. range)
102+ guard let token = token? . first else {
103+ return
104+ }
105+ print ( " Token \( token) " )
106+ } catch {
107+ print ( " \( error) " )
108+ return
109+ }
110+ }
111+ }
0 commit comments