Skip to content

Commit 1c65bda

Browse files
remove some nesting levels in DocumentationLanguageService.doccDocumentation(_:)
1 parent 9cdd1ac commit 1c65bda

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

Sources/SourceKitLSP/Documentation/DoccDocumentationHandler.swift

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,15 @@ extension DocumentationLanguageService {
4646
catalogURL: catalogURL
4747
)
4848
case .markdown:
49-
if case let .symbol(symbolName) = MarkdownTitleFinder.find(parsing: snapshot.text) {
50-
if let moduleName, symbolName == moduleName {
51-
// This is a page representing the module itself.
52-
// Create a dummy symbol graph and tell SwiftDocC to convert the module name.
53-
let emptySymbolGraph = String(
54-
data: try JSONEncoder().encode(
55-
SymbolGraph(
56-
metadata: SymbolGraph.Metadata(
57-
formatVersion: SymbolGraph.SemanticVersion(major: 0, minor: 5, patch: 0),
58-
generator: "SourceKit-LSP"
59-
),
60-
module: SymbolGraph.Module(name: moduleName, platform: SymbolGraph.Platform()),
61-
symbols: [],
62-
relationships: []
63-
)
64-
),
65-
encoding: .utf8
66-
)
67-
return try await documentationManager.renderDocCDocumentation(
68-
symbolUSR: moduleName,
69-
symbolGraph: emptySymbolGraph,
70-
markupFile: snapshot.text,
71-
moduleName: moduleName,
72-
catalogURL: catalogURL
73-
)
74-
}
49+
guard case .symbol(let symbolName) = MarkdownTitleFinder.find(parsing: snapshot.text) else {
50+
// This is an article that can be rendered on its own
51+
return try await documentationManager.renderDocCDocumentation(
52+
markupFile: snapshot.text,
53+
moduleName: moduleName,
54+
catalogURL: catalogURL
55+
)
56+
}
57+
guard let moduleName, symbolName == moduleName else {
7558
// This is a symbol extension page. Find the symbol so that we can include it in the request.
7659
guard let index = workspace.index(checkedFor: .deletedFiles) else {
7760
throw ResponseError.requestFailed(doccDocumentationError: .indexNotAvailable)
@@ -115,8 +98,25 @@ extension DocumentationLanguageService {
11598
catalogURL: catalogURL
11699
)
117100
}
118-
// This is an article that can be rendered on its own
101+
// This is a page representing the module itself.
102+
// Create a dummy symbol graph and tell SwiftDocC to convert the module name.
103+
let emptySymbolGraph = String(
104+
data: try JSONEncoder().encode(
105+
SymbolGraph(
106+
metadata: SymbolGraph.Metadata(
107+
formatVersion: SymbolGraph.SemanticVersion(major: 0, minor: 5, patch: 0),
108+
generator: "SourceKit-LSP"
109+
),
110+
module: SymbolGraph.Module(name: moduleName, platform: SymbolGraph.Platform()),
111+
symbols: [],
112+
relationships: []
113+
)
114+
),
115+
encoding: .utf8
116+
)
119117
return try await documentationManager.renderDocCDocumentation(
118+
symbolUSR: moduleName,
119+
symbolGraph: emptySymbolGraph,
120120
markupFile: snapshot.text,
121121
moduleName: moduleName,
122122
catalogURL: catalogURL
@@ -128,20 +128,18 @@ extension DocumentationLanguageService {
128128
}
129129

130130
struct MarkdownTitleFinder: MarkupVisitor {
131-
public typealias Result = Title?
132-
133131
public enum Title {
134132
case plainText(String)
135133
case symbol(String)
136134
}
137135

138-
public static func find(parsing text: String) -> Result {
136+
public static func find(parsing text: String) -> Title? {
139137
let document = Markdown.Document(parsing: text, options: [.parseSymbolLinks])
140138
var visitor = MarkdownTitleFinder()
141139
return visitor.visit(document)
142140
}
143141

144-
public mutating func defaultVisit(_ markup: any Markup) -> Result {
142+
public mutating func defaultVisit(_ markup: any Markup) -> Title? {
145143
for child in markup.children {
146144
if let value = visit(child) {
147145
return value
@@ -150,7 +148,7 @@ struct MarkdownTitleFinder: MarkupVisitor {
150148
return nil
151149
}
152150

153-
public mutating func visitHeading(_ heading: Heading) -> Result {
151+
public mutating func visitHeading(_ heading: Heading) -> Title? {
154152
guard heading.level == 1 else {
155153
return nil
156154
}

0 commit comments

Comments
 (0)