@@ -42,23 +42,35 @@ public extension Filespace {
4242 @WorkspaceActor
4343 func validateSuggestions( lines: [ String ] , cursorPosition: CursorPosition ) -> Bool {
4444 guard let presentingSuggestion else { return false }
45-
46- // cursor has moved to another line
47- if cursorPosition. line != presentingSuggestion. position. line {
45+ guard Self . validateSuggestion (
46+ presentingSuggestion,
47+ lines: lines,
48+ cursorPosition: cursorPosition
49+ )
50+ else {
4851 reset ( )
4952 resetSnapshot ( )
5053 return false
5154 }
5255
56+ return true
57+ }
58+ }
59+
60+ extension Filespace {
61+ static func validateSuggestion(
62+ _ suggestion: CodeSuggestion ,
63+ lines: [ String ] ,
64+ cursorPosition: CursorPosition
65+ ) -> Bool {
66+ // cursor has moved to another line
67+ if cursorPosition. line != suggestion. position. line { return false }
68+
5369 // the cursor position is valid
54- guard cursorPosition. line >= 0 , cursorPosition. line < lines. count else {
55- reset ( )
56- resetSnapshot ( )
57- return false
58- }
70+ guard cursorPosition. line >= 0 , cursorPosition. line < lines. count else { return false }
5971
6072 let editingLine = lines [ cursorPosition. line] . dropLast ( 1 ) // dropping line ending
61- let suggestionLines = presentingSuggestion . text. split ( whereSeparator: \. isNewline)
73+ let suggestionLines = suggestion . text. split ( whereSeparator: \. isNewline)
6274 let suggestionFirstLine = suggestionLines. first ?? " "
6375
6476 /// For example:
@@ -78,15 +90,15 @@ public extension Filespace {
7890 /// ```
7991 let typedSuggestion = {
8092 assert (
81- presentingSuggestion . range. start. character >= 0 ,
93+ suggestion . range. start. character >= 0 ,
8294 " Generating suggestion with invalid range "
8395 )
8496
8597 let utf16View = editingLine. utf16
8698
8799 let startIndex = utf16View. index (
88100 utf16View. startIndex,
89- offsetBy: max ( 0 , presentingSuggestion . range. start. character) ,
101+ offsetBy: max ( 0 , suggestion . range. start. character) ,
90102 limitedBy: utf16View. endIndex
91103 ) ?? utf16View. startIndex
92104
@@ -103,14 +115,12 @@ public extension Filespace {
103115 return " "
104116 } ( )
105117
106- /// if the line will not change after accepting the suggestion
118+ // if the line will not change after accepting the suggestion
107119 if suggestionLines. count == 1 {
108120 if editingLine. hasPrefix ( suggestionFirstLine) ,
109121 cursorPosition. character
110- >= suggestionFirstLine. utf16. count + presentingSuggestion . range. start. character
122+ >= suggestionFirstLine. utf16. count + suggestion . range. start. character
111123 {
112- reset ( )
113- resetSnapshot ( )
114124 return false
115125 }
116126 }
@@ -119,22 +129,16 @@ public extension Filespace {
119129 if cursorPosition. character > 0 ,
120130 !suggestionFirstLine. hasPrefix ( typedSuggestion)
121131 {
122- reset ( )
123- resetSnapshot ( )
124132 return false
125133 }
126134
127135 // finished typing the whole suggestion when the suggestion has only one line
128136 if typedSuggestion. hasPrefix ( suggestionFirstLine) , suggestionLines. count <= 1 {
129- reset ( )
130- resetSnapshot ( )
131137 return false
132138 }
133139
134140 // undo to a state before the suggestion was generated
135- if editingLine. utf16. count < presentingSuggestion. position. character {
136- reset ( )
137- resetSnapshot ( )
141+ if editingLine. utf16. count < suggestion. position. character {
138142 return false
139143 }
140144
0 commit comments