Skip to content

Commit 10c8991

Browse files
authored
Merge pull request #2242 from ahoppen/move-docc-service
2 parents be1d9a6 + 615dead commit 10c8991

File tree

12 files changed

+62
-29
lines changed

12 files changed

+62
-29
lines changed

Package.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,22 @@ var targets: [Target] = [
237237
swiftSettings: globalSwiftSettings
238238
),
239239

240-
// MARK: DocCDocumentation
240+
// MARK: DocumentationLanguageService
241+
242+
.target(
243+
name: "DocumentationLanguageService",
244+
dependencies: [
245+
"BuildServerIntegration",
246+
"DocCDocumentation",
247+
"LanguageServerProtocol",
248+
"SKUtilities",
249+
"SourceKitLSP",
250+
"SemanticIndex",
251+
.product(name: "Markdown", package: "swift-markdown"),
252+
],
253+
exclude: [],
254+
swiftSettings: globalSwiftSettings
255+
),
241256

242257
.target(
243258
name: "DocCDocumentation",
@@ -263,6 +278,7 @@ var targets: [Target] = [
263278
dependencies: [
264279
"BuildServerIntegration",
265280
"ClangLanguageService",
281+
"DocumentationLanguageService",
266282
"LanguageServerProtocol",
267283
"SKLogging",
268284
"SKOptions",

Sources/ClangLanguageService/ClangLanguageService.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ package import SwiftSyntax
2424
import TSCExtensions
2525
package import ToolchainRegistry
2626

27-
#if canImport(DocCDocumentation)
28-
import DocCDocumentation
29-
#endif
30-
3127
#if os(Windows)
3228
import WinSDK
3329
#endif
@@ -497,16 +493,16 @@ extension ClangLanguageService {
497493
return try await forwardRequestToClangd(req)
498494
}
499495

500-
#if canImport(DocCDocumentation)
501496
package func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
502497
guard let sourceKitLSPServer else {
503498
throw ResponseError.unknown("Connection to the editor closed")
504499
}
505500

506501
let snapshot = try sourceKitLSPServer.documentManager.latestSnapshot(req.textDocument.uri)
507-
throw ResponseError.requestFailed(doccDocumentationError: .unsupportedLanguage(snapshot.language))
502+
throw ResponseError.requestFailed(
503+
"Documentation preview is not available for \(snapshot.language.description) files"
504+
)
508505
}
509-
#endif
510506

511507
package func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] {
512508
return try await forwardRequestToClangd(req)

Sources/SourceKitLSP/Documentation/DoccDocumentationHandler.swift renamed to Sources/DocumentationLanguageService/DoccDocumentationHandler.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Foundation
1818
package import LanguageServerProtocol
1919
import Markdown
2020
import SKUtilities
21+
import SourceKitLSP
2122
import SemanticIndex
2223

2324
extension DocumentationLanguageService {
@@ -63,9 +64,13 @@ extension DocumentationLanguageService {
6364
ofDocCSymbolLink: symbolLink,
6465
fetchSymbolGraph: { location in
6566
guard let symbolWorkspace = try await workspaceForDocument(uri: location.documentUri),
66-
let languageService = try await languageService(for: location.documentUri, .swift, in: symbolWorkspace)
67+
let languageService = await sourceKitLSPServer.languageService(
68+
for: location.documentUri,
69+
.swift,
70+
in: symbolWorkspace
71+
)
6772
else {
68-
throw ResponseError.internalError("Unable to find Swift language service for \(location.documentUri)")
73+
throw ResponseError.internalError("Unable to find language service for \(location.documentUri)")
6974
}
7075
return try await languageService.symbolGraph(forOnDiskContentsOf: location.documentUri, at: location)
7176
}
@@ -76,9 +81,13 @@ extension DocumentationLanguageService {
7681
let symbolDocumentUri = symbolOccurrence.location.documentUri
7782
guard
7883
let symbolWorkspace = try await workspaceForDocument(uri: symbolDocumentUri),
79-
let languageService = try await languageService(for: symbolDocumentUri, .swift, in: symbolWorkspace)
84+
let languageService = await sourceKitLSPServer.languageService(
85+
for: symbolDocumentUri,
86+
.swift,
87+
in: symbolWorkspace
88+
)
8089
else {
81-
throw ResponseError.internalError("Unable to find Swift language service for \(symbolDocumentUri)")
90+
throw ResponseError.internalError("Unable to find language service for \(symbolDocumentUri)")
8291
}
8392
let symbolGraph = try await languageService.symbolGraph(
8493
forOnDiskContentsOf: symbolDocumentUri,

Sources/SourceKitLSP/Documentation/DocumentationLanguageService.swift renamed to Sources/DocumentationLanguageService/DocumentationLanguageService.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Foundation
1414
package import IndexStoreDB
1515
package import LanguageServerProtocol
1616
package import SKOptions
17+
package import SourceKitLSP
1718
import SwiftExtensions
1819
package import SwiftSyntax
1920
package import ToolchainRegistry
@@ -52,7 +53,7 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
5253

5354
func languageService(
5455
for uri: DocumentURI,
55-
_ language: Language,
56+
_ language: LanguageServerProtocol.Language,
5657
in workspace: Workspace
5758
) async throws -> LanguageService? {
5859
guard let sourceKitLSPServer else {

Sources/InProcessClient/LanguageServiceRegistry+staticallyKnownServices.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ import LanguageServerProtocol
1515
package import SourceKitLSP
1616
import SwiftLanguageService
1717

18+
#if canImport(DocumentationLanguageService)
19+
import DocumentationLanguageService
20+
#endif
21+
1822
extension LanguageServiceRegistry {
1923
/// All types conforming to `LanguageService` that are known at compile time.
2024
package static let staticallyKnownServices = {
2125
var registry = LanguageServiceRegistry()
2226
registry.register(ClangLanguageService.self, for: [.c, .cpp, .objective_c, .objective_cpp])
2327
registry.register(SwiftLanguageService.self, for: [.swift])
28+
#if canImport(DocumentationLanguageService)
2429
registry.register(DocumentationLanguageService.self, for: [.markdown, .tutorial])
30+
#endif
2531
return registry
2632
}()
2733
}

Sources/LanguageServerProtocol/Error.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public struct ErrorCode: RawRepresentable, Codable, Hashable, Sendable {
8383

8484
// MARK: SourceKit-LSP specific error codes
8585
public static let workspaceNotOpen: ErrorCode = ErrorCode(rawValue: -32003)
86+
87+
/// The method is not implemented in this `LanguageService`.
88+
public static let requestNotImplemented: ErrorCode = ErrorCode(rawValue: -32004)
8689
}
8790

8891
/// An error response represented by a code and message.
@@ -131,6 +134,10 @@ extension ResponseError {
131134
public static func internalError(_ message: String) -> ResponseError {
132135
return ResponseError(code: .internalError, message: message)
133136
}
137+
138+
public static func requestNotImplemented(_ method: any RequestType.Type) -> ResponseError {
139+
return ResponseError(code: .requestNotImplemented, message: "request not implemented: \(method.method)")
140+
}
134141
}
135142

136143
/// An error during message decoding.

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ add_library(SourceKitLSP STATIC
2626
TextEdit+IsNoop.swift
2727
Workspace.swift
2828
)
29-
target_sources(SourceKitLSP PRIVATE
30-
Documentation/DocCDocumentationHandler.swift
31-
Documentation/DocumentationLanguageService.swift
32-
)
3329
set_target_properties(SourceKitLSP PROPERTIES
3430
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}
3531
)

Sources/SourceKitLSP/LanguageService.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ package protocol LanguageService: AnyObject, Sendable {
176176
func completion(_ req: CompletionRequest) async throws -> CompletionList
177177
func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem
178178
func hover(_ req: HoverRequest) async throws -> HoverResponse?
179-
#if canImport(DocCDocumentation)
180179
func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse
181-
#endif
182180
func symbolInfo(_ request: SymbolInfoRequest) async throws -> [SymbolDetails]
183181

184182
/// Return the symbol graph at the given location for the contents of the document as they are on-disk (opposed to the

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,8 @@ extension SourceKitLSPServer: QueueBasedMessageHandler {
757757
await self.handleRequest(for: request, requestHandler: self.declaration)
758758
case let request as RequestAndReply<DefinitionRequest>:
759759
await self.handleRequest(for: request, requestHandler: self.definition)
760-
#if canImport(DocCDocumentation)
761760
case let request as RequestAndReply<DoccDocumentationRequest>:
762761
await self.handleRequest(for: request, requestHandler: self.doccDocumentation)
763-
#endif
764762
case let request as RequestAndReply<DocumentColorRequest>:
765763
await self.handleRequest(for: request, requestHandler: self.documentColor)
766764
case let request as RequestAndReply<DocumentDiagnosticsRequest>:
@@ -1573,15 +1571,13 @@ extension SourceKitLSPServer {
15731571
return try await documentService(for: uri).completionItemResolve(request)
15741572
}
15751573

1576-
#if canImport(DocCDocumentation)
15771574
func doccDocumentation(
15781575
_ req: DoccDocumentationRequest,
15791576
workspace: Workspace,
15801577
languageService: LanguageService
15811578
) async throws -> DoccDocumentationResponse {
15821579
return try await languageService.doccDocumentation(req)
15831580
}
1584-
#endif
15851581

15861582
func hover(
15871583
_ req: HoverRequest,

Sources/SwiftLanguageService/DoccDocumentation.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if canImport(DocCDocumentation)
1413
import BuildServerIntegration
15-
import DocCDocumentation
1614
import Foundation
1715
import IndexStoreDB
1816
package import LanguageServerProtocol
19-
import SemanticIndex
2017
import SKLogging
18+
import SemanticIndex
19+
import SourceKitLSP
2120
import SwiftExtensions
2221
import SwiftSyntax
23-
import SourceKitLSP
22+
23+
#if canImport(DocCDocumentation)
24+
import DocCDocumentation
25+
#endif
2426

2527
extension SwiftLanguageService {
2628
package func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
29+
#if canImport(DocCDocumentation)
2730
guard let sourceKitLSPServer else {
2831
throw ResponseError.internalError("SourceKit-LSP is shutting down")
2932
}
@@ -100,8 +103,12 @@ extension SwiftLanguageService {
100103
moduleName: moduleName,
101104
catalogURL: catalogURL
102105
)
106+
#else
107+
throw ResponseError.requestFailed("Documentation preview is not available in this build of SourceKit-LSP")
108+
#endif
103109
}
104110

111+
#if canImport(DocCDocumentation)
105112
private func findMarkupExtensionFile(
106113
workspace: Workspace,
107114
documentationManager: DocCDocumentationManager,
@@ -127,6 +134,7 @@ extension SwiftLanguageService {
127134
language: .markdown
128135
)?.text
129136
}
137+
#endif
130138
}
131139

132140
private struct DocumentableSymbol {
@@ -216,4 +224,3 @@ private struct DocumentableSymbol {
216224
return nil
217225
}
218226
}
219-
#endif

0 commit comments

Comments
 (0)