Skip to content

Commit 24118eb

Browse files
cleanup unused code
1 parent 39bf485 commit 24118eb

File tree

6 files changed

+140
-138
lines changed

6 files changed

+140
-138
lines changed

Sources/SourceKitLSP/Documentation/DocCCatalogIndexManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final actor DocCCatalogIndexManager {
3232
}
3333
}
3434

35-
func index(for catalogURL: URL, moduleName: String?) async throws(DocCIndexError) -> DocCCatalogIndex {
35+
func index(for catalogURL: URL) async throws(DocCIndexError) -> DocCCatalogIndex {
3636
if let existingCatalog = catalogToIndexMap[catalogURL] {
3737
return try existingCatalog.get()
3838
}
@@ -43,7 +43,7 @@ final actor DocCCatalogIndexManager {
4343
documentPathsToConvert: [],
4444
includeRenderReferenceStore: true,
4545
documentationBundleLocation: catalogURL,
46-
documentationBundleDisplayName: moduleName ?? "unknown",
46+
documentationBundleDisplayName: "unknown",
4747
documentationBundleIdentifier: "unknown",
4848
symbolGraphs: [],
4949
emitSymbolSourceFileURIs: true,

Sources/SourceKitLSP/Documentation/DocCReferenceResolutionService.swift

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,7 @@ final class DocCReferenceResolutionService: DocumentationService, Sendable {
118118
}
119119
switch request.payload {
120120
case .symbol(let symbolUSR):
121-
guard let index = context.uncheckedIndex?.checked(for: .deletedFiles) else {
122-
completion(.failure(.indexNotAvailable))
123-
return
124-
}
125-
guard let symbolOccurrence = index.primaryDefinitionOrDeclarationOccurrence(ofUSR: symbolUSR),
126-
let symbolURL = symbolOccurrence.location.documentUri.fileURL
127-
else {
128-
completion(.failure(.symbolNotFound(symbolUSR)))
129-
return
130-
}
131-
completion(
132-
.success(
133-
.resolvedInformation(
134-
OutOfProcessReferenceResolver.ResolvedInformation(
135-
symbolURL: symbolURL,
136-
symbolName: symbolOccurrence.symbol.name
137-
)
138-
)
139-
)
140-
)
121+
completion(.failure(.symbolNotFound(symbolUSR)))
141122
case .asset(let assetReference):
142123
guard let catalog = context.catalogIndex else {
143124
completion(.failure(.indexNotAvailable))
@@ -209,7 +190,6 @@ final class DocCReferenceResolutionService: DocumentationService, Sendable {
209190

210191
struct DocCReferenceResolutionContext {
211192
let catalogURL: URL?
212-
let uncheckedIndex: UncheckedIndex?
213193
let catalogIndex: DocCCatalogIndex?
214194
}
215195

Sources/SourceKitLSP/Documentation/DocCBuildInformation.swift renamed to Sources/SourceKitLSP/Documentation/DocCWorkspaceExtensions.swift

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@ import BuildSystemIntegration
1616
import Foundation
1717
import LanguageServerProtocol
1818

19-
struct DocCBuildInformation {
20-
let catalogURL: URL?
21-
let moduleName: String?
22-
let catalogIndex: DocCCatalogIndex?
23-
24-
init(catalogURL: URL? = nil, moduleName: String? = nil, catalogIndex: DocCCatalogIndex? = nil) {
25-
self.catalogURL = catalogURL
26-
self.moduleName = moduleName
27-
self.catalogIndex = catalogIndex
28-
}
29-
}
30-
3119
extension Workspace {
3220
private var documentationManager: DocumentationManager {
3321
get throws {
@@ -38,29 +26,29 @@ extension Workspace {
3826
}
3927
}
4028

41-
func doccBuildInformation(for document: DocumentURI) async -> DocCBuildInformation {
42-
let target = await buildSystemManager.canonicalTarget(for: document)
43-
guard let target else {
44-
return DocCBuildInformation()
29+
func findModuleName(for document: DocumentURI) async -> String? {
30+
guard let target = await buildSystemManager.canonicalTarget(for: document) else {
31+
return nil
4532
}
4633
let sourceFiles = (try? await buildSystemManager.sourceFiles(in: [target]).flatMap(\.sources)) ?? []
47-
var moduleName: String? = nil
48-
let catalogURL: URL? = sourceFiles.compactMap(\.uri.fileURL?.doccCatalogURL).first
4934
for sourceFile in sourceFiles {
5035
let language = await buildSystemManager.defaultLanguage(for: sourceFile.uri, in: target)
5136
guard language == .swift else {
5237
continue
5338
}
54-
moduleName = await buildSystemManager.moduleName(for: sourceFile.uri, in: target)
55-
if moduleName != nil {
56-
break
39+
if let moduleName = await buildSystemManager.moduleName(for: sourceFile.uri, in: target) {
40+
return moduleName
5741
}
5842
}
59-
var catalogIndex: DocCCatalogIndex? = nil
60-
if let catalogURL {
61-
catalogIndex = try? await documentationManager.catalogIndex(for: catalogURL, moduleName: moduleName)
43+
return nil
44+
}
45+
46+
func findDocCCatalog(for document: DocumentURI) async -> URL? {
47+
guard let target = await buildSystemManager.canonicalTarget(for: document) else {
48+
return nil
6249
}
63-
return DocCBuildInformation(catalogURL: catalogURL, moduleName: moduleName, catalogIndex: catalogIndex)
50+
let sourceFiles = (try? await buildSystemManager.sourceFiles(in: [target]).flatMap(\.sources)) ?? []
51+
return sourceFiles.compactMap(\.uri.fileURL?.doccCatalogURL).first
6452
}
6553
}
6654

Sources/SourceKitLSP/Documentation/DoccDocumentationHandler.swift

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@
1313
#if canImport(SwiftDocC)
1414
import Foundation
1515
import IndexStoreDB
16+
package import LanguageServerProtocol
1617
import Markdown
1718
import SemanticIndex
18-
import SymbolKit
19-
20-
#if compiler(>=6)
21-
package import LanguageServerProtocol
22-
#else
23-
import LanguageServerProtocol
24-
#endif
19+
import SKUtilities
2520

2621
extension DocumentationLanguageService {
2722
private var documentationManager: DocumentationManager {
@@ -41,37 +36,28 @@ extension DocumentationLanguageService {
4136
guard let workspace = await sourceKitLSPServer.workspaceForDocument(uri: req.textDocument.uri) else {
4237
throw ResponseError.workspaceNotOpen(req.textDocument.uri)
4338
}
44-
let doccBuildInfo = await workspace.doccBuildInformation(for: req.textDocument.uri)
45-
guard let fileContents = snapshot.text.data(using: .utf8) else {
46-
throw ResponseError.internalError("Failed to encode file contents")
47-
}
39+
let moduleName = await workspace.findModuleName(for: req.textDocument.uri)
40+
let catalogURL = await workspace.findDocCCatalog(for: req.textDocument.uri)
4841

49-
var externalIDsToConvert: [String]? = nil
50-
var symbolGraphs: [Data] = []
51-
var markupFiles: [Data] = []
52-
var tutorialFiles: [Data] = []
5342
switch snapshot.language {
5443
case .tutorial:
55-
tutorialFiles.append(fileContents)
44+
return try await documentationManager.renderDocCDocumentation(
45+
tutorialFile: snapshot.text,
46+
moduleName: moduleName,
47+
catalogURL: catalogURL
48+
)
5649
case .markdown:
57-
markupFiles.append(fileContents)
5850
if case let .symbol(symbolName) = MarkdownTitleFinder.find(parsing: snapshot.text) {
59-
if let moduleName = doccBuildInfo.moduleName, symbolName == moduleName {
51+
if let moduleName = moduleName, symbolName == moduleName {
6052
// This is a page representing the module itself.
6153
// Create a dummy symbol graph and tell SwiftDocC to convert the module name.
62-
externalIDsToConvert = [moduleName]
63-
symbolGraphs.append(
64-
try JSONEncoder().encode(
65-
SymbolGraph(
66-
metadata: SymbolGraph.Metadata(
67-
formatVersion: SymbolGraph.SemanticVersion(major: 0, minor: 5, patch: 0),
68-
generator: "SourceKit-LSP"
69-
),
70-
module: SymbolGraph.Module(name: moduleName, platform: SymbolGraph.Platform()),
71-
symbols: [],
72-
relationships: []
73-
)
74-
)
54+
let emptySymbolGraph = try await documentationManager.emptySymbolGraph(moduleName: moduleName)
55+
return try await documentationManager.renderDocCDocumentation(
56+
symbolUSR: moduleName,
57+
symbolGraph: emptySymbolGraph,
58+
markupFile: snapshot.text,
59+
moduleName: moduleName,
60+
catalogURL: catalogURL
7561
)
7662
} else {
7763
// This is a symbol extension page. Find the symbol so that we can include it in the request.
@@ -106,24 +92,27 @@ extension DocumentationLanguageService {
10692
includeSymbolGraph: true,
10793
fallbackSettingsAfterTimeout: false
10894
)
109-
guard let symbolGraph = cursorInfo.symbolGraph, let rawSymbolGraph = symbolGraph.data(using: .utf8) else {
95+
guard let symbolGraph = cursorInfo.symbolGraph else {
11096
throw ResponseError.internalError("Unable to retrieve symbol graph for \(symbolOccurrence.symbol.name)")
11197
}
112-
externalIDsToConvert = [symbolOccurrence.symbol.usr]
113-
symbolGraphs.append(rawSymbolGraph)
98+
return try await documentationManager.renderDocCDocumentation(
99+
symbolUSR: symbolOccurrence.symbol.usr,
100+
symbolGraph: symbolGraph,
101+
markupFile: snapshot.text,
102+
moduleName: moduleName,
103+
catalogURL: catalogURL
104+
)
114105
}
115106
}
107+
// This is an article that can be rendered on its own
108+
return try await documentationManager.renderDocCDocumentation(
109+
markupFile: snapshot.text,
110+
moduleName: moduleName,
111+
catalogURL: catalogURL
112+
)
116113
default:
117114
throw ResponseError.requestFailed(doccDocumentationError: .noDocumentation)
118115
}
119-
return try await documentationManager.convertDocumentation(
120-
workspace: workspace,
121-
buildInformation: doccBuildInfo,
122-
externalIDsToConvert: externalIDsToConvert,
123-
symbolGraphs: symbolGraphs,
124-
markupFiles: markupFiles,
125-
tutorialFiles: tutorialFiles
126-
)
127116
}
128117
}
129118

Sources/SourceKitLSP/Documentation/DocumentationManager.swift

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,68 @@ package final actor DocumentationManager {
4949
await catalogIndexManager.invalidate(catalogURLs: affectedCatalogURLs)
5050
}
5151

52-
func catalogIndex(for catalogURL: URL, moduleName: String?) async throws(DocCIndexError) -> DocCCatalogIndex {
53-
try await catalogIndexManager.index(for: catalogURL, moduleName: moduleName)
52+
func emptySymbolGraph(moduleName: String) throws -> String {
53+
let data = try JSONEncoder().encode(
54+
SymbolGraph(
55+
metadata: SymbolGraph.Metadata(
56+
formatVersion: SymbolGraph.SemanticVersion(major: 0, minor: 5, patch: 0),
57+
generator: "SourceKit-LSP"
58+
),
59+
module: SymbolGraph.Module(name: moduleName, platform: SymbolGraph.Platform()),
60+
symbols: [],
61+
relationships: []
62+
)
63+
)
64+
guard let result = String(data: data, encoding: .utf8) else {
65+
throw ResponseError.internalError("Failed to encode symbol graph")
66+
}
67+
return result
5468
}
5569

56-
func convertDocumentation(
57-
workspace: Workspace,
58-
buildInformation: DocCBuildInformation,
59-
externalIDsToConvert: [String]? = nil,
60-
symbolGraphs: [Data] = [],
61-
overridingDocumentationComments: [String: [String]] = [:],
62-
markupFiles: [Data] = [],
63-
tutorialFiles: [Data] = []
70+
func catalogIndex(for catalogURL: URL) async throws(DocCIndexError) -> DocCCatalogIndex {
71+
try await catalogIndexManager.index(for: catalogURL)
72+
}
73+
74+
func renderDocCDocumentation(
75+
symbolUSR: String? = nil,
76+
symbolGraph: String? = nil,
77+
overrideDocComments: [String]? = nil,
78+
markupFile: String? = nil,
79+
tutorialFile: String? = nil,
80+
moduleName: String?,
81+
catalogURL: URL?
6482
) async throws -> DoccDocumentationResponse {
83+
// Make inputs consumable by DocC
84+
var externalIDsToConvert: [String]? = nil
85+
var overridingDocumentationComments = [String: [String]]()
86+
if let symbolUSR {
87+
externalIDsToConvert = [symbolUSR]
88+
if let overrideDocComments {
89+
overridingDocumentationComments[symbolUSR] = overrideDocComments
90+
}
91+
}
92+
var symbolGraphs = [Data]()
93+
if let symbolGraphData = symbolGraph?.data(using: .utf8) {
94+
symbolGraphs.append(symbolGraphData)
95+
}
96+
var markupFiles = [Data]()
97+
if let markupFile = markupFile?.data(using: .utf8) {
98+
markupFiles.append(markupFile)
99+
}
100+
var tutorialFiles = [Data]()
101+
if let tutorialFile = tutorialFile?.data(using: .utf8) {
102+
tutorialFiles.append(tutorialFile)
103+
}
65104
// Store the convert request identifier in order to fulfill index requests from SwiftDocC
66105
let convertRequestIdentifier = UUID().uuidString
106+
var catalogIndex: DocCCatalogIndex? = nil
107+
if let catalogURL {
108+
catalogIndex = try await self.catalogIndex(for: catalogURL)
109+
}
67110
referenceResolutionService.addContext(
68111
DocCReferenceResolutionContext(
69-
catalogURL: buildInformation.catalogURL,
70-
uncheckedIndex: workspace.uncheckedIndex,
71-
catalogIndex: buildInformation.catalogIndex
112+
catalogURL: catalogURL,
113+
catalogIndex: catalogIndex
72114
),
73115
withKey: convertRequestIdentifier
74116
)
@@ -78,7 +120,7 @@ package final actor DocumentationManager {
78120
documentPathsToConvert: nil,
79121
includeRenderReferenceStore: false,
80122
documentationBundleLocation: nil,
81-
documentationBundleDisplayName: buildInformation.moduleName ?? "Unknown",
123+
documentationBundleDisplayName: moduleName ?? "Unknown",
82124
documentationBundleIdentifier: "unknown",
83125
symbolGraphs: symbolGraphs,
84126
overridingDocumentationComments: overridingDocumentationComments,

0 commit comments

Comments
 (0)