Skip to content

Commit d7fad1c

Browse files
committed
Update TextCursorTracker
1 parent 3054a71 commit d7fad1c

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

Core/Sources/SuggestionWidget/TextCursorTracker.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@ import Perception
44
import SuggestionBasic
55
import XcodeInspector
66

7+
/// A passive tracker that observe the changes of the source editor content.
78
@Perceptible
89
final class TextCursorTracker {
910
@MainActor
10-
var cursorPosition: CursorPosition = .zero
11+
var cursorPosition: CursorPosition { content.cursorPosition }
1112
@MainActor
12-
var currentLine: String = ""
13+
var currentLine: String {
14+
if content.cursorPosition.line >= 0, content.cursorPosition.line < content.lines.count {
15+
content.lines[content.cursorPosition.line]
16+
} else {
17+
""
18+
}
19+
}
20+
21+
@MainActor
22+
var content: SourceEditor.Content = .init(
23+
content: "",
24+
lines: [],
25+
selections: [],
26+
cursorPosition: .zero,
27+
cursorOffset: 0,
28+
lineAnnotations: []
29+
)
1330

1431
@PerceptionIgnored var editorObservationTask: Set<AnyCancellable> = []
1532
@PerceptionIgnored var eventObservationTask: Task<Void, Never>?
@@ -38,22 +55,15 @@ final class TextCursorTracker {
3855
eventObservationTask?.cancel()
3956
let content = editor.getLatestEvaluatedContent()
4057
Task { @MainActor in
41-
self.cursorPosition = content.cursorPosition
42-
self.currentLine = if content.cursorPosition.line >= 0,
43-
content.cursorPosition.line < content.lines.count
44-
{
45-
content.lines[content.cursorPosition.line]
46-
} else {
47-
""
48-
}
58+
self.content = content
4959
}
5060
eventObservationTask = Task { [weak self] in
5161
for await event in await editor.axNotifications.notifications() {
5262
guard let self else { return }
5363
guard event.kind == .evaluatedContentChanged else { continue }
5464
let content = editor.getLatestEvaluatedContent()
5565
Task { @MainActor in
56-
self.cursorPosition = content.cursorPosition
66+
self.content = content
5767
}
5868
}
5969
}

0 commit comments

Comments
 (0)