Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit b0e3615

Browse files
authored
Add sourceRange property to Symbol (#237)
* Set source range for symbols * Adopt Equatable, Comparable, and Hashable in extension on SourceRange * Refactor with sourceRange(using:) helper method * Deprecate sourceLocation Replace existing use of sourceLocation in DCOV extension * Add changelog entries for #237
1 parent e4f88b5 commit b0e3615

File tree

6 files changed

+61
-20
lines changed

6 files changed

+61
-20
lines changed

Changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
#219 by @Lukas-Stuehrk.
1616
- Added support for documenting default implementations.
1717
#221 by @Lukas-Stuehrk.
18+
- Added `sourceRange` property to `Symbol`.
19+
#237 by @mattt.
1820

1921
### Fixed
2022

@@ -27,6 +29,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2729

2830
- Changed display of code declarations in HTML.
2931
#204 by @mattt.
32+
- Changed serialization of `Symbol` to encode and decode `sourceRange` key
33+
instead of `sourceLocation` key.
34+
#237 by @mattt.
35+
36+
### Deprecated
37+
38+
- Deprecated `Symbol.sourceLocation` property.
39+
Use `Symbol.sourceRange.start` instead.
40+
#237 by @mattt.
3041
- Changed the `generate` command to skip hidden files
3142
and top-level `Tests` directories.
3243
#229 by @mattt.

Sources/SwiftDoc/Extensions/SwiftSyntax+Extensions.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,37 @@ extension SourceLocation: Hashable {
2121

2222
// MARK: -
2323

24+
extension SourceRange: Equatable {
25+
public static func == (lhs: SourceRange, rhs: SourceRange) -> Bool {
26+
return lhs.start == rhs.start && lhs.end == rhs.end
27+
}
28+
}
29+
30+
extension SourceRange: Comparable {
31+
public static func < (lhs: SourceRange, rhs: SourceRange) -> Bool {
32+
return lhs.start < rhs.start
33+
}
34+
}
35+
36+
extension SourceRange: Hashable {
37+
public func hash(into hasher: inout Hasher) {
38+
hasher.combine(start)
39+
hasher.combine(end)
40+
}
41+
}
42+
43+
// MARK: -
44+
2445
protocol SymbolDeclProtocol: SyntaxProtocol {
2546
var declaration: Syntax { get }
2647
}
2748

49+
extension SymbolDeclProtocol {
50+
func sourceRange(using converter: SourceLocationConverter) -> SourceRange {
51+
return SourceRange(start: startLocation(converter: converter), end: endLocation(converter: converter))
52+
}
53+
}
54+
2855
extension AssociatedtypeDeclSyntax: SymbolDeclProtocol {}
2956
extension ClassDeclSyntax: SymbolDeclProtocol {}
3057
extension EnumDeclSyntax: SymbolDeclProtocol {}

Sources/SwiftDoc/Interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public final class Interface {
6868
for name in inheritedTypeNames {
6969
let inheritedTypes = symbols.filter({ ($0.api is Class || $0.api is Protocol) && $0.id.description == name })
7070
if inheritedTypes.isEmpty {
71-
let inherited = Symbol(api: Unknown(name: name), context: [], declaration: [], documentation: nil, sourceLocation: nil)
71+
let inherited = Symbol(api: Unknown(name: name), context: [], declaration: [], documentation: nil, sourceRange: nil)
7272
relationships.insert(Relationship(subject: symbol, predicate: .conformsTo, object: inherited))
7373
} else {
7474
for inherited in inheritedTypes {

Sources/SwiftDoc/SourceFile.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public struct SourceFile: Hashable, Codable {
5656
func symbol<Node, Declaration>(_ type: Declaration.Type, _ node: Node) -> Symbol? where Declaration: API & ExpressibleBySyntax, Node == Declaration.Syntax, Node: SymbolDeclProtocol {
5757
guard let api = Declaration(node) else { return nil }
5858
guard let documentation = try? Documentation.parse(node.documentation) else { return nil }
59-
let sourceLocation = sourceLocationConverter.location(for: node.position)
60-
return Symbol(api: api, context: context, declaration: declaration(for: node), documentation: documentation, sourceLocation: sourceLocation)
59+
let sourceRange = node.sourceRange(using: sourceLocationConverter)
60+
return Symbol(api: api, context: context, declaration: declaration(for: node), documentation: documentation, sourceRange: sourceRange)
6161
}
6262

6363
func symbol<Node: SymbolDeclProtocol>(_ node: Node, api: API) -> Symbol? {
6464
guard let documentation = try? Documentation.parse(node.documentation) else { return nil }
65-
let sourceLocation = sourceLocationConverter.location(for: node.position)
66-
return Symbol(api: api, context: context, declaration: declaration(for: node), documentation: documentation, sourceLocation: sourceLocation)
65+
let sourceRange = node.sourceRange(using: sourceLocationConverter)
66+
return Symbol(api: api, context: context, declaration: declaration(for: node), documentation: documentation, sourceRange: sourceRange)
6767
}
6868

6969
func declaration<Node: SymbolDeclProtocol>(for node: Node) -> [Token] {

Sources/SwiftDoc/Symbol.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ public final class Symbol {
1010
public let context: [Contextual]
1111
public let declaration: [Token]
1212
public let documentation: Documentation?
13-
public let sourceLocation: SourceLocation?
13+
public let sourceRange: SourceRange?
14+
15+
@available(swift, deprecated: 0.0.1, message: "Use sourceRange instead")
16+
public var sourceLocation: SourceLocation? { sourceRange?.start }
1417

1518
public private(set) lazy var `extension`: Extension? = context.compactMap { $0 as? Extension }.first
1619
public private(set) lazy var conditions: [CompilationCondition] = context.compactMap { $0 as? CompilationCondition }
1720

18-
init(api: API, context: [Contextual], declaration: [Token], documentation: Documentation?, sourceLocation: SourceLocation?) {
21+
init(api: API, context: [Contextual], declaration: [Token], documentation: Documentation?, sourceRange: SourceRange?) {
1922
self.api = api
2023
self.context = context
2124
self.declaration = declaration
2225
self.documentation = documentation
23-
self.sourceLocation = sourceLocation
26+
self.sourceRange = sourceRange
2427
}
2528

2629
public var name: String {
@@ -114,8 +117,8 @@ public final class Symbol {
114117
extension Symbol: Equatable {
115118
public static func == (lhs: Symbol, rhs: Symbol) -> Bool {
116119
guard lhs.documentation == rhs.documentation,
117-
lhs.sourceLocation == rhs.sourceLocation
118-
else { return false }
120+
lhs.sourceRange == rhs.sourceRange
121+
else { return false }
119122

120123
guard lhs.context.count == rhs.context.count else { return false}
121124
for (lc, rc) in zip(lhs.context, rhs.context) {
@@ -170,8 +173,8 @@ extension Symbol: Equatable {
170173

171174
extension Symbol: Comparable {
172175
public static func < (lhs: Symbol, rhs: Symbol) -> Bool {
173-
if let lsl = lhs.sourceLocation, let rsl = rhs.sourceLocation {
174-
return lsl < rsl
176+
if let lsr = lhs.sourceRange, let rsr = rhs.sourceRange {
177+
return lsr < rsr
175178
} else {
176179
return lhs.name < rhs.name
177180
}
@@ -183,7 +186,7 @@ extension Symbol: Comparable {
183186
extension Symbol: Hashable {
184187
public func hash(into hasher: inout Hasher) {
185188
hasher.combine(documentation)
186-
hasher.combine(sourceLocation)
189+
hasher.combine(sourceRange)
187190
switch api {
188191
case let api as AssociatedType:
189192
hasher.combine(api)
@@ -225,7 +228,7 @@ extension Symbol: Codable {
225228
private enum CodingKeys: String, CodingKey {
226229
case declaration
227230
case documentation
228-
case sourceLocation
231+
case sourceRange
229232

230233
case associatedType
231234
case `case`
@@ -282,9 +285,9 @@ extension Symbol: Codable {
282285

283286
let declaration = try container.decodeIfPresent([Token].self, forKey: .declaration)
284287
let documentation = try container.decodeIfPresent(Documentation.self, forKey: .documentation)
285-
let sourceLocation = try container.decodeIfPresent(SourceLocation.self, forKey: .sourceLocation)
288+
let sourceRange = try container.decodeIfPresent(SourceRange.self, forKey: .sourceRange)
286289

287-
self.init(api: api, context: [] /* TODO */, declaration: declaration ?? [], documentation: documentation, sourceLocation: sourceLocation)
290+
self.init(api: api, context: [] /* TODO */, declaration: declaration ?? [], documentation: documentation, sourceRange: sourceRange)
288291
}
289292

290293
public func encode(to encoder: Encoder) throws {
@@ -323,6 +326,6 @@ extension Symbol: Codable {
323326
}
324327

325328
try container.encode(documentation, forKey: .documentation)
326-
try container.encode(sourceLocation, forKey: .sourceLocation)
329+
try container.encode(sourceRange, forKey: .sourceRange)
327330
}
328331
}

Sources/swift-doc/Extensions/DCOV+Extensions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ extension Entry {
77
let name = symbol.id.description
88
let type = String(describing: Swift.type(of: symbol.api))
99
let documented = symbol.isDocumented
10-
let file = symbol.sourceLocation?.file
11-
let line = symbol.sourceLocation?.line
12-
let column = symbol.sourceLocation?.column
10+
let file = symbol.sourceRange?.start.file
11+
let line = symbol.sourceRange?.start.line
12+
let column = symbol.sourceRange?.start.column
1313

1414
self.init(name: name, type: type, documented: documented, file: file, line: line, column: column)
1515
}

0 commit comments

Comments
 (0)