Skip to content

Commit 2eab928

Browse files
committed
Fix index out of scope
1 parent 3632ead commit 2eab928

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Core/Sources/Service/Workspace.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ final class Filespace {
9393
let suggestionFirstLine = suggestionLines.first ?? ""
9494

9595
// the line content doesn't match the suggestion
96-
if cursorPosition.character > 0, !suggestionFirstLine
97-
.hasPrefix(editingLine[..<editingLine
98-
.index(editingLine.startIndex, offsetBy: cursorPosition.character)])
96+
if cursorPosition.character > 0,
97+
!suggestionFirstLine.hasPrefix(editingLine[..<(editingLine.index(
98+
editingLine.startIndex,
99+
offsetBy: cursorPosition.character,
100+
limitedBy: editingLine.endIndex
101+
) ?? editingLine.endIndex)])
99102
{
100103
reset()
101104
return false

Core/Tests/ServiceTests/FilespaceSuggestionInvalidationTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ class FilespaceSuggestionInvalidationTests: XCTestCase {
9191
XCTAssertNil(suggestion)
9292
}
9393

94+
func test_line_content_does_not_match_input_should_invalidate_index_out_of_scope() async throws {
95+
let filespace = try await prepare(
96+
suggestionText: "hello man",
97+
cursorPosition: .init(line: 1, character: 0)
98+
)
99+
let isValid = await filespace.validateSuggestions(
100+
lines: ["\n", "helo\n", "\n"],
101+
cursorPosition: .init(line: 1, character: 100)
102+
)
103+
XCTAssertFalse(isValid)
104+
let suggestion = await filespace.presentingSuggestion
105+
XCTAssertNil(suggestion)
106+
}
107+
94108
func test_finish_typing_the_whole_single_line_suggestion_should_invalidate() async throws {
95109
let filespace = try await prepare(
96110
suggestionText: "hello man",

0 commit comments

Comments
 (0)