|
| 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 LSPTestSupport |
| 14 | +import LanguageServerProtocol |
| 15 | +import SKTestSupport |
| 16 | +import XCTest |
| 17 | + |
| 18 | +final class HoverTests: XCTestCase { |
| 19 | + func testHover() async throws { |
| 20 | + let testClient = try await TestSourceKitLSPClient() |
| 21 | + let url = URL(fileURLWithPath: "/\(UUID())/a.swift") |
| 22 | + let uri = DocumentURI(url) |
| 23 | + |
| 24 | + testClient.openDocument( |
| 25 | + """ |
| 26 | + /// This is a doc comment for S. |
| 27 | + /// |
| 28 | + /// Details. |
| 29 | + struct S {} |
| 30 | + """, |
| 31 | + uri: uri |
| 32 | + ) |
| 33 | + |
| 34 | + do { |
| 35 | + let resp = try await testClient.send( |
| 36 | + HoverRequest( |
| 37 | + textDocument: TextDocumentIdentifier(url), |
| 38 | + position: Position(line: 3, utf16index: 7) |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + XCTAssertNotNil(resp) |
| 43 | + if let hover = resp { |
| 44 | + XCTAssertNil(hover.range) |
| 45 | + guard case .markupContent(let content) = hover.contents else { |
| 46 | + XCTFail("hover.contents is not .markupContents") |
| 47 | + return |
| 48 | + } |
| 49 | + XCTAssertEqual(content.kind, .markdown) |
| 50 | + XCTAssertEqual( |
| 51 | + content.value, |
| 52 | + """ |
| 53 | + S |
| 54 | + ```swift |
| 55 | + struct S |
| 56 | + ``` |
| 57 | +
|
| 58 | + --- |
| 59 | + This is a doc comment for S. |
| 60 | +
|
| 61 | + ### Discussion |
| 62 | +
|
| 63 | + Details. |
| 64 | + """ |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + do { |
| 70 | + let resp = try await testClient.send( |
| 71 | + HoverRequest( |
| 72 | + textDocument: TextDocumentIdentifier(url), |
| 73 | + position: Position(line: 0, utf16index: 7) |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + XCTAssertNil(resp) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func testMultiCursorInfoResultsHover() async throws { |
| 82 | + let testClient = try await TestSourceKitLSPClient() |
| 83 | + let uri = DocumentURI.for(.swift) |
| 84 | + |
| 85 | + let positions = testClient.openDocument( |
| 86 | + """ |
| 87 | + struct Foo { |
| 88 | + init() {} |
| 89 | + } |
| 90 | + _ = 1️⃣Foo() |
| 91 | + """, |
| 92 | + uri: uri |
| 93 | + ) |
| 94 | + |
| 95 | + let response = try await testClient.send( |
| 96 | + HoverRequest( |
| 97 | + textDocument: TextDocumentIdentifier(uri), |
| 98 | + position: positions["1️⃣"] |
| 99 | + ) |
| 100 | + ) |
| 101 | + |
| 102 | + guard case .markupContent(let content) = response?.contents else { |
| 103 | + XCTFail("hover.contents is not .markupContents") |
| 104 | + return |
| 105 | + } |
| 106 | + XCTAssertEqual(content.kind, .markdown) |
| 107 | + XCTAssertEqual( |
| 108 | + content.value, |
| 109 | + """ |
| 110 | + Foo |
| 111 | + ```swift |
| 112 | + struct Foo |
| 113 | + ``` |
| 114 | +
|
| 115 | + --- |
| 116 | +
|
| 117 | + # Alternative result |
| 118 | + init() |
| 119 | + ```swift |
| 120 | + init() |
| 121 | + ``` |
| 122 | +
|
| 123 | + --- |
| 124 | +
|
| 125 | + """ |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + func testHoverNameEscaping() async throws { |
| 130 | + let testClient = try await TestSourceKitLSPClient() |
| 131 | + let uri = DocumentURI.for(.swift) |
| 132 | + |
| 133 | + testClient.openDocument( |
| 134 | + """ |
| 135 | + /// this is **bold** documentation |
| 136 | + func test(_ a: Int, _ b: Int) { } |
| 137 | + /// this is *italic* documentation |
| 138 | + func *%*(lhs: String, rhs: String) { } |
| 139 | + """, |
| 140 | + uri: uri |
| 141 | + ) |
| 142 | + |
| 143 | + do { |
| 144 | + let resp = try await testClient.send( |
| 145 | + HoverRequest( |
| 146 | + textDocument: TextDocumentIdentifier(uri), |
| 147 | + position: Position(line: 1, utf16index: 7) |
| 148 | + ) |
| 149 | + ) |
| 150 | + |
| 151 | + XCTAssertNotNil(resp) |
| 152 | + if let hover = resp { |
| 153 | + XCTAssertNil(hover.range) |
| 154 | + guard case .markupContent(let content) = hover.contents else { |
| 155 | + XCTFail("hover.contents is not .markupContents") |
| 156 | + return |
| 157 | + } |
| 158 | + XCTAssertEqual(content.kind, .markdown) |
| 159 | + XCTAssertEqual( |
| 160 | + content.value, |
| 161 | + ##""" |
| 162 | + test(\_:\_:) |
| 163 | + ```swift |
| 164 | + func test(_ a: Int, _ b: Int) |
| 165 | + ``` |
| 166 | +
|
| 167 | + --- |
| 168 | + this is **bold** documentation |
| 169 | + """## |
| 170 | + ) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + do { |
| 175 | + let resp = try await testClient.send( |
| 176 | + HoverRequest( |
| 177 | + textDocument: TextDocumentIdentifier(uri), |
| 178 | + position: Position(line: 3, utf16index: 7) |
| 179 | + ) |
| 180 | + ) |
| 181 | + |
| 182 | + XCTAssertNotNil(resp) |
| 183 | + if let hover = resp { |
| 184 | + XCTAssertNil(hover.range) |
| 185 | + guard case .markupContent(let content) = hover.contents else { |
| 186 | + XCTFail("hover.contents is not .markupContents") |
| 187 | + return |
| 188 | + } |
| 189 | + XCTAssertEqual(content.kind, .markdown) |
| 190 | + XCTAssertEqual( |
| 191 | + content.value, |
| 192 | + ##""" |
| 193 | + \*%\*(\_:\_:) |
| 194 | + ```swift |
| 195 | + func *%* (lhs: String, rhs: String) |
| 196 | + ``` |
| 197 | +
|
| 198 | + --- |
| 199 | + this is *italic* documentation |
| 200 | + """## |
| 201 | + ) |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments