-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] Consolidate Declaration types. #2
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
base: typed-declarations
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,67 @@ | ||
// | ||
// ObjCDeclaration.swift | ||
// Declaration.swift | ||
// SourceKitten | ||
// | ||
// Created by Paul Young on 12/4/15. | ||
// Created by Paul Young on 12/10/15. | ||
// Copyright © 2015 SourceKitten. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftXPC | ||
|
||
public struct ObjCDeclaration: DeclarationType { | ||
public let language: Language = .Swift | ||
public let kind: ObjCDeclarationKind? // FIXME: Type 'ObjCDeclaration' does not conform to protocol 'DeclarationType' | ||
public struct Declaration { | ||
public let language: Language | ||
public let kind: DeclarationKindType? | ||
public let location: SourceLocation | ||
public let extent: (start: SourceLocation, end: SourceLocation) | ||
public let name: String? | ||
public let typeName: String? | ||
public let usr: String? | ||
public let declaration: String? | ||
public let documentationComment: String? | ||
public let children: [DeclarationType] | ||
public let children: [Declaration] | ||
public let accessibility: Accessibility? | ||
} | ||
|
||
// MARK: Hashable | ||
|
||
extension Declaration: Hashable { | ||
public var hashValue: Int { | ||
return usr?.hashValue ?? 0 | ||
} | ||
} | ||
|
||
public func ==(lhs: Declaration, rhs: Declaration) -> 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: Declaration, rhs: Declaration) -> Bool { | ||
return lhs.location < rhs.location | ||
} | ||
|
||
|
||
// MARK: Objective-C Declaration | ||
|
||
extension Declaration { | ||
public init?(cursor: CXCursor) { | ||
guard cursor.shouldDocument() else { | ||
return nil | ||
} | ||
language = .ObjC | ||
kind = cursor.objCKind() | ||
extent = cursor.extent() | ||
name = cursor.name() | ||
//typeName = cursor. // FIXME: no cursor.typeName() | ||
usr = cursor.usr() | ||
declaration = cursor.declaration() | ||
//documentationComment = cursor.parsedComment() // FIXME: Cannot assign value of type 'CXComment' to type 'String?' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the correct way to convert |
||
children = cursor.flatMap(Declaration.init).rejectPropertyMethods() | ||
} | ||
|
||
/// Returns the USR for the auto-generated getter for this property. | ||
/// | ||
|
@@ -35,7 +78,7 @@ public struct ObjCDeclaration: DeclarationType { | |
} | ||
|
||
private func generateAccessorUSR(getter getter: Bool) -> String { | ||
assert(kind == .Property) | ||
assert(isProperty) | ||
guard let usr = usr else { | ||
fatalError("Couldn't extract USR") | ||
} | ||
|
@@ -58,53 +101,58 @@ public struct ObjCDeclaration: DeclarationType { | |
let restOfSetterName = usr.substringFromIndex(pyStartIndex.advancedBy(5)) | ||
return "\(usrPrefix)(im)set\(capitalFirstLetter)\(restOfSetterName):" | ||
} | ||
} | ||
|
||
extension ObjCDeclaration { | ||
public init?(cursor: CXCursor) { | ||
guard cursor.shouldDocument() else { | ||
return nil | ||
|
||
var isProperty: Bool { | ||
guard let objCKind = kind as? ObjCDeclarationKind? where objCKind == .Property else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call ☎️ |
||
return false | ||
} | ||
kind = cursor.objCKind() | ||
extent = cursor.extent() | ||
name = cursor.name() | ||
//typeName = cursor. // FIXME: no cursor.typeName() | ||
usr = cursor.usr() | ||
declaration = cursor.declaration() | ||
documentationComment = cursor.parsedComment() // FIXME: Cannot assign value of type 'CXComment' to type 'String?' | ||
children = cursor.flatMap(ObjCDeclaration.init).rejectPropertyMethods() // FIXME: Cannot assign value of type '[ObjCDeclaration]' to type '[DeclarationType]' | ||
return true | ||
} | ||
} | ||
|
||
extension SequenceType where Generator.Element == ObjCDeclaration { | ||
extension SequenceType where Generator.Element == Declaration { | ||
/// Removes implicitly generated property getters & setters | ||
func rejectPropertyMethods() -> [ObjCDeclaration] { | ||
func rejectPropertyMethods() -> [Declaration] { | ||
let propertyGetterSetterUSRs = filter { | ||
$0.kind == .Property | ||
$0.isProperty | ||
}.flatMap { | ||
[$0.getterUSR, $0.setterUSR] | ||
} | ||
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 | ||
// MARK: - Swift Declaration | ||
|
||
/// 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 | ||
extension Declaration { | ||
public init(dictionary: XPCDictionary) { | ||
language = .Swift | ||
kind = SwiftDocKey.getKind(dictionary).flatMap { SwiftDeclarationKind(rawValue: $0) } // FIXME: why doesn't .flatMap(SwiftDeclarationKind.init) work here? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no big deal but I plan to file a bug report. |
||
|
||
if let file = SwiftDocKey.getDocFile(dictionary), | ||
line = SwiftDocKey.getDocLine(dictionary).map({ UInt32($0) }), | ||
column = SwiftDocKey.getDocColumn(dictionary).map({ UInt32($0) }) { | ||
|
||
if let offset = SwiftDocKey.getOffset(dictionary).map({ UInt32($0) }) { | ||
location = SourceLocation(file: file, line: line, column: column, offset: offset) | ||
} | ||
|
||
if let parsedScopeStart = SwiftDocKey.getParsedScopeStart(dictionary).map({ UInt32($0) }), | ||
parsedScopeEnd = SwiftDocKey.getParsedScopeEnd(dictionary).map({ UInt32($0) }) { | ||
|
||
let start = SourceLocation.init(file: file, line: line, column: column, offset: parsedScopeStart) | ||
let end = SourceLocation.init(file: file, line: line, column: column, offset: parsedScopeEnd) | ||
extent = (start: start, end: end) | ||
} | ||
} | ||
|
||
name = SwiftDocKey.getName(dictionary) | ||
typeName = SwiftDocKey.getTypeName(dictionary) | ||
usr = SwiftDocKey.getUSR(dictionary) | ||
declaration = SwiftDocKey.getParsedDeclaration(dictionary) | ||
//documentationComment = // FIXME | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I was uncertain how to get this information. |
||
//children = SwiftDocKey.getSubstructure(dictionary) // FIXME: Cannot assign value of type 'XPCArray?' to type '[DeclarationType]' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't figure out how to convert |
||
//accessibility = // FIXME: Accessibility(rawValue: ...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine this probably needs to be something like: accessibility = SwiftDocKey.getAccessibility(dictionary).flatMap { Accessibility(rawValue: $0) } |
||
} | ||
} |
This file was deleted.
This file was deleted.
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'm just not sure how to get this information.