Skip to content

Conversation

@franklinsch
Copy link
Contributor

@franklinsch franklinsch commented May 15, 2022

Bug/issue #, if applicable: #186, rdar://88537303

Summary

Adds command-line arguments to configure a repo URL so that documentation websites can display a link to the symbol's declaration in the repo.

Example invocation:

swift package generate-documentation \
  --source-service github \
  --source-service-base-url https://github.com/<org>/<repo>/blob/main \
  --checkout-path <path to local checkout>

Dependencies

swiftlang/swift-docc-render#366

Testing

Set the DOCC_EXEC environment variable to a local build of DocC from this branch and run with the flags described above. Verify that documentation pages for Swift symbols contains a .metadata.source field that links to the symbol's declaration in the git service.

Checklist

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

TODO

  • Add documentation
  • Add tests

@franklinsch franklinsch marked this pull request as draft May 15, 2022 11:36
@Kyle-Ye Kyle-Ye linked an issue Jun 5, 2022 that may be closed by this pull request
@franklinsch franklinsch force-pushed the emit-source-url branch 3 times, most recently from c8d7958 to 250f73c Compare June 12, 2022 09:22
@franklinsch franklinsch force-pushed the emit-source-url branch 2 times, most recently from 3d304d2 to b7121f5 Compare July 1, 2022 12:40
@franklinsch franklinsch marked this pull request as ready for review July 1, 2022 12:41
@franklinsch
Copy link
Contributor Author

@swift-ci please test

@franklinsch
Copy link
Contributor Author

@swift-ci please test

@franklinsch franklinsch requested a review from ethan-kusters July 1, 2022 12:57
@franklinsch franklinsch force-pushed the emit-source-url branch 2 times, most recently from 21c5978 to 2c432e3 Compare July 1, 2022 13:06
@franklinsch
Copy link
Contributor Author

@swift-ci please test

@franklinsch
Copy link
Contributor Author

@swift-ci please test

@franklinsch
Copy link
Contributor Author

@ethan-kusters Did you have a chance to take a look at this? The DocC Render PR is moving forward swiftlang/swift-docc-render#366 (review)

/// Whether the documentation converter should include access level information for symbols.
let shouldEmitSymbolAccessLevels: Bool

/// The source repository where the documentation's sources are hosted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The source repository where the documentation's sources are hosted.
/// The online source control repository where the documentation's source is hosted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I clarified this.

/// The variants for the source file URI of a page.
public var sourceFileURIVariants: VariantCollection<String?> = .init(defaultValue: nil)

/// The source location of the topic's source code.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The source location of the topic's source code.
/// The source location of the topic's declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed this property and updated the documentation comment:

/// The remote location where the source declaration of the topic can be viewed.
public var remoteSource: RemoteSource? {

}

/// Describes the location of the topic's source code, hosted by a source service.
public struct Source: Codable, Equatable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be Location or Declaration? We use Location in SymbolKit it looks like: https://github.com/apple/swift-docc-symbolkit/blob/main/openapi.yaml#L290.

I think Source is a good term for describing the entire collection of a project's source code but I'm not sure it's right for describing a specific symbol's declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards being ok with "source" either referring to the source repository of the code in the 'semantic analysis' part of DocC and it referring to a specific symbol's remote source file when rendering each topic individually. I've renamed this to remoteSource to avoid confusion with the source of the symbol in a local file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fyi @dobromir-hristov, if we go with this name change, we'll need to update the DocC Render PR to update the property name in the metadata object)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, just remind me in the PR with the final name :)


/// Describes the location of the topic's source code, hosted by a source service.
public struct Source: Codable, Equatable {
/// The file name of the topic's source code.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The file name of the topic's source code.
/// The name of the file where the topic's declared.


/// Creates a source code repository.
/// - Parameters:
/// - checkoutPath: The path at which the repository is checked out locally.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be more specific here? This needs to be the location where the repository was checked out when the given symbol graph was produced, right? Not just any local checkout.

@ethan-kusters
Copy link
Contributor

Looks great overall! Really nicely structured. Just left a few nit-picky comments.

What are your thoughts on Info.plist support? I think it makes sense to land this initially without it (especially with the complications of needing to specify the current local checkout path and the possibility of local source getting out-of-sync with remote source), but I'm wondering if you have thoughts on if we should support this in the future.

It would also be nice if when running docc preview I got links to my local source for free... I guess it's unlikely that there's any kind of OS support for opening a file to a specific line number in the current default editor though.

public var sourceFileURIVariants: VariantCollection<String?> = .init(defaultValue: nil)

/// The source location of the topic's source code.
public var source: Source? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment below wondering if we should call this source, but regardless of what general name we land on here – should we specify that this is a remote location? That way it's clear how this is different from the existing sourceFileURI.

Suggested change
public var source: Source? {
public var remoteSource: Source? {
Suggested change
public var source: Source? {
public var remoteLocation: Location? {
Suggested change
public var source: Source? {
public var remoteSourceLocation: SourceLocation? {

@dobromir-hristov
Copy link
Contributor

docc-render PR is approved. So let me know when you guys are ready and want to merge it :)

@franklinsch
Copy link
Contributor Author

What are your thoughts on Info.plist support? I think it makes sense to land this initially without it (especially with the complications of needing to specify the current local checkout path and the possibility of local source getting out-of-sync with remote source), but I'm wondering if you have thoughts on if we should support this in the future.

I don't think Info.plist is the right place to specify this data, especially the local checkout path as you mentioned. Instead, I see build systems/IDEs integrate with this in a way that provides sensible values to these arguments, if the user opts into this.

It would also be nice if when running docc preview I got links to my local source for free... I guess it's unlikely that there's any kind of OS support for opening a file to a specific line number in the current default editor though.

This would be nice as well, but opt-in as well I'd say. If your goal with docc preview is to preview a 1:1 mapping of what will be hosted on your documentation website, you may not want to see source links that wouldn't appear in your website.

@franklinsch franklinsch force-pushed the emit-source-url branch 2 times, most recently from 4349e0c to cdf218f Compare August 4, 2022 13:32
@franklinsch
Copy link
Contributor Author

Based on feedback above, I've gone ahead and made --checkout-path non-optional, i.e., a value needs to be provided by the user. This is because other DocC behavior assumes your current working directory is a DocC catalog (so that you can simply run docc preview from within a catalog), and DocC catalogs are not usually the root of git repositories.

However, it would be nice if the DocC SwiftPM plugin would automatically fill in --checkout-path to the root path of the local checkout, since SwiftPM already has smarts about git repositories. I've filed swiftlang/swift-docc-plugin#24 to track this enhancement.

@franklinsch
Copy link
Contributor Author

@swift-ci please test

@franklinsch
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@ethan-kusters ethan-kusters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great- thank you!! Only blocking feedback is the OpenAPI spec validation. Very excited to have this in. 🥳

SourceRepository(
checkoutPath: checkoutPath,
sourceServiceBaseURL: sourceServiceBaseURL,
formatLineNumber: { line in "lines-\(line)" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like lines- or line- will work here but line- seems a little nicer to me since we'll never be linking to a block.

Suggested change
formatLineNumber: { line in "lines-\(line)" }
formatLineNumber: { line in "line-\(line)" }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find an official format reference from BitBucket, but lines- is what their website uses when you click on a file number, e.g., https://bitbucket.org/kannemadugupriyanka/sb1-test-fork-test-test/src/8f1c1b45b4622388b241cf281b595b4e02cef258/Test%20Test/.project#lines-4, so given that it seems safer to use lines-.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I did come across this from BitBucket as a spec for what it's worth: https://support.atlassian.com/bitbucket-cloud/docs/hyperlink-to-source-code-in-bitbucket/.

But it lists something entirely different from what the UI does: #<filename>-<linenumber>.

https://bitbucket.org/kannemadugupriyanka/sb1-test-fork-test-test/src/8f1c1b45b4622388b241cf281b595b4e02cef258/Test%20Test/.project#.project-4

And then the <filename> part is technically optional actually so:

https://bitbucket.org/kannemadugupriyanka/sb1-test-fork-test-test/src/8f1c1b45b4622388b241cf281b595b4e02cef258/Test%20Test/.project#-4

works as well.

Any way 😃 Just matching the behavior of the UI seems fine enough.

"""
)
)
public var sourceService: String?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nit-pick but ArgParser I think actually has built-in support for this kind of thing: https://apple.github.io/swift-argument-parser/documentation/argumentparser/declaringarguments/#Parsing-custom-types.

enum ReleaseMode: String, ExpressibleByArgument {
    case debug, release
}

struct Example: ParsableCommand {
    @Option var mode: ReleaseMode

    mutating func run() throws {
        print(mode)
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that initially, but I wanted to allow any casing for these values, e.g., both--source-service GitHub and --source-service github, so I implemented the validation logic manually.

@franklinsch
Copy link
Contributor Author

@swift-ci please test

@franklinsch franklinsch merged commit 082f8b0 into swiftlang:main Aug 8, 2022
@franklinsch franklinsch deleted the emit-source-url branch August 8, 2022 15:06
franklinsch added a commit to franklinsch/swift-docc that referenced this pull request Aug 12, 2022
Added documentation to configure DocC's new functionality that emits
links to symbols' declaration in the project's source repository, which
was implemented in swiftlang#256.
franklinsch added a commit to franklinsch/swift-docc that referenced this pull request Aug 12, 2022
Added documentation to configure DocC's new functionality that emits
links to symbols' declaration in the project's source repository, which
was implemented in swiftlang#256.
franklinsch added a commit to franklinsch/swift-docc that referenced this pull request Aug 26, 2022
Added documentation to configure DocC's new functionality that emits
links to symbols' declaration in the project's source repository, which
was implemented in swiftlang#256.
franklinsch added a commit that referenced this pull request Aug 26, 2022
Added documentation to configure DocC's new functionality that emits
links to symbols' declaration in the project's source repository, which
was implemented in #256.
@Kyle-Ye Kyle-Ye mentioned this pull request Dec 28, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SR-15828] DocC should have an option to add links from docs to source

4 participants