diff --git a/Sources/SwiftLanguageService/SymbolGraph.swift b/Sources/SwiftLanguageService/SymbolGraph.swift index 9f1781158..2ce3a7091 100644 --- a/Sources/SwiftLanguageService/SymbolGraph.swift +++ b/Sources/SwiftLanguageService/SymbolGraph.swift @@ -82,12 +82,14 @@ private struct DocumentableSymbol { self.documentationComments = node.leadingTrivia.flatMap { trivia -> [String] in switch trivia { case .docLineComment(let comment): - return [String(comment.dropFirst(3).trimmingCharacters(in: .whitespaces))] + // Preserve leading whitespace for DocC indentation parsing (issue #2119) + return [String(comment.dropFirst(3))] case .docBlockComment(let comment): return comment.dropFirst(3) .dropLast(2) .split(whereSeparator: \.isNewline) - .map { String($0).trimmingCharacters(in: .whitespaces) } + // Preserve leading whitespace for DocC indentation parsing (issue #2119) + .map(String.init) default: return [] } diff --git a/Tests/SourceKitLSPTests/DoccDocumentationTests.swift b/Tests/SourceKitLSPTests/DoccDocumentationTests.swift index 503a43e5d..8fddfbd4e 100644 --- a/Tests/SourceKitLSPTests/DoccDocumentationTests.swift +++ b/Tests/SourceKitLSPTests/DoccDocumentationTests.swift @@ -66,6 +66,72 @@ final class DoccDocumentationTests: SourceKitLSPTestCase { ) } + func testMultilineReturnsInDocLineComment() async throws { + // Regression test for https://github.com/swiftlang/vscode-swift/issues/2119 + let testClient = try await TestSourceKitLSPClient() + let uri = DocumentURI(for: .swift) + let positions = testClient.openDocument( + """ + /// Checks whether a field exists. + /// + /// - Returns: One of the following + /// * 1️⃣0: field does not exist + /// * 1: field exists + public func hexists() -> Int { return 1 } + """, + uri: uri + ) + let response = try await testClient.send( + DoccDocumentationRequest( + textDocument: TextDocumentIdentifier(uri), + position: positions["1️⃣"] + ) + ) + let renderNodeString = response.renderNode + XCTAssertTrue( + renderNodeString.contains("0: field does not exist"), + "Expected continuation text in render node" + ) + XCTAssertFalse( + renderNodeString.contains("\"kind\" : \"content\""), + "Continuation lines should not create a separate Discussion section" + ) + } + + func testMultilineReturnsInDocBlockComment() async throws { + // Regression test for https://github.com/swiftlang/vscode-swift/issues/2119 + let testClient = try await TestSourceKitLSPClient() + let uri = DocumentURI(for: .swift) + let positions = testClient.openDocument( + """ + /** + Checks whether a field exists. + + - Returns: One of the following + * 1️⃣0: field does not exist + * 1: field exists + */ + public func hexists() -> Int { return 1 } + """, + uri: uri + ) + let response = try await testClient.send( + DoccDocumentationRequest( + textDocument: TextDocumentIdentifier(uri), + position: positions["1️⃣"] + ) + ) + let renderNodeString = response.renderNode + XCTAssertTrue( + renderNodeString.contains("0: field does not exist"), + "Expected continuation text in render node" + ) + XCTAssertFalse( + renderNodeString.contains("\"kind\" : \"content\""), + "Continuation lines should not create a separate Discussion section" + ) + } + func testStructure() async throws { try await renderDocumentation( markedText: """ @@ -551,6 +617,7 @@ final class DoccDocumentationTests: SourceKitLSPTestCase { ) } + // MARK: Markdown Articles and Extensions func testMarkdownArticle() async throws {