Skip to content

[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

Open
wants to merge 1 commit into
base: typed-declarations
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Owner Author

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.

usr = cursor.usr()
declaration = cursor.declaration()
//documentationComment = cursor.parsedComment() // FIXME: Cannot assign value of type 'CXComment' to type 'String?'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the correct way to convert CXComment to a String?

children = cursor.flatMap(Declaration.init).rejectPropertyMethods()
}

/// Returns the USR for the auto-generated getter for this property.
///
Expand All @@ -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")
}
Expand All @@ -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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precondition(language == .ObjC)?

Copy link
Owner Author

Choose a reason for hiding this comment

The 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?
Copy link
Owner Author

Choose a reason for hiding this comment

The 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
Copy link
Owner Author

Choose a reason for hiding this comment

The 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]'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't figure out how to convert XPCArray? to [DeclarationType].

//accessibility = // FIXME: Accessibility(rawValue: ...)
Copy link
Owner Author

Choose a reason for hiding this comment

The 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) }

}
}
20 changes: 0 additions & 20 deletions Source/SourceKittenFramework/DeclarationType.swift

This file was deleted.

75 changes: 0 additions & 75 deletions Source/SourceKittenFramework/SwiftDeclaration.swift

This file was deleted.

16 changes: 4 additions & 12 deletions sourcekitten.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
2C0A78691C111E2500C64CB0 /* DeclarationType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0A78681C111E2500C64CB0 /* DeclarationType.swift */; };
2C0A786B1C111F2500C64CB0 /* SwiftDeclaration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0A786A1C111F2500C64CB0 /* SwiftDeclaration.swift */; };
2C0A78691C111E2500C64CB0 /* Declaration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0A78681C111E2500C64CB0 /* Declaration.swift */; };
2C0A786D1C111FC100C64CB0 /* Accessibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0A786C1C111FC100C64CB0 /* Accessibility.swift */; };
2C0A786F1C11222700C64CB0 /* DeclarationKindType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0A786E1C11222700C64CB0 /* DeclarationKindType.swift */; };
2C184FEB1C121838001AA834 /* ObjCDeclaration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C184FEA1C121838001AA834 /* ObjCDeclaration.swift */; };
2C55B3321BEB3CA7002E8C6B /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E834D61D1B2D054B002AA1FE /* Result.framework */; };
2C55B3331BEB3CAB002E8C6B /* Commandant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8EBAA5D1A5D374B002F1B8E /* Commandant.framework */; };
2C55B3341BEB3D7D002E8C6B /* Commandant.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = E8EBAA5D1A5D374B002F1B8E /* Commandant.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -130,11 +128,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2C0A78681C111E2500C64CB0 /* DeclarationType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeclarationType.swift; sourceTree = "<group>"; };
2C0A786A1C111F2500C64CB0 /* SwiftDeclaration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftDeclaration.swift; sourceTree = "<group>"; };
2C0A78681C111E2500C64CB0 /* Declaration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Declaration.swift; sourceTree = "<group>"; };
2C0A786C1C111FC100C64CB0 /* Accessibility.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Accessibility.swift; sourceTree = "<group>"; };
2C0A786E1C11222700C64CB0 /* DeclarationKindType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeclarationKindType.swift; sourceTree = "<group>"; };
2C184FEA1C121838001AA834 /* ObjCDeclaration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjCDeclaration.swift; sourceTree = "<group>"; };
3F0CBB401BAAFF160015BBA8 /* Clang+SourceKitten.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Clang+SourceKitten.swift"; sourceTree = "<group>"; };
3F56EACF1BAB251C006433D0 /* JSONOutput.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONOutput.swift; sourceTree = "<group>"; };
5499CA961A2394B700783309 /* Components.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Components.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -378,7 +374,7 @@
E8D4743A1A648F290011A49C /* ClangTranslationUnit.swift */,
E8EE34BE1B9A502F00947605 /* CodeCompletionItem.swift */,
2C0A786E1C11222700C64CB0 /* DeclarationKindType.swift */,
2C0A78681C111E2500C64CB0 /* DeclarationType.swift */,
2C0A78681C111E2500C64CB0 /* Declaration.swift */,
E852418E1A5F4FB3007099FB /* Dictionary+Merge.swift */,
E806D2921BE058D600D1BE41 /* Documentation.swift */,
E84763691A5A0651000EAE22 /* File.swift */,
Expand All @@ -395,7 +391,6 @@
E806D28C1BE0589B00D1BE41 /* SourceLocation.swift */,
E8AE53C61A5B5FCA0092D24A /* String+SourceKitten.swift */,
E834740E1A593B5B00532B9A /* Structure.swift */,
2C0A786A1C111F2500C64CB0 /* SwiftDeclaration.swift */,
E89291A81A5B800300D91568 /* SwiftDeclarationKind.swift */,
E89291A61A5B7FF800D91568 /* SwiftDocKey.swift */,
E8A18A3E1A592246000362B7 /* SwiftDocs.swift */,
Expand All @@ -405,7 +400,6 @@
E80F236A1A5CB04100FD2352 /* SyntaxToken.swift */,
E806D28E1BE058B100D1BE41 /* Text.swift */,
E8A9B88F1B56CB5500CD17D4 /* Xcode.swift */,
2C184FEA1C121838001AA834 /* ObjCDeclaration.swift */,
);
name = SourceKittenFramework;
path = Source/SourceKittenFramework;
Expand Down Expand Up @@ -627,10 +621,9 @@
E8D4743B1A648F290011A49C /* ClangTranslationUnit.swift in Sources */,
E8EE34BF1B9A502F00947605 /* CodeCompletionItem.swift in Sources */,
E852418F1A5F4FB3007099FB /* Dictionary+Merge.swift in Sources */,
2C0A78691C111E2500C64CB0 /* DeclarationType.swift in Sources */,
2C0A78691C111E2500C64CB0 /* Declaration.swift in Sources */,
E847636A1A5A0651000EAE22 /* File.swift in Sources */,
3F56EAD01BAB251C006433D0 /* JSONOutput.swift in Sources */,
2C184FEB1C121838001AA834 /* ObjCDeclaration.swift in Sources */,
E8A18A3B1A58971D000362B7 /* Language.swift in Sources */,
E806D28F1BE058B100D1BE41 /* Text.swift in Sources */,
E8241CA51A5E01A10047687E /* Module.swift in Sources */,
Expand All @@ -642,7 +635,6 @@
E806D28D1BE0589B00D1BE41 /* SourceLocation.swift in Sources */,
E868473E1A587CCC0043DC65 /* sourcekitd.swift in Sources */,
E8AE53C71A5B5FCA0092D24A /* String+SourceKitten.swift in Sources */,
2C0A786B1C111F2500C64CB0 /* SwiftDeclaration.swift in Sources */,
E834740F1A593B5B00532B9A /* Structure.swift in Sources */,
E89291A91A5B800300D91568 /* SwiftDeclarationKind.swift in Sources */,
E89291A71A5B7FF800D91568 /* SwiftDocKey.swift in Sources */,
Expand Down