Skip to content

Commit 242609d

Browse files
authored
Merge pull request #1959 from matthewbastien/markdown-documentation
Handle Markdown and Tutorial files in textDocument/doccDocumentation
2 parents ab64878 + 17daedd commit 242609d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2119
-391
lines changed

Contributor Documentation/BSP Extensions.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,13 @@ export interface PrepareParams {
7676
```ts
7777
export interface SourceKitSourceItemData {
7878
/** The language of the source file. If `nil`, the language is inferred from the file extension. */
79-
language? LanguageId;
79+
language?: LanguageId;
8080

81-
/** Whether the file is a header file that is clearly associated with one target.
82-
*
83-
* For example header files in SwiftPM projects are always associated to one target and SwiftPM can provide build
84-
* settings for that header file.
85-
*
86-
* In general, build systems don't need to list all header files in the `buildTarget/sources` request: Semantic
87-
* functionality for header files is usually provided by finding a main file that includes the header file and
88-
* inferring build settings from it. Listing header files in `buildTarget/sources` allows SourceKit-LSP to provide
89-
* semantic functionality for header files if they haven't been included by any main file. **/
90-
isHeader?: bool;
81+
/**
82+
* The kind of source file that this source item represents. If omitted, the item is assumed to be a normal source
83+
* file, ie. omitting this key is equivalent to specifying it as `source`.
84+
*/
85+
kind?: "source" | "header" | "doccCatalog";
9186

9287
/**
9388
* The output path that is during indexing for this file, ie. the `-index-unit-output-path`, if it is specified

Package.swift

+25-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ var targets: [Target] = [
214214
swiftSettings: globalSwiftSettings
215215
),
216216

217+
// MARK: DocCDocumentation
218+
219+
.target(
220+
name: "DocCDocumentation",
221+
dependencies: [
222+
"BuildServerProtocol",
223+
"BuildSystemIntegration",
224+
"LanguageServerProtocol",
225+
"SemanticIndex",
226+
"SKLogging",
227+
"SwiftExtensions",
228+
.product(name: "IndexStoreDB", package: "indexstore-db"),
229+
.product(name: "SwiftDocC", package: "swift-docc"),
230+
.product(name: "SymbolKit", package: "swift-docc-symbolkit"),
231+
],
232+
exclude: ["CMakeLists.txt"],
233+
swiftSettings: globalSwiftSettings
234+
),
235+
217236
// MARK: InProcessClient
218237

219238
.target(
@@ -476,6 +495,7 @@ var targets: [Target] = [
476495
dependencies: [
477496
"BuildServerProtocol",
478497
"BuildSystemIntegration",
498+
"DocCDocumentation",
479499
"LanguageServerProtocol",
480500
"LanguageServerProtocolExtensions",
481501
"LanguageServerProtocolJSONRPC",
@@ -487,9 +507,9 @@ var targets: [Target] = [
487507
"SwiftExtensions",
488508
"ToolchainRegistry",
489509
"TSCExtensions",
490-
.product(name: "SwiftDocC", package: "swift-docc"),
491510
.product(name: "IndexStoreDB", package: "indexstore-db"),
492511
.product(name: "Crypto", package: "swift-crypto"),
512+
.product(name: "Markdown", package: "swift-markdown"),
493513
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
494514
]
495515
+ swiftPMDependency([
@@ -772,6 +792,8 @@ var dependencies: [Package.Dependency] {
772792
return [
773793
.package(path: "../indexstore-db"),
774794
.package(path: "../swift-docc"),
795+
.package(path: "../swift-docc-symbolkit"),
796+
.package(path: "../swift-markdown"),
775797
.package(path: "../swift-tools-support-core"),
776798
.package(path: "../swift-argument-parser"),
777799
.package(path: "../swift-syntax"),
@@ -783,6 +805,8 @@ var dependencies: [Package.Dependency] {
783805
return [
784806
.package(url: "https://github.com/swiftlang/indexstore-db.git", branch: relatedDependenciesBranch),
785807
.package(url: "https://github.com/swiftlang/swift-docc.git", branch: relatedDependenciesBranch),
808+
.package(url: "https://github.com/swiftlang/swift-docc-symbolkit.git", branch: relatedDependenciesBranch),
809+
.package(url: "https://github.com/swiftlang/swift-markdown.git", branch: relatedDependenciesBranch),
786810
.package(url: "https://github.com/apple/swift-tools-support-core.git", branch: relatedDependenciesBranch),
787811
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
788812
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: relatedDependenciesBranch),

Sources/BuildServerProtocol/Messages/BuildTargetSourcesRequest.swift

+28-11
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ public struct SourceItemDataKind: RawRepresentable, Codable, Hashable, Sendable
119119
}
120120

121121
/// **(BSP Extension)**
122-
public struct SourceKitSourceItemData: LSPAnyCodable, Codable {
123-
/// The language of the source file. If `nil`, the language is inferred from the file extension.
124-
public var language: Language?
125122

126-
/// Whether the file is a header file that is clearly associated with one target.
123+
public enum SourceKitSourceItemKind: String, Codable {
124+
/// A source file that belongs to the target
125+
case source = "source"
126+
127+
/// A header file that is clearly associated with one target.
127128
///
128129
/// For example header files in SwiftPM projects are always associated to one target and SwiftPM can provide build
129130
/// settings for that header file.
@@ -132,7 +133,19 @@ public struct SourceKitSourceItemData: LSPAnyCodable, Codable {
132133
/// functionality for header files is usually provided by finding a main file that includes the header file and
133134
/// inferring build settings from it. Listing header files in `buildTarget/sources` allows SourceKit-LSP to provide
134135
/// semantic functionality for header files if they haven't been included by any main file.
135-
public var isHeader: Bool?
136+
case header = "header"
137+
138+
/// A SwiftDocC documentation catalog usually ending in the ".docc" extension.
139+
case doccCatalog = "doccCatalog"
140+
}
141+
142+
public struct SourceKitSourceItemData: LSPAnyCodable, Codable {
143+
/// The language of the source file. If `nil`, the language is inferred from the file extension.
144+
public var language: Language?
145+
146+
/// The kind of source file that this source item represents. If omitted, the item is assumed to be a normal source file,
147+
/// ie. omitting this key is equivalent to specifying it as `source`.
148+
public var kind: SourceKitSourceItemKind?
136149

137150
/// The output path that is used during indexing for this file, ie. the `-index-unit-output-path`, if it is specified
138151
/// in the compiler arguments or the file that is passed as `-o`, if `-index-unit-output-path` is not specified.
@@ -144,18 +157,22 @@ public struct SourceKitSourceItemData: LSPAnyCodable, Codable {
144157
/// `outputPathsProvider: true` in `SourceKitInitializeBuildResponseData`.
145158
public var outputPath: String?
146159

147-
public init(language: Language? = nil, isHeader: Bool? = nil, outputPath: String? = nil) {
160+
public init(language: Language? = nil, kind: SourceKitSourceItemKind? = nil, outputPath: String? = nil) {
148161
self.language = language
149-
self.isHeader = isHeader
162+
self.kind = kind
150163
self.outputPath = outputPath
151164
}
152165

153166
public init?(fromLSPDictionary dictionary: [String: LanguageServerProtocol.LSPAny]) {
154167
if case .string(let language) = dictionary[CodingKeys.language.stringValue] {
155168
self.language = Language(rawValue: language)
156169
}
157-
if case .bool(let isHeader) = dictionary[CodingKeys.isHeader.stringValue] {
158-
self.isHeader = isHeader
170+
if case .string(let rawKind) = dictionary[CodingKeys.kind.stringValue] {
171+
self.kind = SourceKitSourceItemKind(rawValue: rawKind)
172+
}
173+
// Backwards compatibility for isHeader
174+
if case .bool(let isHeader) = dictionary["isHeader"], isHeader {
175+
self.kind = .header
159176
}
160177
if case .string(let outputFilePath) = dictionary[CodingKeys.outputPath.stringValue] {
161178
self.outputPath = outputFilePath
@@ -167,8 +184,8 @@ public struct SourceKitSourceItemData: LSPAnyCodable, Codable {
167184
if let language {
168185
result[CodingKeys.language.stringValue] = .string(language.rawValue)
169186
}
170-
if let isHeader {
171-
result[CodingKeys.isHeader.stringValue] = .bool(isHeader)
187+
if let kind {
188+
result[CodingKeys.kind.stringValue] = .string(kind.rawValue)
172189
}
173190
if let outputPath {
174191
result[CodingKeys.outputPath.stringValue] = .string(outputPath)

Sources/BuildSystemIntegration/BuildSystemManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
12661266
isPartOfRootProject: isPartOfRootProject,
12671267
mayContainTests: mayContainTests,
12681268
isBuildable: !(target?.tags.contains(.notBuildable) ?? false)
1269-
&& !(sourceKitData?.isHeader ?? false)
1269+
&& (sourceKitData?.kind ?? .source) == .source
12701270
)
12711271
switch sourceItem.kind {
12721272
case .file:

Sources/BuildSystemIntegration/FileBuildSettings.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ package struct FileBuildSettings: Equatable, Sendable {
5555
///
5656
/// This patches the arguments by searching for the argument corresponding to
5757
/// `originalFile` and replacing it.
58-
func patching(newFile: DocumentURI, originalFile: DocumentURI) -> FileBuildSettings {
58+
package func patching(newFile: DocumentURI, originalFile: DocumentURI) -> FileBuildSettings {
5959
var arguments = self.compilerArguments
6060
// URL.lastPathComponent is only set for file URLs but we want to also infer a file extension for non-file URLs like
6161
// untitled:file.cpp

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

+15-8
Original file line numberDiff line numberDiff line change
@@ -593,16 +593,23 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
593593
kind: .file,
594594
generated: false,
595595
dataKind: .sourceKit,
596-
data: SourceKitSourceItemData(isHeader: true).encodeToLSPAny()
597-
)
598-
}
599-
sources += (swiftPMTarget.resources + swiftPMTarget.ignored + swiftPMTarget.others).map {
600-
SourceItem(
601-
uri: DocumentURI($0),
602-
kind: $0.isDirectory ? .directory : .file,
603-
generated: false
596+
data: SourceKitSourceItemData(kind: .header).encodeToLSPAny()
604597
)
605598
}
599+
sources += (swiftPMTarget.resources + swiftPMTarget.ignored + swiftPMTarget.others)
600+
.map { (url: URL) -> SourceItem in
601+
var data: SourceKitSourceItemData? = nil
602+
if url.isDirectory, url.pathExtension == "docc" {
603+
data = SourceKitSourceItemData(kind: .doccCatalog)
604+
}
605+
return SourceItem(
606+
uri: DocumentURI(url),
607+
kind: url.isDirectory ? .directory : .file,
608+
generated: false,
609+
dataKind: data != nil ? .sourceKit : nil,
610+
data: data?.encodeToLSPAny()
611+
)
612+
}
606613
result.append(SourcesItem(target: target, sources: sources))
607614
}
608615
return BuildTargetSourcesResponse(items: result)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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+
package import BuildServerProtocol
14+
package import BuildSystemIntegration
15+
package import Foundation
16+
import LanguageServerProtocol
17+
import SKLogging
18+
19+
package extension BuildSystemManager {
20+
/// Retrieves the name of the Swift module for a given target.
21+
///
22+
/// **Note:** prefer using ``module(for:in:)`` over ths function. This function
23+
/// only exists for cases where you want the Swift module name of a target where
24+
/// you don't know one of its Swift document URIs in advance. E.g. when handling
25+
/// requests for Markdown/Tutorial files in DocC since they don't have compile
26+
/// commands that could be used to find the module name.
27+
///
28+
/// - Parameter target: The build target identifier
29+
/// - Returns: The name of the Swift module or nil if it could not be determined
30+
func moduleName(for target: BuildTargetIdentifier) async -> String? {
31+
let sourceFiles =
32+
await orLog(
33+
"Failed to retreive source files from target \(target.uri)",
34+
{ try await self.sourceFiles(in: [target]).flatMap(\.sources) }
35+
) ?? []
36+
for sourceFile in sourceFiles {
37+
let language = await defaultLanguage(for: sourceFile.uri, in: target)
38+
guard language == .swift else {
39+
continue
40+
}
41+
if let moduleName = await moduleName(for: sourceFile.uri, in: target) {
42+
return moduleName
43+
}
44+
}
45+
return nil
46+
}
47+
48+
/// Finds the SwiftDocC documentation catalog associated with a target, if any.
49+
///
50+
/// - Parameter target: The build target identifier
51+
/// - Returns: The URL of the documentation catalog or nil if one could not be found
52+
func doccCatalog(for target: BuildTargetIdentifier) async -> URL? {
53+
let sourceFiles =
54+
await orLog(
55+
"Failed to retrieve source files from target \(target.uri)",
56+
{ try await self.sourceFiles(in: [target]).flatMap(\.sources) }
57+
) ?? []
58+
let catalogURLs = sourceFiles.compactMap { sourceItem -> URL? in
59+
guard sourceItem.dataKind == .sourceKit,
60+
let data = SourceKitSourceItemData(fromLSPAny: sourceItem.data),
61+
data.kind == .doccCatalog
62+
else {
63+
return nil
64+
}
65+
return sourceItem.uri.fileURL
66+
}.sorted(by: { $0.absoluteString < $1.absoluteString })
67+
if catalogURLs.count > 1 {
68+
logger.error("Multiple SwiftDocC catalogs found in build target \(target.uri)")
69+
}
70+
return catalogURLs.first
71+
}
72+
}

0 commit comments

Comments
 (0)