Files
sourcekit-lsp/Tests/SourceKitLSPTests/FormattingTests.swift
Alex Hoppen 2ec571b529 Revert "Require a Swift 6 compiler to build SourceKit-LSP"
This reverts commit 7ab7c01cdc.

# Conflicts:
#	Package.swift
2024-09-26 18:23:59 -07:00

302 lines
8.1 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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
//
//===----------------------------------------------------------------------===//
import LanguageServerProtocol
import SKLogging
import SKTestSupport
import SourceKitLSP
import XCTest
final class FormattingTests: XCTestCase {
func testFormatting() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct S {
1⃣var foo: 2⃣ 3⃣Int
4⃣var bar: Int
}5
""",
uri: uri
)
let response = try await testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 3, insertSpaces: true)
)
)
let edits = try XCTUnwrap(response)
XCTAssertEqual(
edits,
[
TextEdit(range: Range(positions["1"]), newText: " "),
TextEdit(range: positions["2"]..<positions["3"], newText: ""),
TextEdit(range: Range(positions["4"]), newText: " "),
TextEdit(range: Range(positions["5"]), newText: "\n"),
]
)
}
func testFormattingNoEdits() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
struct S {
var foo: Int
}
""",
uri: uri
)
let response = try await testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 2, insertSpaces: true)
)
)
let edits = try XCTUnwrap(response)
XCTAssertEqual(edits.count, 0)
}
func testConfigFileOnDisk() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
let project = try await MultiFileTestProject(files: [
".swift-format": """
{
"version": 1,
"indentation": {
"spaces": 1
}
}
""",
"test.swift": """
struct Root {
1⃣var bar = 123
}
""",
])
let (uri, positions) = try project.openDocument("test.swift")
let response = try await project.testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 2, insertSpaces: true)
)
)
XCTAssertEqual(
response,
[
TextEdit(range: Range(positions["1"]), newText: " ")
]
)
}
func testConfigFileInParentDirectory() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
let project = try await MultiFileTestProject(files: [
".swift-format": """
{
"version": 1,
"indentation": {
"spaces": 1
}
}
""",
"sub/test.swift": """
struct Root {
1⃣var bar = 123
}
""",
])
let (uri, positions) = try project.openDocument("test.swift")
let response = try await project.testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 2, insertSpaces: true)
)
)
XCTAssertEqual(
response,
[
TextEdit(range: Range(positions["1"]), newText: " ")
]
)
}
func testConfigFileInNestedDirectory() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
let project = try await MultiFileTestProject(files: [
".swift-format": """
{
"version": 1,
"indentation": {
"spaces": 1
}
},
""",
"sub/.swift-format": """
{
"version": 1,
"indentation": {
"spaces": 3
}
}
""",
"sub/test.swift": """
struct Root {
1⃣var bar = 123
}
""",
])
let (uri, positions) = try project.openDocument("test.swift")
let response = try await project.testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 2, insertSpaces: true)
)
)
XCTAssertEqual(
response,
[
TextEdit(range: Range(positions["1"]), newText: " ")
]
)
}
func testInvalidConfigurationFile() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
// We pick up an invalid swift-format configuration file and thus don't set the user-provided options.
// The swift-format default is 2 spaces.
let project = try await MultiFileTestProject(files: [
".swift-format": "",
"test.swift": """
struct Root {
1⃣var bar = 123
}
""",
])
let (uri, _) = try project.openDocument("test.swift")
await assertThrowsError(
try await project.testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 3, insertSpaces: true)
)
)
)
}
func testInsertAndRemove() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
1⃣public 2⃣extension Example {
3⃣func function() {}
}
""",
uri: uri
)
let response = try await testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 2, insertSpaces: true)
)
)
let edits = try XCTUnwrap(response)
XCTAssertEqual(
edits,
[
TextEdit(range: positions["1"]..<positions["2"], newText: ""),
TextEdit(range: Range(positions["3"]), newText: "public "),
]
)
}
func testMultiLineStringInsertion() async throws {
try await SkipUnless.toolchainContainsSwiftFormat()
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
#"""
_ = [
Node(
documentation: """
1A
2B
3C
4""",
children: [
Child(
documentation: """
5A
6 7\#("")
8 9 🔟"""
)
]
)
]
"""#,
uri: uri
)
let response = try await testClient.send(
DocumentFormattingRequest(
textDocument: TextDocumentIdentifier(uri),
options: FormattingOptions(tabSize: 2, insertSpaces: true)
)
)
let edits = try XCTUnwrap(response)
XCTAssertEqual(
edits,
[
TextEdit(range: Range(positions["1"]), newText: " "),
TextEdit(range: Range(positions["2"]), newText: " "),
TextEdit(range: Range(positions["3"]), newText: " "),
TextEdit(range: Range(positions["4"]), newText: " "),
TextEdit(range: Range(positions["5"]), newText: " "),
TextEdit(range: Range(positions["6"]), newText: "\n"),
TextEdit(range: positions["7"]..<positions["8"], newText: ""),
TextEdit(range: positions["9"]..<positions["🔟"], newText: ""),
]
)
}
}