|
| 1 | +import ActiveApplicationMonitor |
| 2 | +import AppKit |
| 3 | +import CopilotModel |
| 4 | +import Environment |
| 5 | +import SuggestionInjector |
| 6 | +import XPCShared |
| 7 | + |
| 8 | +/// It's used to run some commands without really triggering the menu bar item. |
| 9 | +/// |
| 10 | +/// For example, we can use it to generate real-time suggestions without Apple Scripts. |
| 11 | +struct PseudoCommandHandler { |
| 12 | + func presentPreviousSuggestion() async { |
| 13 | + let handler = WindowBaseCommandHandler() |
| 14 | + _ = try? await handler.presentPreviousSuggestion(editor: .init( |
| 15 | + content: "", |
| 16 | + lines: [], |
| 17 | + uti: "", |
| 18 | + cursorPosition: .outOfScope, |
| 19 | + tabSize: 0, |
| 20 | + indentSize: 0, |
| 21 | + usesTabsForIndentation: false |
| 22 | + )) |
| 23 | + } |
| 24 | + |
| 25 | + func presentNextSuggestion() async { |
| 26 | + let handler = WindowBaseCommandHandler() |
| 27 | + _ = try? await handler.presentNextSuggestion(editor: .init( |
| 28 | + content: "", |
| 29 | + lines: [], |
| 30 | + uti: "", |
| 31 | + cursorPosition: .outOfScope, |
| 32 | + tabSize: 0, |
| 33 | + indentSize: 0, |
| 34 | + usesTabsForIndentation: false |
| 35 | + )) |
| 36 | + } |
| 37 | + |
| 38 | + func generateRealtimeSuggestions() async { |
| 39 | + guard let editor = await getEditorContent() else { |
| 40 | + try? await Environment.triggerAction("Prefetch Suggestions") |
| 41 | + return |
| 42 | + } |
| 43 | + let mode = PresentationMode( |
| 44 | + rawValue: UserDefaults.shared |
| 45 | + .integer(forKey: SettingsKey.suggestionPresentationMode) |
| 46 | + ) ?? .comment |
| 47 | + let handler: SuggestionCommandHandler = { |
| 48 | + switch mode { |
| 49 | + case .comment: |
| 50 | + return CommentBaseCommandHandler() |
| 51 | + case .floatingWidget: |
| 52 | + return WindowBaseCommandHandler() |
| 53 | + } |
| 54 | + }() |
| 55 | + _ = try? await handler.generateRealtimeSuggestions(editor: editor) |
| 56 | + } |
| 57 | + |
| 58 | + func rejectSuggestions() async { |
| 59 | + let handler = WindowBaseCommandHandler() |
| 60 | + _ = try? await handler.rejectSuggestion(editor: .init( |
| 61 | + content: "", |
| 62 | + lines: [], |
| 63 | + uti: "", |
| 64 | + cursorPosition: .outOfScope, |
| 65 | + tabSize: 0, |
| 66 | + indentSize: 0, |
| 67 | + usesTabsForIndentation: false |
| 68 | + )) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +private extension PseudoCommandHandler { |
| 73 | + func getFileContent() async -> (String, [String], CursorPosition)? { |
| 74 | + guard let xcode = ActiveApplicationMonitor.activeXcode else { return nil } |
| 75 | + let application = AXUIElementCreateApplication(xcode.processIdentifier) |
| 76 | + guard let focusElement = application.focusedElement, |
| 77 | + focusElement.description == "Source Editor" |
| 78 | + else { return nil } |
| 79 | + guard let selectionRange = focusElement.selectedTextRange else { return nil } |
| 80 | + let content = focusElement.value |
| 81 | + let split = content.breakLines() |
| 82 | + let selectedPosition = selectionRange.upperBound |
| 83 | + // find row and col from content at selected position |
| 84 | + var rowIndex = 0 |
| 85 | + var count = 0 |
| 86 | + var colIndex = 0 |
| 87 | + for (i, row) in split.enumerated() { |
| 88 | + if count + row.count > selectedPosition { |
| 89 | + rowIndex = i |
| 90 | + colIndex = selectedPosition - count |
| 91 | + break |
| 92 | + } |
| 93 | + count += row.count |
| 94 | + } |
| 95 | + return (content, split, CursorPosition(line: rowIndex, character: colIndex)) |
| 96 | + } |
| 97 | + |
| 98 | + func getFileURL() async -> URL? { |
| 99 | + try? await Environment.fetchCurrentFileURL() |
| 100 | + } |
| 101 | + |
| 102 | + @ServiceActor |
| 103 | + func getFilespace() async -> Filespace? { |
| 104 | + guard let fileURL = await getFileURL() else { return nil } |
| 105 | + for (_, workspace) in workspaces { |
| 106 | + if let space = workspace.filespaces[fileURL] { return space } |
| 107 | + } |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + @ServiceActor |
| 112 | + func getEditorContent() async -> EditorContent? { |
| 113 | + guard |
| 114 | + let filespace = await getFilespace(), |
| 115 | + let uti = filespace.uti, |
| 116 | + let tabSize = filespace.tabSize, |
| 117 | + let indentSize = filespace.indentSize, |
| 118 | + let usesTabsForIndentation = filespace.usesTabsForIndentation, |
| 119 | + let content = await getFileContent() |
| 120 | + else { return nil } |
| 121 | + return .init( |
| 122 | + content: content.0, |
| 123 | + lines: content.1, |
| 124 | + uti: uti, |
| 125 | + cursorPosition: content.2, |
| 126 | + tabSize: tabSize, |
| 127 | + indentSize: indentSize, |
| 128 | + usesTabsForIndentation: usesTabsForIndentation |
| 129 | + ) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +public extension String { |
| 134 | + /// Break a string into lines. |
| 135 | + func breakLines() -> [String] { |
| 136 | + let lines = split(separator: "\n", omittingEmptySubsequences: false) |
| 137 | + var all = [String]() |
| 138 | + for (index, line) in lines.enumerated() { |
| 139 | + if index == lines.endIndex - 1 { |
| 140 | + all.append(String(line)) |
| 141 | + } else { |
| 142 | + all.append(String(line) + "\n") |
| 143 | + } |
| 144 | + } |
| 145 | + return all |
| 146 | + } |
| 147 | +} |
0 commit comments