-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathHelper.swift
More file actions
52 lines (44 loc) · 1.55 KB
/
Helper.swift
File metadata and controls
52 lines (44 loc) · 1.55 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import AppKit
struct LocationStrategyHelper {
/// `lineNumber` is 0-based
///
/// - Parameters:
/// - length: If specified, use this length instead of the actual line length. Useful when you want to get the exact line height and y that ignores the unwrappded lines.
static func getLineFrame(
_ lineNumber: Int,
in editor: AXUIElement,
with lines: [String],
length: Int? = nil
) -> 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
}
let rangeLength: Int = {
if let length {
return min(length, lines[lineNumber].count)
} else {
return lines[lineNumber].count
}
}()
var range = CFRange(location: characterPosition, length: rangeLength)
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
}
}