Skip to content

Commit f1df981

Browse files
committed
Resolve Cursors Method, Show Completions On CMD
1 parent fefc805 commit f1df981

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

Sources/CodeEditSourceEditor/Controller/TextViewController+Cursor.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ extension TextViewController {
4747
continue
4848
}
4949
let column = (selectedRange.range.location - linePosition.range.location) + 1
50-
let row = linePosition.index + 1
51-
positions.append(CursorPosition(range: selectedRange.range, line: row, column: column))
50+
let line = linePosition.index + 1
51+
positions.append(CursorPosition(range: selectedRange.range, line: line, column: column))
5252
}
5353

5454
isPostingCursorNotification = true
@@ -58,5 +58,34 @@ extension TextViewController {
5858
coordinator.textViewDidChangeSelection(controller: self, newPositions: cursorPositions)
5959
}
6060
isPostingCursorNotification = false
61+
62+
if let completionDelegate = completionDelegate, let position = cursorPositions.first {
63+
SuggestionController.shared.cursorsUpdated(textView: self, delegate: completionDelegate, position: position)
64+
}
65+
}
66+
67+
/// Fills out all properties on the given cursor position if it's missing either the range or line/column
68+
/// information.
69+
func resolveCursorPosition(_ position: CursorPosition) -> CursorPosition? {
70+
var range = position.range
71+
if range == .notFound {
72+
guard position.line > 0, position.column > 0,
73+
let linePosition = textView.layoutManager.textLineForIndex(position.line - 1) else {
74+
return nil
75+
}
76+
range = NSRange(location: linePosition.range.location + position.column, length: 0)
77+
}
78+
79+
var line = position.line
80+
var column = position.column
81+
if position.line <= 0 || position.column <= 0 {
82+
guard range != .notFound, let linePosition = textView.layoutManager.textLineForOffset(range.location) else {
83+
return nil
84+
}
85+
column = (range.location - linePosition.range.location) + 1
86+
line = linePosition.index + 1
87+
}
88+
89+
return CursorPosition(range: range, line: line, column: column)
6190
}
6291
}

Sources/CodeEditSourceEditor/Controller/TextViewController+Lifecycle.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ extension TextViewController {
229229
self.findViewController?.showFindPanel()
230230
return nil
231231
case (0, "\u{1b}"): // Escape key
232-
self.findViewController?.hideFindPanel()
233-
return nil
234-
case (controlKey, " "):
235-
let autocompleteCoordinators = textCoordinators.map {
236-
($0.val as? AutoCompleteCoordinatorProtocol)?.showAutocompleteWindow()
232+
if findViewController?.viewModel.isShowingFindPanel == true {
233+
self.findViewController?.hideFindPanel()
234+
return nil
237235
}
238-
return nil
236+
// Attempt to show completions otherwise
237+
return handleShowCompletions(event)
238+
case (controlKey, " "):
239+
return handleShowCompletions(event)
239240
case (_, _):
240241
return event
241242
}
@@ -258,4 +259,16 @@ extension TextViewController {
258259
}
259260
return nil
260261
}
262+
263+
private func handleShowCompletions(_ event: NSEvent) -> NSEvent? {
264+
if let completionDelegate = self.completionDelegate, let cursorPosition = cursorPositions.first {
265+
SuggestionController.shared.showCompletions(
266+
textView: self,
267+
delegate: completionDelegate,
268+
cursorPosition: cursorPosition
269+
)
270+
return nil
271+
}
272+
return event
273+
}
261274
}

0 commit comments

Comments
 (0)