-
Notifications
You must be signed in to change notification settings - Fork 328
Separate signature and parameter documentation using swift-markdown #2292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
a7medev
wants to merge
7
commits into
swiftlang:main
Choose a base branch
from
a7medev:feat/signature-help-separate-param-doc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ac09ca
Separate signature and parameter documentation using swift-markdown
a7medev beb9e0c
Correctly add swift-markdown in cmake configuration
a7medev 7a1d592
Refine ParametersDocumentationExtractor to avoid mutations
a7medev 63e9704
Test parameter extraction edge cases and match swift-docc
a7medev 83e1522
Extract parameter documentation with raw identifiers
a7medev 668da58
Make ParametersDocumentationExtractor members top-level & remove firs…
a7medev 992bedc
Attempt to fix Windows build
a7medev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
Sources/SwiftLanguageService/ParametersDocumentationExtractor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import Markdown | ||
|
||
/// Extracts parameter documentation from a markdown string. | ||
/// | ||
/// The parameter extraction implementation is almost ported from the implementation in the Swift compiler codebase. | ||
/// | ||
/// The problem with doing that in the Swift compiler codebase is that once you parse a the comment as markdown into | ||
/// a `Document` you cannot easily convert it back into markdown (we'd need to write our own markdown formatter). | ||
/// Besides, `cmark` doesn't handle Doxygen commands. | ||
/// | ||
/// We considered using `swift-docc` but we faced some problems with it: | ||
/// | ||
/// 1. We would need to refactor existing use of `swift-docc` in SourceKit-LSP to reuse some of that logic here besides | ||
/// providing the required arguments. | ||
/// 2. The result returned by DocC can't be directly converted to markdown, we'd need to provide our own DocC markdown renderer. | ||
/// | ||
/// Implementing this using `swift-markdown` allows us to easily parse the comment, process it, convert it back to markdown. | ||
/// It also provides minimal parsing for Doxygen commands (we're only interested in `\param`) allowing us to use the same | ||
/// implementation for Clang-based declarations. | ||
/// | ||
/// Although this approach involves code duplication, it's simple enough for the initial implementation. We should consider | ||
/// `swift-docc` in the future. | ||
private struct ParametersDocumentationExtractor { | ||
ahoppen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ahoppen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
struct Parameter { | ||
let name: String | ||
let documentation: String | ||
} | ||
|
||
/// Extracts parameter documentation from a markdown string. | ||
/// | ||
/// - Returns: A tuple containing the extracted parameters and the remaining markdown. | ||
func extract(from markdown: String) -> (parameters: [String: String], remaining: String) { | ||
let document = Document(parsing: markdown, options: [.parseBlockDirectives, .parseMinimalDoxygen]) | ||
|
||
var parameters: [String: String] = [:] | ||
var remainingBlocks: [any BlockMarkup] = [] | ||
|
||
for block in document.blockChildren { | ||
switch block { | ||
case let unorderedList as UnorderedList: | ||
let (newUnorderedList, params) = extract(from: unorderedList) | ||
if let newUnorderedList { | ||
remainingBlocks.append(newUnorderedList) | ||
} | ||
|
||
for param in params { | ||
// If duplicate parameter documentation is found, keep the first one following swift-docc's behavior | ||
parameters[param.name] = parameters[param.name] ?? param.documentation | ||
} | ||
|
||
case let doxygenParameter as DoxygenParameter: | ||
let param = extract(from: doxygenParameter) | ||
// If duplicate parameter documentation is found, keep the first one following swift-docc's behavior | ||
parameters[param.name] = parameters[param.name] ?? param.documentation | ||
|
||
default: | ||
remainingBlocks.append(block) | ||
} | ||
} | ||
|
||
let remaining = Document(remainingBlocks).format() | ||
|
||
return (parameters, remaining) | ||
} | ||
|
||
/// Extracts parameter documentation from a Doxygen parameter command. | ||
private func extract(from doxygenParameter: DoxygenParameter) -> Parameter { | ||
return Parameter( | ||
name: doxygenParameter.name, | ||
documentation: Document(doxygenParameter.blockChildren).format(), | ||
) | ||
} | ||
|
||
/// Extracts parameter documentation from an unordered list. | ||
/// | ||
/// - Returns: A new UnorderedList with the items that were not added to the parameters if any. | ||
private func extract(from unorderedList: UnorderedList) -> (remaining: UnorderedList?, parameters: [Parameter]) { | ||
var parameters: [Parameter] = [] | ||
var newItems: [ListItem] = [] | ||
|
||
for item in unorderedList.listItems { | ||
if let param = extractSingle(from: item) { | ||
parameters.append(param) | ||
} else if let params = extractOutline(from: item) { | ||
parameters.append(contentsOf: params) | ||
} else { | ||
newItems.append(item) | ||
} | ||
} | ||
|
||
if newItems.isEmpty { | ||
return (remaining: nil, parameters: parameters) | ||
} | ||
|
||
return (remaining: UnorderedList(newItems), parameters: parameters) | ||
} | ||
|
||
/// Parameter documentation from a `Parameters:` outline. | ||
/// | ||
/// Example: | ||
/// ```markdown | ||
/// - Parameters: | ||
/// - param: description | ||
/// ``` | ||
/// | ||
/// - Returns: True if the list item has parameter outline documentation, false otherwise. | ||
private func extractOutline(from listItem: ListItem) -> [Parameter]? { | ||
guard let firstChild = listItem.child(at: 0) as? Paragraph, | ||
ahoppen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let headingText = firstChild.child(at: 0) as? Text | ||
else { | ||
return nil | ||
} | ||
|
||
guard headingText.string.trimmingCharacters(in: .whitespaces).lowercased().hasPrefix("parameters:") else { | ||
return nil | ||
} | ||
|
||
return listItem.children.flatMap { child in | ||
guard let nestedList = child as? UnorderedList else { | ||
return [] as [Parameter] | ||
} | ||
|
||
return nestedList.listItems.compactMap(extractOutlineItem) | ||
} | ||
} | ||
|
||
/// Extracts parameter documentation from a single parameter. | ||
/// | ||
/// Example: | ||
/// ```markdown | ||
/// - Parameter param: description | ||
/// ``` | ||
/// | ||
/// - Returns: True if the list item has single parameter documentation, false otherwise. | ||
private func extractSingle(from listItem: ListItem) -> Parameter? { | ||
guard let paragraph = listItem.child(at: 0) as? Paragraph, | ||
let paragraphText = paragraph.child(at: 0) as? Text | ||
else { | ||
return nil | ||
} | ||
|
||
let parameterPrefix = "parameter " | ||
let paragraphContent = paragraphText.string | ||
|
||
guard paragraphContent.count >= parameterPrefix.count else { | ||
return nil | ||
} | ||
|
||
let prefixEnd = paragraphContent.index(paragraphContent.startIndex, offsetBy: parameterPrefix.count) | ||
ahoppen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let potentialMatch = paragraphContent[..<prefixEnd].lowercased() | ||
|
||
guard potentialMatch == parameterPrefix else { | ||
return nil | ||
} | ||
|
||
let remainingContent = String(paragraphContent[prefixEnd...]).trimmingCharacters(in: .whitespaces) | ||
|
||
return extractParam(firstTextContent: remainingContent, listItem: listItem, single: true) | ||
} | ||
|
||
/// Extracts a parameter field from a list item (used for parameter outline items) | ||
private func extractOutlineItem(from listItem: ListItem) -> Parameter? { | ||
guard let paragraph = listItem.child(at: 0) as? Paragraph else { | ||
return nil | ||
} | ||
|
||
let firstText = paragraph.child(at: 0) as? Text | ||
|
||
return extractParam(firstTextContent: firstText?.string ?? "", listItem: listItem, single: false) | ||
} | ||
|
||
/// Extracts a parameter field from a list item provided the relevant first text content allowing reuse in ``extractOutlineItem`` and ``extractSingle`` | ||
/// | ||
/// - Parameters: | ||
/// - firstTextContent: The content of the first text child of the list item's first paragraph | ||
/// - listItem: The list item to extract the parameter from | ||
/// - single: Whether the parameter is a single parameter or part of a parameter outline | ||
/// | ||
/// - Returns: A tuple containing the parameter name and documentation if a parameter was found, nil otherwise. | ||
private func extractParam( | ||
a7medev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
firstTextContent: String, | ||
listItem: ListItem, | ||
single: Bool | ||
) -> Parameter? { | ||
guard let paragraph = listItem.child(at: 0) as? Paragraph else { | ||
return nil | ||
} | ||
|
||
let components = firstTextContent.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: false) | ||
|
||
guard components.count == 2 else { | ||
return extractWithRawIdentifier(from: listItem, single: single) | ||
} | ||
|
||
let name = String(components[0]).trimmingCharacters(in: .whitespaces) | ||
guard !name.isEmpty else { | ||
return nil | ||
} | ||
|
||
|
||
let remainingFirstTextContent = String(components[1]).trimmingCharacters(in: .whitespaces) | ||
let remainingParagraphChildren = [Text(remainingFirstTextContent)] + paragraph.inlineChildren.dropFirst() | ||
let remainingChildren = [Paragraph(remainingParagraphChildren)] + listItem.blockChildren.dropFirst() | ||
let documentation = Document(remainingChildren).format() | ||
|
||
return Parameter(name: name, documentation: documentation) | ||
} | ||
|
||
/// Extracts a parameter with its name as a raw identifier. | ||
/// | ||
/// Example: | ||
/// ```markdown | ||
/// - Parameter `foo bar`: documentation | ||
/// - Parameters: | ||
/// - `foo bar`: documentation | ||
/// ``` | ||
/// | ||
/// - Parameters: | ||
/// - listItem: The list item to extract the parameter from | ||
/// - single: Whether the parameter is a single parameter or part of a parameter outline | ||
func extractWithRawIdentifier(from listItem: ListItem, single: Bool) -> Parameter? { | ||
/// The index of ``InlineCode`` for the raw identifier parameter name in the first paragraph of ``listItem`` | ||
let inlineCodeIndex = single ? 1 : 0 | ||
|
||
guard let paragraph = listItem.child(at: 0) as? Paragraph, | ||
let rawIdentifier = paragraph.child(at: inlineCodeIndex) as? InlineCode, | ||
let text = paragraph.child(at: inlineCodeIndex + 1) as? Text | ||
else { | ||
return nil | ||
} | ||
|
||
let textContent = text.string.trimmingCharacters(in: .whitespaces) | ||
|
||
guard textContent.hasPrefix(":") else { | ||
return nil | ||
} | ||
|
||
let remainingTextContent = String(textContent.dropFirst()).trimmingCharacters(in: .whitespaces) | ||
let remainingParagraphChildren = | ||
[Text(remainingTextContent)] + paragraph.inlineChildren.dropFirst(inlineCodeIndex + 2) | ||
let remainingChildren = [Paragraph(remainingParagraphChildren)] + listItem.blockChildren.dropFirst(1) | ||
let documentation = Document(remainingChildren).format() | ||
|
||
return Parameter(name: rawIdentifier.code, documentation: documentation) | ||
} | ||
} | ||
|
||
/// Extracts parameter documentation from markdown text. | ||
/// | ||
/// - Parameter markdown: The markdown text to extract parameters from | ||
/// - Returns: A tuple containing the extracted parameters dictionary and the remaining markdown text | ||
package func extractParametersDocumentation(from markdown: String) -> ([String: String], String) { | ||
let extractor = ParametersDocumentationExtractor() | ||
return extractor.extract(from: markdown) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.