|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import LanguageServerProtocol |
| 14 | +import SKLogging |
| 15 | +import SKTestSupport |
| 16 | +import SourceKitLSP |
| 17 | +import XCTest |
| 18 | + |
| 19 | +final class RangeFormattingTests: XCTestCase { |
| 20 | + func testOnlyFormatsSpecifiedLines() async throws { |
| 21 | + try await SkipUnless.toolchainContainsSwiftFormat() |
| 22 | + let testClient = try await TestSourceKitLSPClient() |
| 23 | + let uri = DocumentURI(for: .swift) |
| 24 | + |
| 25 | + let positions = testClient.openDocument( |
| 26 | + """ |
| 27 | + func foo() { |
| 28 | + if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() { |
| 29 | + 1️⃣// do stuff2️⃣ |
| 30 | + } |
| 31 | + } |
| 32 | + """, |
| 33 | + uri: uri |
| 34 | + ) |
| 35 | + |
| 36 | + let response = try await testClient.send( |
| 37 | + DocumentRangeFormattingRequest( |
| 38 | + textDocument: TextDocumentIdentifier(uri), |
| 39 | + range: positions["1️⃣"]..<positions["2️⃣"], |
| 40 | + options: FormattingOptions(tabSize: 2, insertSpaces: true) |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + let edits = try XCTUnwrap(response) |
| 45 | + XCTAssertEqual( |
| 46 | + edits, |
| 47 | + [ |
| 48 | + TextEdit(range: Range(positions["1️⃣"]), newText: " ") |
| 49 | + ] |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + func testOnlyFormatsSpecifiedColumns() async throws { |
| 54 | + try await SkipUnless.toolchainContainsSwiftFormat() |
| 55 | + let testClient = try await TestSourceKitLSPClient() |
| 56 | + let uri = DocumentURI(for: .swift) |
| 57 | + |
| 58 | + let positions = testClient.openDocument( |
| 59 | + """ |
| 60 | + func foo() { |
| 61 | + if let SomeReallyLongVar 1️⃣= 2️⃣ 3️⃣Some.More.Stuff(), let a = myfunc() { |
| 62 | + // do stuff |
| 63 | + } |
| 64 | + } |
| 65 | + """, |
| 66 | + uri: uri |
| 67 | + ) |
| 68 | + |
| 69 | + let response = try await testClient.send( |
| 70 | + DocumentRangeFormattingRequest( |
| 71 | + textDocument: TextDocumentIdentifier(uri), |
| 72 | + range: positions["1️⃣"]..<positions["3️⃣"], |
| 73 | + options: FormattingOptions(tabSize: 2, insertSpaces: true) |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + let edits = try XCTUnwrap(response) |
| 78 | + XCTAssertEqual( |
| 79 | + edits, |
| 80 | + [ |
| 81 | + TextEdit(range: positions["2️⃣"]..<positions["3️⃣"], newText: "") |
| 82 | + ] |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + func testFormatsMultipleLines() async throws { |
| 87 | + try await SkipUnless.toolchainContainsSwiftFormat() |
| 88 | + let testClient = try await TestSourceKitLSPClient() |
| 89 | + let uri = DocumentURI(for: .swift) |
| 90 | + |
| 91 | + let positions = testClient.openDocument( |
| 92 | + """ |
| 93 | + 1️⃣func foo() { |
| 94 | + 2️⃣if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() { |
| 95 | + 3️⃣// do stuff |
| 96 | + 4️⃣} |
| 97 | + }5️⃣ |
| 98 | + """, |
| 99 | + uri: uri |
| 100 | + ) |
| 101 | + |
| 102 | + let response = try await testClient.send( |
| 103 | + DocumentRangeFormattingRequest( |
| 104 | + textDocument: TextDocumentIdentifier(uri), |
| 105 | + range: positions["1️⃣"]..<positions["5️⃣"], |
| 106 | + options: FormattingOptions(tabSize: 4, insertSpaces: true) |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + let edits = try XCTUnwrap(response) |
| 111 | + XCTAssertEqual( |
| 112 | + edits, |
| 113 | + [ |
| 114 | + TextEdit(range: Range(positions["2️⃣"]), newText: " "), |
| 115 | + TextEdit(range: Range(positions["3️⃣"]), newText: " "), |
| 116 | + TextEdit(range: Range(positions["4️⃣"]), newText: " "), |
| 117 | + ] |
| 118 | + ) |
| 119 | + } |
| 120 | +} |
0 commit comments