Skip to content

Commit c15f77c

Browse files
committed
Support updating range in PseudoCommandHandler
1 parent ddd23b4 commit c15f77c

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct PseudoCommandHandler {
7070

7171
func acceptSuggestion() async {
7272
if UserDefaults.shared.value(for: \.acceptSuggestionWithAccessibilityAPI) {
73-
guard let xcode = ActiveApplicationMonitor.activeXcode else { return }
73+
guard let xcode = ActiveApplicationMonitor.activeXcode ?? ActiveApplicationMonitor.latestXcode else { return }
7474
let application = AXUIElementCreateApplication(xcode.processIdentifier)
7575
guard let focusElement = application.focusedElement,
7676
focusElement.description == "Source Editor"
@@ -108,9 +108,8 @@ struct PseudoCommandHandler {
108108
.presentErrorMessage("Fail to set editor content.")
109109
}
110110

111-
#warning("MUSTDO: handle range")
112-
if let cursor = result.newSelection?.start {
113-
var range = convertCursorPositionToRange(cursor, in: result.content)
111+
if let selection = result.newSelection {
112+
var range = convertCursorRangeToRange(selection, in: result.content)
114113
if let value = AXValueCreate(.cfRange, &range) {
115114
AXUIElementSetAttributeValue(
116115
focusElement,
@@ -219,21 +218,28 @@ private extension PseudoCommandHandler {
219218
)
220219
}
221220

222-
// a function to convert CursorPosition(line:character:) into a Range(location:length) in given
223-
// content
224-
func convertCursorPositionToRange(
225-
_ cursorPosition: CursorPosition,
221+
func convertCursorRangeToRange(
222+
_ cursorRange: CursorRange,
226223
in content: String
227224
) -> CFRange {
228225
let lines = content.breakLines()
229-
var count = 0
226+
var countS = 0
227+
var countE = 0
228+
var range = CFRange(location: 0, length: 0)
230229
for (i, line) in lines.enumerated() {
231-
if i == cursorPosition.line {
232-
return CFRange(location: count + cursorPosition.character, length: 0)
230+
if i == cursorRange.start.line {
231+
countS = countS + cursorRange.start.character
232+
range.location = countS
233+
}
234+
if i == cursorRange.end.line {
235+
countE = countE + cursorRange.end.character
236+
range.length = max(countE - range.location, 0)
237+
break
233238
}
234-
count += line.count
239+
countS += line.count
240+
countE += line.count
235241
}
236-
return CFRange(location: count, length: 0)
242+
return range
237243
}
238244
}
239245

0 commit comments

Comments
 (0)