From 43086b1a188209bdf9c8bceb0545592dcbe4b711 Mon Sep 17 00:00:00 2001 From: Paul Young Date: Fri, 4 Dec 2015 20:47:57 +0000 Subject: [PATCH 1/2] Move Hashable, Comparable conformance to DeclarationType. * Add AnyDeclarationType. --- .../DeclarationType.swift | 51 ++++++++++++++++++- .../ObjCDeclaration.swift | 21 -------- .../SwiftDeclaration.swift | 21 -------- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/Source/SourceKittenFramework/DeclarationType.swift b/Source/SourceKittenFramework/DeclarationType.swift index ae2882d56..c6505397b 100644 --- a/Source/SourceKittenFramework/DeclarationType.swift +++ b/Source/SourceKittenFramework/DeclarationType.swift @@ -6,7 +6,7 @@ // Copyright © 2015 SourceKitten. All rights reserved. // -public protocol DeclarationType { +public protocol DeclarationType: Hashable, Comparable { var language: Language { get } var kind: DeclarationKindType? { get } var location: SourceLocation { get } @@ -16,5 +16,52 @@ public protocol DeclarationType { var usr: String? { get } var declaration: String? { get } var documentationComment: String? { get } - var children: [DeclarationType] { get } + var children: [AnyDeclarationType] { get } +} + +// MARK: Hashable + +extension DeclarationType { + public var hashValue: Int { + return usr?.hashValue ?? 0 + } +} + +public func ==(lhs: AnyDeclarationType, rhs: AnyDeclarationType) -> Bool { + return lhs.usr == rhs.usr && + lhs.location == rhs.location +} + +// MARK: Comparable + +/// A [strict total order](http://en.wikipedia.org/wiki/Total_order#Strict_total_order) +/// over instances of `Self`. +public func <(lhs: AnyDeclarationType, rhs: AnyDeclarationType) -> Bool { + return lhs.location < rhs.location +} + +public struct AnyDeclarationType: DeclarationType { + public let language: Language + public let kind: DeclarationKindType? + public let location: SourceLocation + public let extent: Range // FIXME: Type 'SourceLocation' does not conform to protocol 'ForwardIndexType' + public let name: String? + public let typeName: String? + public let usr: String? + public let declaration: String? + public let documentationComment: String? + public let children: [AnyDeclarationType] + + init(_ declarationType: T) { + language = declarationType.language + kind = declarationType.kind + location = declarationType.location + extent = declarationType.extent + name = declarationType.name + typeName = declarationType.typeName + usr = declarationType.usr + declaration = declarationType.declaration + documentationComment = declarationType.documentationComment + children = declarationType.children + } } diff --git a/Source/SourceKittenFramework/ObjCDeclaration.swift b/Source/SourceKittenFramework/ObjCDeclaration.swift index 1a7ed625c..2db912dcb 100644 --- a/Source/SourceKittenFramework/ObjCDeclaration.swift +++ b/Source/SourceKittenFramework/ObjCDeclaration.swift @@ -87,24 +87,3 @@ extension SequenceType where Generator.Element == ObjCDeclaration { return filter { !propertyGetterSetterUSRs.contains($0.usr!) } } } - -// MARK: Hashable - -extension ObjCDeclaration: Hashable { - public var hashValue: Int { - return usr?.hashValue ?? 0 - } -} - -public func ==(lhs: ObjCDeclaration, rhs: ObjCDeclaration) -> Bool { - return lhs.usr == rhs.usr && - lhs.location == rhs.location -} - -// MARK: Comparable - -/// A [strict total order](http://en.wikipedia.org/wiki/Total_order#Strict_total_order) -/// over instances of `Self`. -public func <(lhs: ObjCDeclaration, rhs: ObjCDeclaration) -> Bool { - return lhs.location < rhs.location -} diff --git a/Source/SourceKittenFramework/SwiftDeclaration.swift b/Source/SourceKittenFramework/SwiftDeclaration.swift index eabe710e3..98cfb75d9 100644 --- a/Source/SourceKittenFramework/SwiftDeclaration.swift +++ b/Source/SourceKittenFramework/SwiftDeclaration.swift @@ -36,24 +36,3 @@ extension SwiftDeclaration { accessibility = // FIXME: Accessibility(rawValue: ...) } } - -// MARK: Hashable - -extension SwiftDeclaration: Hashable { - public var hashValue: Int { - return usr?.hashValue ?? 0 - } -} - -public func ==(lhs: SwiftDeclaration, rhs: SwiftDeclaration) -> Bool { - return lhs.usr == rhs.usr && - lhs.location == rhs.location -} - -// MARK: Comparable - -/// A [strict total order](http://en.wikipedia.org/wiki/Total_order#Strict_total_order) -/// over instances of `Self`. -public func <(lhs: SwiftDeclaration, rhs: SwiftDeclaration) -> Bool { - return lhs.location < rhs.location -} From 4009d6a86e9cd1e0b5a4625a6b5f42f918c6b905 Mon Sep 17 00:00:00 2001 From: Paul Young Date: Fri, 4 Dec 2015 21:03:17 +0000 Subject: [PATCH 2/2] Add missing type-erasure declaration types. --- Source/SourceKittenFramework/ObjCDeclaration.swift | 2 +- Source/SourceKittenFramework/SwiftDeclaration.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/SourceKittenFramework/ObjCDeclaration.swift b/Source/SourceKittenFramework/ObjCDeclaration.swift index 2db912dcb..d0ff88f2f 100644 --- a/Source/SourceKittenFramework/ObjCDeclaration.swift +++ b/Source/SourceKittenFramework/ObjCDeclaration.swift @@ -18,7 +18,7 @@ public struct ObjCDeclaration: DeclarationType { public let usr: String? public let declaration: String? public let documentationComment: String? - public let children: [DeclarationType] + public let children: [AnyDeclarationType] /// Returns the USR for the auto-generated getter for this property. /// diff --git a/Source/SourceKittenFramework/SwiftDeclaration.swift b/Source/SourceKittenFramework/SwiftDeclaration.swift index 98cfb75d9..27812a8e1 100644 --- a/Source/SourceKittenFramework/SwiftDeclaration.swift +++ b/Source/SourceKittenFramework/SwiftDeclaration.swift @@ -18,7 +18,7 @@ public struct SwiftDeclaration: DeclarationType { public let usr: String? public let declaration: String? public let documentationComment: String? - public let children: [DeclarationType] + public let children: [AnyDeclarationType] public let accessibility: Accessibility? }