|
1 | 1 | import ActiveApplicationMonitor |
2 | 2 | import AppKit |
3 | | -import SuggestionModel |
4 | 3 | import Environment |
5 | 4 | import Preferences |
6 | 5 | import SuggestionInjector |
| 6 | +import SuggestionModel |
| 7 | +import XcodeInspector |
7 | 8 | import XPCShared |
8 | 9 |
|
9 | 10 | /// It's used to run some commands without really triggering the menu bar item. |
@@ -54,6 +55,19 @@ struct PseudoCommandHandler { |
54 | 55 | } |
55 | 56 | } |
56 | 57 |
|
| 58 | + func invalidateRealtimeSuggestionsIfNeeded(sourceEditor: SourceEditor) async { |
| 59 | + guard let fileURL = try? await Environment.fetchCurrentFileURL(), |
| 60 | + let (_, filespace) = try? await Workspace |
| 61 | + .fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL) else { return } |
| 62 | + |
| 63 | + if await !filespace.validateSuggestions( |
| 64 | + lines: sourceEditor.content.lines, |
| 65 | + cursorPosition: sourceEditor.content.cursorPosition |
| 66 | + ) { |
| 67 | + PresentInWindowSuggestionPresenter().discardSuggestion(fileURL: fileURL) |
| 68 | + } |
| 69 | + } |
| 70 | + |
57 | 71 | func rejectSuggestions() async { |
58 | 72 | let handler = WindowBaseCommandHandler() |
59 | 73 | _ = try? await handler.rejectSuggestion(editor: .init( |
@@ -214,7 +228,7 @@ extension PseudoCommandHandler { |
214 | 228 | let content = focusElement.value |
215 | 229 | let split = content.breakLines() |
216 | 230 | let range = convertRangeToCursorRange(selectionRange, in: content) |
217 | | - return (content, split, [range], range.end) |
| 231 | + return (content, split, [range], range.start) |
218 | 232 | } |
219 | 233 |
|
220 | 234 | func getFileURL() async -> URL? { |
@@ -289,10 +303,10 @@ extension PseudoCommandHandler { |
289 | 303 | var countE = 0 |
290 | 304 | var cursorRange = CursorRange(start: .zero, end: .outOfScope) |
291 | 305 | for (i, line) in lines.enumerated() { |
292 | | - if countS <= range.lowerBound && range.lowerBound < countS + line.count { |
| 306 | + if countS <= range.lowerBound, range.lowerBound < countS + line.count { |
293 | 307 | cursorRange.start = .init(line: i, character: range.lowerBound - countS) |
294 | 308 | } |
295 | | - if countE <= range.upperBound && range.upperBound < countE + line.count { |
| 309 | + if countE <= range.upperBound, range.upperBound < countE + line.count { |
296 | 310 | cursorRange.end = .init(line: i, character: range.upperBound - countE) |
297 | 311 | break |
298 | 312 | } |
@@ -321,3 +335,4 @@ public extension String { |
321 | 335 | return all |
322 | 336 | } |
323 | 337 | } |
| 338 | + |
0 commit comments