forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceEditor.swift
More file actions
144 lines (130 loc) · 4.71 KB
/
SourceEditor.swift
File metadata and controls
144 lines (130 loc) · 4.71 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import AppKit
import AXNotificationStream
import Foundation
import SuggestionModel
/// Representing a source editor inside Xcode.
public class SourceEditor {
public typealias Content = EditorInformation.SourceEditorContent
let runningApplication: NSRunningApplication
public let element: AXUIElement
/// The content of the source editor.
public var content: Content {
let content = element.value
let split = Self.breakLines(content)
let lineAnnotationElements = element.children.filter { $0.identifier == "Line Annotation" }
let lineAnnotations = lineAnnotationElements.map(\.description)
if let selectionRange = element.selectedTextRange {
let range = Self.convertRangeToCursorRange(selectionRange, in: content)
return .init(
content: content,
lines: split,
selections: [range],
cursorPosition: range.start,
lineAnnotations: lineAnnotations
)
}
return .init(
content: content,
lines: split,
selections: [],
cursorPosition: .outOfScope,
lineAnnotations: lineAnnotations
)
}
public init(runningApplication: NSRunningApplication, element: AXUIElement) {
self.runningApplication = runningApplication
self.element = element
}
/// Observe to changes in the source editor.
public func observe(notificationNames: String...) -> AXNotificationStream {
return AXNotificationStream(
app: runningApplication,
element: element,
notificationNames: notificationNames
)
}
/// Observe to changes in the source editor scroll view.
public func observeScrollView(notificationNames: String...) -> AXNotificationStream? {
guard let scrollView = element.parent else { return nil }
return AXNotificationStream(
app: runningApplication,
element: scrollView,
notificationNames: notificationNames
)
}
}
// MARK: - Helpers
public extension SourceEditor {
static func convertCursorRangeToRange(
_ cursorRange: CursorRange,
in lines: [String]
) -> CFRange {
var countS = 0
var countE = 0
var range = CFRange(location: 0, length: 0)
for (i, line) in lines.enumerated() {
if i == cursorRange.start.line {
countS = countS + cursorRange.start.character
range.location = countS
}
if i == cursorRange.end.line {
countE = countE + cursorRange.end.character
range.length = max(countE - range.location, 0)
break
}
countS += line.count
countE += line.count
}
return range
}
static func convertCursorRangeToRange(
_ cursorRange: CursorRange,
in content: String
) -> CFRange {
let lines = breakLines(content)
return convertCursorRangeToRange(cursorRange, in: lines)
}
static func convertRangeToCursorRange(
_ range: ClosedRange<Int>,
in lines: [String]
) -> CursorRange {
guard !lines.isEmpty else { return CursorRange(start: .zero, end: .zero) }
var countS = 0
var countE = 0
var cursorRange = CursorRange(start: .zero, end: .outOfScope)
for (i, line) in lines.enumerated() {
if countS <= range.lowerBound, range.lowerBound < countS + line.count {
cursorRange.start = .init(line: i, character: range.lowerBound - countS)
}
if countE <= range.upperBound, range.upperBound < countE + line.count {
cursorRange.end = .init(line: i, character: range.upperBound - countE)
break
}
countS += line.count
countE += line.count
}
if cursorRange.end == .outOfScope {
cursorRange.end = .init(line: lines.endIndex - 1, character: lines.last?.count ?? 0)
}
return cursorRange
}
static func convertRangeToCursorRange(
_ range: ClosedRange<Int>,
in content: String
) -> CursorRange {
let lines = breakLines(content)
return convertRangeToCursorRange(range, in: lines)
}
static func breakLines(_ string: String) -> [String] {
let lines = string.split(separator: "\n", omittingEmptySubsequences: false)
var all = [String]()
for (index, line) in lines.enumerated() {
if index == lines.endIndex - 1 {
all.append(String(line))
} else {
all.append(String(line) + "\n")
}
}
return all
}
}