Files
Xi Ge 8e28609a31 Fix crashes in code completion for out-of-range positions and at end of file. rdar://194222884
When a request came in with a column past the end of its line, which
happens when the editor's copy of the document has drifted from ours,
LineTable clamped it to the end of the line including its terminator,
which is really the start of the next line. In code completion, if that
next line began with an identifier like `func`, the completion position
landed after the cursor and building the text edit range trapped. LineTable
now clamps such columns to the end of the line's content, before the
newline, as LSP specifies, and completion clamps the request position once
up front so that everything downstream agrees on where the cursor is.

Separately, argument completions at the very end of a file read the
character after the cursor to look for a closing parenthesis, which
subscripted the string at its end index. That check now makes sure there is
a character to look at first.
2026-12-03 16:50:32 -08:00

134 lines
5.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(SourceKitLSP) import LanguageServerProtocol
import SKTestSupport
import SKUtilities
import SourceKitLSP
import SwiftSyntax
import XCTest
final class DocumentManagerTests: SourceKitLSPTestCase {
/// Applies `edits` to a document containing `text` and returns the document's new text.
///
/// Also checks that the `SourceEdit`s returned by `DocumentManager.edit`, which are used to incrementally re-parse
/// the document, describe the same change as the new text.
private func apply(
_ edits: [TextDocumentContentChangeEvent],
to text: String,
file: StaticString = #filePath,
line: UInt = #line
) throws -> String {
let documentManager = DocumentManager()
let uri = DocumentURI(for: .swift)
_ = try documentManager.open(uri, language: .swift, version: 0, text: text)
let (preEditSnapshot, postEditSnapshot, sourceEdits) = try documentManager.edit(
uri,
newVersion: 1,
edits: edits
)
var utf8 = Array(preEditSnapshot.text.utf8)
for sourceEdit in sourceEdits {
utf8.replaceSubrange(
sourceEdit.range.lowerBound.utf8Offset..<sourceEdit.range.upperBound.utf8Offset,
with: sourceEdit.replacementBytes
)
}
XCTAssertEqual(String(decoding: utf8, as: UTF8.self), postEditSnapshot.text, file: file, line: line)
return postEditSnapshot.text
}
private func apply(
_ range: Range<Position>,
_ newText: String,
to text: String,
file: StaticString = #filePath,
line: UInt = #line
) throws -> String {
return try apply(
[TextDocumentContentChangeEvent(range: range, text: newText)],
to: text,
file: file,
line: line
)
}
func testEditPastEndOfLine() throws {
let text = "abc\ndef\n"
// A column past the end of a line refers to the end of that line, before its line terminator.
XCTAssertEqual(try apply(Range(Position(line: 0, utf16index: 10)), "X", to: text), "abcX\ndef\n")
XCTAssertEqual(try apply(Range(Position(line: 0, utf16index: 4)), "X", to: text), "abcX\ndef\n")
XCTAssertEqual(
try apply(Position(line: 0, utf16index: 5)..<Position(line: 0, utf16index: 9), "X", to: text),
"abcX\ndef\n"
)
XCTAssertEqual(
try apply(Position(line: 0, utf16index: 1)..<Position(line: 0, utf16index: 10), "X", to: text),
"aX\ndef\n"
)
XCTAssertEqual(
try apply(Position(line: 0, utf16index: 10)..<Position(line: 1, utf16index: 0), "", to: text),
"abcdef\n"
)
XCTAssertEqual(
try apply(Position(line: 0, utf16index: 10)..<Position(line: 1, utf16index: 10), "X", to: text),
"abcX\n"
)
// A line past the end of the file refers to the end of the file.
XCTAssertEqual(try apply(Range(Position(line: 5, utf16index: 0)), "X", to: text), "abc\ndef\nX")
XCTAssertEqual(try apply(Range(Position(line: 1, utf16index: 10)), "X", to: "abc\ndef"), "abc\ndefX")
}
func testEditPastEndOfLineWithOtherLineTerminators() throws {
XCTAssertEqual(try apply(Range(Position(line: 0, utf16index: 4)), "X", to: "abc\r\ndef"), "abcX\r\ndef")
XCTAssertEqual(
try apply(Position(line: 0, utf16index: 10)..<Position(line: 1, utf16index: 0), "", to: "abc\r\ndef"),
"abcdef"
)
XCTAssertEqual(try apply(Range(Position(line: 0, utf16index: 10)), "X", to: "abc\rdef"), "abcX\rdef")
XCTAssertEqual(try apply(Range(Position(line: 1, utf16index: 5)), "X", to: "a\n\nb"), "a\nX\nb")
XCTAssertEqual(try apply(Range(Position(line: 0, utf16index: 10)), "X", to: "こん\nに"), "こんX\nに")
// U+2028 is not a line terminator, so it stays part of the line.
XCTAssertEqual(try apply(Range(Position(line: 0, utf16index: 10)), "X", to: "a\u{2028}\nb"), "a\u{2028}X\nb")
}
func testSequentialEditsPastEndOfLine() throws {
XCTAssertEqual(
try apply(
[
TextDocumentContentChangeEvent(range: Range(Position(line: 0, utf16index: 10)), text: "X"),
TextDocumentContentChangeEvent(range: Range(Position(line: 0, utf16index: 20)), text: "Y"),
],
to: "abc\ndef\n"
),
"abcXY\ndef\n"
)
}
func testEditsWithArbitraryPositionsKeepSourceEditsConsistent() throws {
for text in ["", "a", "ab\ncd", "ab\ncd\n", "ab\r\ncd\r", "\n\n", "こ\nん"] {
let lineCount = LineTable(text).lineCount
var positions: [Position] = []
for line in -1...(lineCount + 1) {
for column in 0...5 {
positions.append(Position(line: line, utf16index: column))
}
}
for lowerBound in positions {
for upperBound in positions where lowerBound <= upperBound {
_ = try apply(lowerBound..<upperBound, "X", to: text)
}
}
}
}
}