Skip to content

Commit cbb0221

Browse files
committed
Add evaluated content changed event to SourceEditor
1 parent 028ed92 commit cbb0221

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Tool/Sources/XcodeInspector/SourceEditor.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class SourceEditor {
2222
case selectedTextChanged
2323
case valueChanged
2424
case scrollPositionChanged
25+
case evaluatedContentChanged
2526
}
2627

2728
let runningApplication: NSRunningApplication
@@ -31,6 +32,22 @@ public class SourceEditor {
3132

3233
/// To prevent expensive calculations in ``getContent()``.
3334
private let cache = Cache()
35+
36+
public func getLatestEvaluatedContent() -> Content {
37+
let selectionRange = element.selectedTextRange
38+
let (content, lines, selections) = cache.latest()
39+
let lineAnnotationElements = element.children.filter { $0.identifier == "Line Annotation" }
40+
let lineAnnotations = lineAnnotationElements.map(\.description)
41+
42+
return .init(
43+
content: content,
44+
lines: lines,
45+
selections: selections,
46+
cursorPosition: selections.first?.start ?? .outOfScope,
47+
cursorOffset: selectionRange?.lowerBound ?? 0,
48+
lineAnnotations: lineAnnotations
49+
)
50+
}
3451

3552
/// Get the content of the source editor.
3653
///
@@ -44,6 +61,8 @@ public class SourceEditor {
4461
let lineAnnotationElements = element.children.filter { $0.identifier == "Line Annotation" }
4562
let lineAnnotations = lineAnnotationElements.map(\.description)
4663

64+
axNotifications.send(.init(kind: .evaluatedContentChanged, element: element))
65+
4766
return .init(
4867
content: content,
4968
lines: lines,
@@ -176,6 +195,12 @@ extension SourceEditor {
176195
return (lines, selections)
177196
}
178197
}
198+
199+
func latest() -> (content: String, lines: [String], selections: [CursorRange]) {
200+
Self.queue.sync {
201+
(sourceContent ?? "", cachedLines, cachedSelections)
202+
}
203+
}
179204
}
180205
}
181206

0 commit comments

Comments
 (0)