-
Notifications
You must be signed in to change notification settings - Fork 166
Emit symbol URL to link to declaration in repo #256
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
Merged
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| This source file is part of the Swift.org open source project | ||
|
|
||
| Copyright (c) 2022 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 Swift project authors | ||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| /// A remote repository that hosts source code. | ||
| public struct SourceRepository { | ||
| /// The path at which the repository is cloned locally. | ||
| public var checkoutPath: String | ||
|
|
||
| /// The base URL where the service hosts the repository's contents. | ||
| public var sourceServiceBaseURL: URL | ||
|
|
||
| /// A function that formats a line number to be included in a URL. | ||
| public var formatLineNumber: (Int) -> String | ||
|
|
||
| /// Creates a source code repository. | ||
| /// - Parameters: | ||
| /// - checkoutPath: The path at which the repository is checked out locally and from which its symbol graphs were generated. | ||
| /// - sourceServiceBaseURL: The base URL where the service hosts the repository's contents. | ||
| /// - formatLineNumber: A function that formats a line number to be included in a URL. | ||
| public init( | ||
| checkoutPath: String, | ||
| sourceServiceBaseURL: URL, | ||
| formatLineNumber: @escaping (Int) -> String | ||
| ) { | ||
| self.checkoutPath = checkoutPath | ||
| self.sourceServiceBaseURL = sourceServiceBaseURL | ||
| self.formatLineNumber = formatLineNumber | ||
| } | ||
|
|
||
| /// Formats a local source file URL to a URL hosted by the remote source code service. | ||
| /// - Parameters: | ||
| /// - sourceFileURL: The location of the source file on disk. | ||
| /// - lineNumber: A line number in the source file, 1-indexed. | ||
| /// - Returns: The URL of the file hosted by the remote source code service if it could be constructed, otherwise, `nil`. | ||
| public func format(sourceFileURL: URL, lineNumber: Int? = nil) -> URL? { | ||
| guard sourceFileURL.path.hasPrefix(checkoutPath) else { | ||
| return nil | ||
| } | ||
|
|
||
| let path = sourceFileURL.path.dropFirst(checkoutPath.count).removingLeadingSlash | ||
| return sourceServiceBaseURL | ||
| .appendingPathComponent(path) | ||
| .withFragment(lineNumber.map(formatLineNumber)) | ||
| } | ||
| } | ||
|
|
||
| public extension SourceRepository { | ||
| /// Creates a source repository hosted by the GitHub service. | ||
| /// - Parameters: | ||
| /// - checkoutPath: The path of the local checkout. | ||
| /// - sourceServiceBaseURL: The base URL where the service hosts the repository's contents. | ||
| static func github(checkoutPath: String, sourceServiceBaseURL: URL) -> SourceRepository { | ||
| SourceRepository( | ||
| checkoutPath: checkoutPath, | ||
| sourceServiceBaseURL: sourceServiceBaseURL, | ||
| formatLineNumber: { line in "L\(line)" } | ||
| ) | ||
| } | ||
|
|
||
| /// Creates a source repository hosted by the GitLab service. | ||
| /// - Parameters: | ||
| /// - checkoutPath: The path of the local checkout. | ||
| /// - sourceServiceBaseURL: The base URL where the service hosts the repository's contents. | ||
| static func gitlab(checkoutPath: String, sourceServiceBaseURL: URL) -> SourceRepository { | ||
| SourceRepository( | ||
| checkoutPath: checkoutPath, | ||
| sourceServiceBaseURL: sourceServiceBaseURL, | ||
| formatLineNumber: { line in "L\(line)" } | ||
| ) | ||
| } | ||
|
|
||
| /// Creates a source repository hosted by the BitBucket service. | ||
| /// - Parameters: | ||
| /// - checkoutPath: The path of the local checkout. | ||
| /// - sourceServiceBaseURL: The base URL where the service hosts the repository's contents. | ||
| static func bitbucket(checkoutPath: String, sourceServiceBaseURL: URL) -> SourceRepository { | ||
| SourceRepository( | ||
| checkoutPath: checkoutPath, | ||
| sourceServiceBaseURL: sourceServiceBaseURL, | ||
| formatLineNumber: { line in "lines-\(line)" } | ||
| ) | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like
lines-orline-will work here butline-seems a little nicer to me since we'll never be linking to a block.There was a problem hiding this comment.
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 uselines-.There was a problem hiding this comment.
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.