Skip to content

Commit 134926d

Browse files
committed
Add comments
1 parent da20352 commit 134926d

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

Core/Sources/Service/Workspace.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,19 @@ final class Filespace {
6868
lastSuggestionUpdateTime = Environment.now()
6969
}
7070

71+
/// Validate the suggestion is still valid.
72+
/// - Parameters:
73+
/// - lines: lines of the file
74+
/// - cursorPosition: cursor position
75+
/// - Returns: `true` if the suggestion is still valid
7176
func validateSuggestions(lines: [String], cursorPosition: CursorPosition) -> Bool {
77+
// cursor has moved to another line
7278
if cursorPosition.line != suggestionSourceSnapshot.cursorPosition.line {
7379
reset()
7480
return false
7581
}
7682

83+
// the cursor position is valid
7784
guard cursorPosition.line >= 0, cursorPosition.line < lines.count else {
7885
reset()
7986
return false
@@ -82,16 +89,20 @@ final class Filespace {
8289
let editingLine = lines[cursorPosition.line].dropLast(1) // dropping \n
8390
let suggestionLines = presentingSuggestion?.text.split(separator: "\n") ?? []
8491
let suggestionFirstLine = suggestionLines.first ?? ""
92+
93+
// the line content doesn't match the suggestion
8594
if !suggestionFirstLine.hasPrefix(editingLine) {
8695
reset()
8796
return false
8897
}
89-
98+
99+
// finished typing the whole suggestion when the suggestion has only one line
90100
if editingLine == suggestionFirstLine, suggestionLines.count <= 1 {
91101
reset()
92102
return false
93103
}
94104

105+
// the line is empty
95106
if editingLine.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
96107
reset()
97108
return false
@@ -440,4 +451,3 @@ extension Workspace {
440451
await _suggestionService?.terminate()
441452
}
442453
}
443-

0 commit comments

Comments
 (0)