Skip to content

Commit 3e8ff22

Browse files
committed
Prevent getting multiple content from SourceEditor in a single run
1 parent d1fc4d6 commit 3e8ff22

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

Core/Sources/ChatService/CustomCommandTemplateProcessor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct CustomCommandTemplateProcessor {
3939
}
4040

4141
func getEditorInformation() -> EditorInformation {
42-
let editorContent = XcodeInspector.shared.focusedEditor?.content
42+
let editorContent = XcodeInspector.shared.focusedEditor?.getContent()
4343
let documentURL = XcodeInspector.shared.activeDocumentURL
4444
let language = documentURL.map(languageIdentifierFromFileURL) ?? .plaintext
4545

Core/Sources/Service/GUI/ChatTabFactory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum ChatTabFactory {
4343
guard let editor = XcodeInspector.shared.focusedEditor else {
4444
return .init(selectedText: "", language: "", fileContent: "")
4545
}
46-
let content = editor.content
46+
let content = editor.getContent()
4747
return .init(
4848
selectedText: content.selectedContent,
4949
language: (

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ struct PseudoCommandHandler {
7878
editor: editor
7979
)
8080
if let sourceEditor {
81+
let editorContent = sourceEditor.getContent()
8182
_ = filespace.validateSuggestions(
82-
lines: sourceEditor.content.lines,
83-
cursorPosition: sourceEditor.content.cursorPosition
83+
lines: editorContent.lines,
84+
cursorPosition: editorContent.cursorPosition
8485
)
8586
}
8687
if filespace.presentingSuggestion != nil {
@@ -98,9 +99,10 @@ struct PseudoCommandHandler {
9899
guard let (_, filespace) = try? await Service.shared.workspacePool
99100
.fetchOrCreateWorkspaceAndFilespace(fileURL: fileURL) else { return }
100101

102+
let content = sourceEditor.getContent()
101103
if !filespace.validateSuggestions(
102-
lines: sourceEditor.content.lines,
103-
cursorPosition: sourceEditor.content.cursorPosition
104+
lines: content.lines,
105+
cursorPosition: content.cursorPosition
104106
) {
105107
PresentInWindowSuggestionPresenter().discardSuggestion(fileURL: fileURL)
106108
}
@@ -351,7 +353,7 @@ extension PseudoCommandHandler {
351353
guard let filespace = await getFilespace(),
352354
let sourceEditor = sourceEditor ?? XcodeInspector.shared.focusedEditor
353355
else { return nil }
354-
let content = sourceEditor.content
356+
let content = sourceEditor.getContent()
355357
let uti = filespace.codeMetadata.uti ?? ""
356358
let tabSize = filespace.codeMetadata.tabSize ?? 4
357359
let indentSize = filespace.codeMetadata.indentSize ?? 4

Pro

Submodule Pro updated from 58572b0 to 80b755f

Tool/Sources/XcodeInspector/SourceEditor.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,25 @@ public class SourceEditor {
2424
var observeAXNotificationsTask: Task<Void, Never>?
2525
public let axNotifications = AsyncPassthroughSubject<AXNotification>()
2626

27-
/// The content of the source editor.
28-
public var content: Content {
27+
28+
private var cachedContent: String?
29+
private var cachedLines = [String]()
30+
31+
/// Get the content of the source editor.
32+
///
33+
/// - note: This method is expensive.
34+
public func getContent() -> Content {
2935
let content = element.value
30-
let split = content.breakLines(appendLineBreakToLastLine: false)
36+
37+
let split = if element.hashValue == cachedContent?.hashValue {
38+
cachedLines
39+
} else {
40+
content.breakLines(appendLineBreakToLastLine: false)
41+
}
42+
43+
cachedContent = content
44+
cachedLines = split
45+
3146
let lineAnnotationElements = element.children.filter { $0.identifier == "Line Annotation" }
3247
let lineAnnotations = lineAnnotationElements.map(\.description)
3348

Tool/Sources/XcodeInspector/XcodeInspector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class XcodeInspector: ObservableObject {
4141
let projectURL = XcodeInspector.shared.activeProjectRootURL
4242
else { return nil }
4343

44-
let editorContent = XcodeInspector.shared.focusedEditor?.content
44+
let editorContent = XcodeInspector.shared.focusedEditor?.getContent()
4545
let language = languageIdentifierFromFileURL(documentURL)
4646
let relativePath = documentURL.path.replacingOccurrences(of: projectURL.path, with: "")
4747

0 commit comments

Comments
 (0)