Skip to content

Commit 345b10c

Browse files
committed
Apply suggestion cursor for the accessibility workaround
1 parent 98b5f8b commit 345b10c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,16 @@ struct PseudoCommandHandler {
107107
.presentErrorMessage("Fail to set editor content.")
108108
}
109109

110-
if let oldPosition {
110+
if let cursor = result.newCursor {
111+
var range = convertCursorPositionToRange(cursor, in: result.content)
112+
if let value = AXValueCreate(.cfRange, &range) {
113+
AXUIElementSetAttributeValue(
114+
focusElement,
115+
kAXSelectedTextRangeAttribute as CFString,
116+
value
117+
)
118+
}
119+
} else if let oldPosition {
111120
var range = CFRange(
112121
location: oldPosition.lowerBound,
113122
length: 0
@@ -199,6 +208,23 @@ private extension PseudoCommandHandler {
199208
usesTabsForIndentation: usesTabsForIndentation
200209
)
201210
}
211+
212+
// a function to convert CursorPosition(line:character:) into a Range(location:length) in given
213+
// content
214+
func convertCursorPositionToRange(
215+
_ cursorPosition: CursorPosition,
216+
in content: String
217+
) -> CFRange {
218+
let lines = content.breakLines()
219+
var count = 0
220+
for (i, line) in lines.enumerated() {
221+
if i == cursorPosition.line {
222+
return CFRange(location: count + cursorPosition.character, length: 0)
223+
}
224+
count += line.count
225+
}
226+
return CFRange(location: count, length: 0)
227+
}
202228
}
203229

204230
public extension String {

0 commit comments

Comments
 (0)