|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 LSPLogging |
| 15 | +import LSPTestSupport |
| 16 | +import SKTestSupport |
| 17 | +import SourceKitLSP |
| 18 | +import XCTest |
| 19 | + |
| 20 | +final class FormattingTests: XCTestCase { |
| 21 | + |
| 22 | + /// Connection and lifetime management for the service. |
| 23 | + var connection: TestSourceKitServer! = nil |
| 24 | + |
| 25 | + /// The primary interface to make requests to the SourceKitServer. |
| 26 | + var sk: TestClient! = nil |
| 27 | + |
| 28 | + var documentManager: DocumentManager! { |
| 29 | + connection.server!._documentManager |
| 30 | + } |
| 31 | + |
| 32 | + override func setUp() { |
| 33 | + connection = TestSourceKitServer() |
| 34 | + sk = connection.client |
| 35 | + _ = try! sk.sendSync( |
| 36 | + InitializeRequest( |
| 37 | + processId: nil, |
| 38 | + rootPath: nil, |
| 39 | + rootURI: nil, |
| 40 | + initializationOptions: nil, |
| 41 | + capabilities: ClientCapabilities( |
| 42 | + workspace: nil, |
| 43 | + textDocument: TextDocumentClientCapabilities( |
| 44 | + codeAction: .init( |
| 45 | + codeActionLiteralSupport: .init( |
| 46 | + codeActionKind: .init(valueSet: [.quickFix]) |
| 47 | + ) |
| 48 | + ), |
| 49 | + publishDiagnostics: .init(codeDescriptionSupport: true) |
| 50 | + ) |
| 51 | + ), |
| 52 | + trace: .off, |
| 53 | + workspaceFolders: nil |
| 54 | + ) |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + override func tearDown() { |
| 59 | + sk = nil |
| 60 | + connection = nil |
| 61 | + } |
| 62 | + |
| 63 | + func testFormatting() throws { |
| 64 | + let url = URL(fileURLWithPath: "/\(UUID())/a.swift") |
| 65 | + let uri = DocumentURI(url) |
| 66 | + |
| 67 | + sk.send(DidOpenTextDocumentNotification(textDocument: TextDocumentItem( |
| 68 | + uri: uri, |
| 69 | + language: .swift, |
| 70 | + version: 1, |
| 71 | + text: """ |
| 72 | + struct S { |
| 73 | + var foo: Int |
| 74 | + } |
| 75 | + """))) |
| 76 | + |
| 77 | + let resp = try sk.sendSync( |
| 78 | + DocumentFormattingRequest( |
| 79 | + textDocument: TextDocumentIdentifier(url), |
| 80 | + options: FormattingOptions( |
| 81 | + tabSize: 2, |
| 82 | + insertSpaces: true |
| 83 | + ) |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + let edits = try XCTUnwrap(resp) |
| 88 | + XCTAssertEqual(edits.count, 1) |
| 89 | + let edit = try XCTUnwrap(edits.first) |
| 90 | + XCTAssertEqual(edit.range, Position(line: 0, utf16index: 0)..<Position(line: 2, utf16index: 1)) |
| 91 | + XCTAssertEqual(edit.newText, """ |
| 92 | + struct S { |
| 93 | + var foo: Int |
| 94 | + } |
| 95 | +
|
| 96 | + """) |
| 97 | + } |
| 98 | + |
| 99 | + func testFormattingNoEdits() throws { |
| 100 | + let url = URL(fileURLWithPath: "/\(UUID())/a.swift") |
| 101 | + let uri = DocumentURI(url) |
| 102 | + |
| 103 | + sk.send(DidOpenTextDocumentNotification(textDocument: TextDocumentItem( |
| 104 | + uri: uri, |
| 105 | + language: .swift, |
| 106 | + version: 1, |
| 107 | + text: """ |
| 108 | + struct S { |
| 109 | + var foo: Int |
| 110 | + } |
| 111 | +
|
| 112 | + """))) |
| 113 | + |
| 114 | + let resp = try sk.sendSync( |
| 115 | + DocumentFormattingRequest( |
| 116 | + textDocument: TextDocumentIdentifier(url), |
| 117 | + options: FormattingOptions( |
| 118 | + tabSize: 2, |
| 119 | + insertSpaces: true |
| 120 | + ) |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + let edits = try XCTUnwrap(resp) |
| 125 | + XCTAssertEqual(edits.count, 0) |
| 126 | + } |
| 127 | +} |
0 commit comments