Skip to content

Commit 7f49657

Browse files
committed
Fix suggestion validation
1 parent f0689e9 commit 7f49657

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

Tool/Sources/WorkspaceSuggestionService/Filespace+SuggestionService.swift

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Workspace
55
public struct FilespaceSuggestionSnapshot: Equatable {
66
public var linesHash: Int
77
public var cursorPosition: CursorPosition
8-
8+
99
public init(linesHash: Int, cursorPosition: CursorPosition) {
1010
self.linesHash = linesHash
1111
self.cursorPosition = cursorPosition
@@ -59,22 +59,53 @@ public extension Filespace {
5959
let editingLine = lines[cursorPosition.line].dropLast(1) // dropping \n
6060
let suggestionLines = presentingSuggestion.text.split(separator: "\n")
6161
let suggestionFirstLine = suggestionLines.first ?? ""
62+
63+
/// For example:
64+
/// ```
65+
/// ABCD012 // typed text
66+
/// ^
67+
/// 0123456 // suggestion range 4-11, generated after `ABCD`
68+
/// ```
69+
/// The suggestion should contain `012`, aka, the suggestion that is typed.
70+
///
71+
/// Another case is that the suggestion may contain the whole line.
72+
/// /// ```
73+
/// ABCD012 // typed text
74+
/// ----^
75+
/// ABCD0123456 // suggestion range 0-11, generated after `ABCD`
76+
/// The suggestion should contain `ABCD012`, aka, the suggestion that is typed.
77+
/// ```
78+
let typedSuggestion = {
79+
let startIndex = editingLine.index(
80+
editingLine.startIndex,
81+
offsetBy: presentingSuggestion.position.character,
82+
limitedBy: editingLine.endIndex
83+
) ?? editingLine.startIndex
84+
85+
let endIndex = editingLine.index(
86+
editingLine.startIndex,
87+
offsetBy: cursorPosition.character,
88+
limitedBy: editingLine.endIndex
89+
) ?? editingLine.endIndex
90+
91+
if endIndex > startIndex {
92+
return editingLine[startIndex..<endIndex]
93+
}
94+
95+
return ""
96+
}()
6297

6398
// the line content doesn't match the suggestion
6499
if cursorPosition.character > 0,
65-
!suggestionFirstLine.hasPrefix(editingLine[..<(editingLine.index(
66-
editingLine.startIndex,
67-
offsetBy: cursorPosition.character,
68-
limitedBy: editingLine.endIndex
69-
) ?? editingLine.endIndex)])
100+
!suggestionFirstLine.hasPrefix(typedSuggestion)
70101
{
71102
reset()
72103
resetSnapshot()
73104
return false
74105
}
75106

76107
// finished typing the whole suggestion when the suggestion has only one line
77-
if editingLine.hasPrefix(suggestionFirstLine), suggestionLines.count <= 1 {
108+
if typedSuggestion.hasPrefix(suggestionFirstLine), suggestionLines.count <= 1 {
78109
reset()
79110
resetSnapshot()
80111
return false

0 commit comments

Comments
 (0)