-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a "references" relationship kind and referenceLocation mixin (#59)
rdar://108470705
- Loading branch information
1 parent
53e5cb9
commit 1d5aba8
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Sources/SymbolKit/SymbolGraph/Relationship/ReferenceLocation.swift
This file contains 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,39 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
Copyright (c) 2023 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 | ||
|
||
extension SymbolGraph.Relationship { | ||
/// A mixin for `references` relationships that indicates the source location of the reference. | ||
public struct ReferenceLocation: Mixin, Codable, Equatable { | ||
public static var mixinKey = "referenceLocation" | ||
|
||
/// The source locations where the reference occurs. | ||
public var range: SymbolGraph.LineList.SourceRange | ||
|
||
/// The URI of the source file where the reference occurs. | ||
public var uri: String | ||
|
||
/// The file URL of the source file where the reference occurs. | ||
@available(macOS 10.11, *) | ||
public var url: URL? { | ||
// The URI string provided in the symbol graph file may be an invalid URL (rdar://69242070) | ||
// | ||
// Using `URL.init(dataRepresentation:relativeTo:)` here handles URI strings with unescaped | ||
// characters without trying to escape or otherwise process the URI string in SymbolKit. | ||
return URL(dataRepresentation: Data(uri.utf8), relativeTo: nil) | ||
} | ||
|
||
public init(range: SymbolGraph.LineList.SourceRange, uri: String) { | ||
self.range = range | ||
self.uri = uri | ||
} | ||
} | ||
} |
This file contains 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 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
36 changes: 36 additions & 0 deletions
36
Tests/SymbolKitTests/SymbolGraph/Relationship/ReferenceLocationTests.swift
This file contains 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,36 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
Copyright (c) 2023 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 XCTest | ||
@testable import SymbolKit | ||
|
||
class ReferenceLocationTests: XCTestCase { | ||
typealias ReferenceLocation = SymbolGraph.Relationship.ReferenceLocation | ||
|
||
func testRoundTrip() throws { | ||
let referenceLocation = ReferenceLocation( | ||
range: .init( | ||
start: .init(line: 14, character: 0), | ||
end: .init(line: 14, character: 6)), | ||
uri: "file://file.swift" | ||
) | ||
var source = SymbolGraph.Relationship(source: "source", target: "target", kind: .references, targetFallback: nil) | ||
source[mixin: ReferenceLocation.self] = referenceLocation | ||
|
||
let encoder = JSONEncoder() | ||
let decoder = JSONDecoder() | ||
|
||
let encodedRelationship = try encoder.encode(source) | ||
let decoded = try decoder.decode(SymbolGraph.Relationship.self, from: encodedRelationship) | ||
|
||
XCTAssertEqual(source, decoded) | ||
XCTAssertEqual(decoded[mixin: ReferenceLocation.self], referenceLocation) | ||
} | ||
} |