-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathHelper.swift
More file actions
36 lines (29 loc) · 1.09 KB
/
Helper.swift
File metadata and controls
36 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import AppKit
struct LocationStrategyHelper {
/// `lineNumber` is 0-based
static func getLineFrame(_ lineNumber: Int, in editor: AXUIElement, with lines: [String]) -> CGRect? {
guard editor.isSourceEditor,
lineNumber < lines.count && lineNumber >= 0
else {
return nil
}
var characterPosition = 0
for i in 0..<lineNumber {
// +1 for newline character
characterPosition += lines[i].count + 1
}
var range = CFRange(location: characterPosition, length: lines[lineNumber].count)
guard let rangeValue = AXValueCreate(AXValueType.cfRange, &range) else {
return nil
}
guard let boundsValue: AXValue = try? editor.copyParameterizedValue(
key: kAXBoundsForRangeParameterizedAttribute,
parameters: rangeValue
) else {
return nil
}
var rect = CGRect.zero
let success = AXValueGetValue(boundsValue, .cgRect, &rect)
return success ? rect : nil
}
}