@@ -68,6 +68,71 @@ struct PseudoCommandHandler {
6868 usesTabsForIndentation: false
6969 ) )
7070 }
71+
72+ func acceptSuggestion( ) async {
73+ if UserDefaults . shared. value ( for: \. acceptSuggestionWithAccessibilityAPI) {
74+ guard let xcode = ActiveApplicationMonitor . activeXcode else { return }
75+ let application = AXUIElementCreateApplication ( xcode. processIdentifier)
76+ guard let focusElement = application. focusedElement,
77+ focusElement. description == " Source Editor "
78+ else { return }
79+ guard let ( content, lines, cursorPosition) = await getFileContent ( ) else {
80+ PresentInWindowSuggestionPresenter ( )
81+ . presentErrorMessage ( " Unable to get file content. " )
82+ return
83+ }
84+ let handler = CommentBaseCommandHandler ( )
85+ do {
86+ guard let result = try await handler. acceptSuggestion ( editor: . init(
87+ content: content,
88+ lines: lines,
89+ uti: " " ,
90+ cursorPosition: cursorPosition,
91+ selections: [ ] ,
92+ tabSize: 0 ,
93+ indentSize: 0 ,
94+ usesTabsForIndentation: false
95+ ) ) else { return }
96+
97+ let oldPosition = focusElement. selectedTextRange
98+
99+ let error = AXUIElementSetAttributeValue (
100+ focusElement,
101+ kAXValueAttribute as CFString ,
102+ result. content as CFTypeRef
103+ )
104+
105+ if error != AXError . success {
106+ PresentInWindowSuggestionPresenter ( )
107+ . presentErrorMessage ( " Fail to set editor content. " )
108+ }
109+
110+ if let oldPosition {
111+ var range = CFRange (
112+ location: oldPosition. lowerBound,
113+ length: 0
114+ )
115+ if let value = AXValueCreate ( . cfRange, & range) {
116+ AXUIElementSetAttributeValue (
117+ focusElement,
118+ kAXSelectedTextRangeAttribute as CFString ,
119+ value
120+ )
121+ }
122+ }
123+
124+ } catch {
125+ PresentInWindowSuggestionPresenter ( ) . presentError ( error)
126+ }
127+ } else {
128+ do {
129+ try await Environment . triggerAction ( " Accept Suggestion " )
130+ return
131+ } catch {
132+ PresentInWindowSuggestionPresenter ( ) . presentError ( error)
133+ }
134+ }
135+ }
71136}
72137
73138private extension PseudoCommandHandler {
0 commit comments