@@ -42,27 +42,32 @@ struct PseudoCommandHandler {
4242
4343 @WorkspaceActor
4444 func generateRealtimeSuggestions( sourceEditor: SourceEditor ? ) async {
45- // Can't use handler if content is not available.
46- guard
47- let editor = await getEditorContent ( sourceEditor: sourceEditor) ,
48- let filespace = await getFilespace ( ) ,
45+ guard let filespace = await getFilespace ( ) ,
4946 let ( workspace, _) = try ? await Service . shared. workspacePool
5047 . fetchOrCreateWorkspaceAndFilespace ( fileURL: filespace. fileURL) else { return }
48+
49+ if Task . isCancelled { return }
50+
51+ // Can't use handler if content is not available.
52+ guard let editor = await getEditorContent ( sourceEditor: sourceEditor)
53+ else { return }
5154
5255 let fileURL = filespace. fileURL
5356 let presenter = PresentInWindowSuggestionPresenter ( )
5457
5558 presenter. markAsProcessing ( true )
5659 defer { presenter. markAsProcessing ( false ) }
5760
58- // Check if the current suggestion is still valid.
59- if filespace. validateSuggestions (
60- lines: editor. lines,
61- cursorPosition: editor. cursorPosition
62- ) {
63- return
64- } else {
65- presenter. discardSuggestion ( fileURL: filespace. fileURL)
61+ if filespace. presentingSuggestion != nil {
62+ // Check if the current suggestion is still valid.
63+ if filespace. validateSuggestions (
64+ lines: editor. lines,
65+ cursorPosition: editor. cursorPosition
66+ ) {
67+ return
68+ } else {
69+ presenter. discardSuggestion ( fileURL: filespace. fileURL)
70+ }
6671 }
6772
6873 let snapshot = FilespaceSuggestionSnapshot (
@@ -78,9 +83,10 @@ struct PseudoCommandHandler {
7883 editor: editor
7984 )
8085 if let sourceEditor {
86+ let editorContent = sourceEditor. getContent ( )
8187 _ = filespace. validateSuggestions (
82- lines: sourceEditor . content . lines,
83- cursorPosition: sourceEditor . content . cursorPosition
88+ lines: editorContent . lines,
89+ cursorPosition: editorContent . cursorPosition
8490 )
8591 }
8692 if filespace. presentingSuggestion != nil {
@@ -98,9 +104,14 @@ struct PseudoCommandHandler {
98104 guard let ( _, filespace) = try ? await Service . shared. workspacePool
99105 . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL) else { return }
100106
107+ if filespace. presentingSuggestion == nil {
108+ return // skip if there's no suggestion presented.
109+ }
110+
111+ let content = sourceEditor. getContent ( )
101112 if !filespace. validateSuggestions (
102- lines: sourceEditor . content. lines,
103- cursorPosition: sourceEditor . content. cursorPosition
113+ lines: content. lines,
114+ cursorPosition: content. cursorPosition
104115 ) {
105116 PresentInWindowSuggestionPresenter ( ) . discardSuggestion ( fileURL: fileURL)
106117 }
@@ -351,7 +362,8 @@ extension PseudoCommandHandler {
351362 guard let filespace = await getFilespace ( ) ,
352363 let sourceEditor = sourceEditor ?? XcodeInspector . shared. focusedEditor
353364 else { return nil }
354- let content = sourceEditor. content
365+ if Task . isCancelled { return nil }
366+ let content = sourceEditor. getContent ( )
355367 let uti = filespace. codeMetadata. uti ?? " "
356368 let tabSize = filespace. codeMetadata. tabSize ?? 4
357369 let indentSize = filespace. codeMetadata. indentSize ?? 4
0 commit comments