@@ -74,8 +74,10 @@ final class Filespace {
7474 /// - cursorPosition: cursor position
7575 /// - Returns: `true` if the suggestion is still valid
7676 func validateSuggestions( lines: [ String ] , cursorPosition: CursorPosition ) -> Bool {
77+ guard let presentingSuggestion else { return false }
78+
7779 // cursor has moved to another line
78- if cursorPosition. line != suggestionSourceSnapshot . cursorPosition . line {
80+ if cursorPosition. line != presentingSuggestion . position . line {
7981 reset ( )
8082 return false
8183 }
@@ -87,11 +89,14 @@ final class Filespace {
8789 }
8890
8991 let editingLine = lines [ cursorPosition. line] . dropLast ( 1 ) // dropping \n
90- let suggestionLines = presentingSuggestion? . text. split ( separator: " \n " ) ?? [ ]
92+ let suggestionLines = presentingSuggestion. text. split ( separator: " \n " )
9193 let suggestionFirstLine = suggestionLines. first ?? " "
9294
9395 // the line content doesn't match the suggestion
94- if !suggestionFirstLine. hasPrefix ( editingLine) {
96+ if cursorPosition. character > 0 , !suggestionFirstLine
97+ . hasPrefix ( editingLine [ ..< editingLine
98+ . index ( editingLine. startIndex, offsetBy: cursorPosition. character) ] )
99+ {
95100 reset ( )
96101 return false
97102 }
@@ -102,8 +107,8 @@ final class Filespace {
102107 return false
103108 }
104109
105- // the line is empty
106- if editingLine. trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty {
110+ // undo to a state before the suggestion was generated
111+ if editingLine. count < presentingSuggestion . position . character {
107112 reset ( )
108113 return false
109114 }
@@ -451,3 +456,4 @@ extension Workspace {
451456 await _suggestionService? . terminate ( )
452457 }
453458}
459+
0 commit comments