|
| 1 | +import AppKit |
| 2 | +import AXNotificationStream |
| 3 | +import Foundation |
| 4 | +import SuggestionModel |
| 5 | + |
| 6 | +/// Representing a source editor inside Xcode. |
| 7 | +public class SourceEditor { |
| 8 | + public struct Content { |
| 9 | + /// The content of the source editor. |
| 10 | + var content: String |
| 11 | + /// The content of the source editor in lines. |
| 12 | + var lines: [String] |
| 13 | + /// The selection ranges of the source editor. |
| 14 | + var selections: [CursorRange] |
| 15 | + /// The cursor position of the source editor. |
| 16 | + var cursorPosition: CursorPosition |
| 17 | + /// Line annotations of the source editor. |
| 18 | + var lineAnnotations: [String] |
| 19 | + } |
| 20 | + |
| 21 | + let runningApplication: NSRunningApplication |
| 22 | + let element: AXUIElement |
| 23 | + |
| 24 | + /// The content of the source editor. |
| 25 | + public var content: Content { |
| 26 | + let content = element.value |
| 27 | + let split = Self.breakLines(content) |
| 28 | + let lineAnnotationElements = element.children { $0.identifier == "Line Annotation" } |
| 29 | + let lineAnnotations = lineAnnotationElements.map(\.label) |
| 30 | + |
| 31 | + if let selectionRange = element.selectedTextRange { |
| 32 | + let range = Self.convertRangeToCursorRange(selectionRange, in: content) |
| 33 | + return .init( |
| 34 | + content: content, |
| 35 | + lines: split, |
| 36 | + selections: [range], |
| 37 | + cursorPosition: range.end, |
| 38 | + lineAnnotations: lineAnnotations |
| 39 | + ) |
| 40 | + } |
| 41 | + return .init( |
| 42 | + content: content, |
| 43 | + lines: split, |
| 44 | + selections: [], |
| 45 | + cursorPosition: .outOfScope, |
| 46 | + lineAnnotations: lineAnnotations |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + public init(runningApplication: NSRunningApplication, element: AXUIElement) { |
| 51 | + self.runningApplication = runningApplication |
| 52 | + self.element = element |
| 53 | + } |
| 54 | + |
| 55 | + /// Observe to changes in the source editor. |
| 56 | + public func observe(notificationNames: String...) -> AXNotificationStream { |
| 57 | + return AXNotificationStream( |
| 58 | + app: runningApplication, |
| 59 | + element: element, |
| 60 | + notificationNames: notificationNames |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + /// Observe to changes in the source editor scroll view. |
| 65 | + public func observeScrollView(notificationNames: String...) -> AXNotificationStream? { |
| 66 | + guard let scrollView = element.parent else { return nil } |
| 67 | + return AXNotificationStream( |
| 68 | + app: runningApplication, |
| 69 | + element: scrollView, |
| 70 | + notificationNames: notificationNames |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// MARK: - Helpers |
| 76 | + |
| 77 | +public extension SourceEditor { |
| 78 | + static func convertCursorRangeToRange( |
| 79 | + _ cursorRange: CursorRange, |
| 80 | + in lines: [String] |
| 81 | + ) -> CFRange { |
| 82 | + var countS = 0 |
| 83 | + var countE = 0 |
| 84 | + var range = CFRange(location: 0, length: 0) |
| 85 | + for (i, line) in lines.enumerated() { |
| 86 | + if i == cursorRange.start.line { |
| 87 | + countS = countS + cursorRange.start.character |
| 88 | + range.location = countS |
| 89 | + } |
| 90 | + if i == cursorRange.end.line { |
| 91 | + countE = countE + cursorRange.end.character |
| 92 | + range.length = max(countE - range.location, 0) |
| 93 | + break |
| 94 | + } |
| 95 | + countS += line.count |
| 96 | + countE += line.count |
| 97 | + } |
| 98 | + return range |
| 99 | + } |
| 100 | + |
| 101 | + static func convertCursorRangeToRange( |
| 102 | + _ cursorRange: CursorRange, |
| 103 | + in content: String |
| 104 | + ) -> CFRange { |
| 105 | + let lines = breakLines(content) |
| 106 | + return convertCursorRangeToRange(cursorRange, in: lines) |
| 107 | + } |
| 108 | + |
| 109 | + static func convertRangeToCursorRange( |
| 110 | + _ range: ClosedRange<Int>, |
| 111 | + in lines: [String] |
| 112 | + ) -> CursorRange { |
| 113 | + guard !lines.isEmpty else { return CursorRange(start: .zero, end: .zero) } |
| 114 | + var countS = 0 |
| 115 | + var countE = 0 |
| 116 | + var cursorRange = CursorRange(start: .zero, end: .outOfScope) |
| 117 | + for (i, line) in lines.enumerated() { |
| 118 | + if countS <= range.lowerBound, range.lowerBound < countS + line.count { |
| 119 | + cursorRange.start = .init(line: i, character: range.lowerBound - countS) |
| 120 | + } |
| 121 | + if countE <= range.upperBound, range.upperBound < countE + line.count { |
| 122 | + cursorRange.end = .init(line: i, character: range.upperBound - countE) |
| 123 | + break |
| 124 | + } |
| 125 | + countS += line.count |
| 126 | + countE += line.count |
| 127 | + } |
| 128 | + if cursorRange.end == .outOfScope { |
| 129 | + cursorRange.end = .init(line: lines.endIndex - 1, character: lines.last?.count ?? 0) |
| 130 | + } |
| 131 | + return cursorRange |
| 132 | + } |
| 133 | + |
| 134 | + static func convertRangeToCursorRange( |
| 135 | + _ range: ClosedRange<Int>, |
| 136 | + in content: String |
| 137 | + ) -> CursorRange { |
| 138 | + let lines = breakLines(content) |
| 139 | + return convertRangeToCursorRange(range, in: lines) |
| 140 | + } |
| 141 | + |
| 142 | + static func breakLines(_ string: String) -> [String] { |
| 143 | + let lines = string.split(separator: "\n", omittingEmptySubsequences: false) |
| 144 | + var all = [String]() |
| 145 | + for (index, line) in lines.enumerated() { |
| 146 | + if index == lines.endIndex - 1 { |
| 147 | + all.append(String(line)) |
| 148 | + } else { |
| 149 | + all.append(String(line) + "\n") |
| 150 | + } |
| 151 | + } |
| 152 | + return all |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments