0001 // 0002 // SourceDeclaration.swift 0003 // SourceKitten 0004 // 0005 // Created by JP Simard on 7/15/15. 0006 // Copyright © 2015 SourceKitten. All rights reserved. 0007 // 0008 0009 #if SWIFT_PACKAGE 0010 import Clang_C 0011 #endif 0012 import Foundation 0013 0014 public func insertMarks(declarations: [SourceDeclaration], limitRange: NSRange? = nil) -> [SourceDeclaration] { 0015 guard declarations.count > 0 else { return [] } 0016 guard let path = declarations.first?.location.file, let file = File(path: path) else { 0017 fatalError("can't extract marks without a file.") 0018 } 0019 let currentMarks = file.contents.pragmaMarks(path, excludeRanges: declarations.map({ 0020 file.contents.byteRangeToNSRange(start: $0.range.location, length: $0.range.length) ?? NSRange() 0021 }), limitRange: limitRange) 0022 let newDeclarations: [SourceDeclaration] = declarations.map { declaration in 0023 var varDeclaration = declaration 0024 let range = file.contents.byteRangeToNSRange(start: declaration.range.location, length: declaration.range.length) 0025 varDeclaration.children = insertMarks(declaration.children, limitRange: range) 0026 return varDeclaration 0027 } 0028 return (newDeclarations + currentMarks).sort() 0029 } 0030 0031 /// Represents a source code declaration. 0032 public struct SourceDeclaration
ClangTranslationUnit.swift:68 .map { insertMarks($0) }SourceDeclaration.swift:25 varDeclaration.children = insertMarks(declaration.children, limitRange: range){ 0033 let type
ClangTranslationUnit.swift:51 public let declarations: [String: [SourceDeclaration]]ClangTranslationUnit.swift:64 .flatMap { $0.cursor().flatMap(SourceDeclaration.init) }JSONOutput.swift:60 public func declarationsToJSON(decl: [String: [SourceDeclaration]]) -> String {JSONOutput.swift:64 private func toOutputDictionary(decl: SourceDeclaration) -> [String: AnyObject] {JSONOutput.swift:100 private func toOutputDictionary(decl: [SourceDeclaration]) -> [String: AnyObject] {SourceDeclaration.swift:14 public func insertMarks(declarations: [SourceDeclaration], limitRange: NSRange? = nil) -> [SourceDeclaration] {SourceDeclaration.swift:14 public func insertMarks(declarations: [SourceDeclaration], limitRange: NSRange? = nil) -> [SourceDeclaration] {SourceDeclaration.swift:22 let newDeclarations: [SourceDeclaration] = declarations.map { declaration inSourceDeclaration.swift:41 var children: [SourceDeclaration]SourceDeclaration.swift:88 extension SourceDeclaration {SourceDeclaration.swift:101 children = cursor.flatMap(SourceDeclaration.init).rejectPropertyMethods()SourceDeclaration.swift:107 func rejectPropertyMethods() -> [SourceDeclaration] {SourceDeclaration.swift:117 extension SourceDeclaration: Hashable {SourceDeclaration.swift:123 public func ==(lhs: SourceDeclaration, rhs: SourceDeclaration) -> Bool {SourceDeclaration.swift:123 public func ==(lhs: SourceDeclaration, rhs: SourceDeclaration) -> Bool {SourceDeclaration.swift:130 extension SourceDeclaration: Comparable {}SourceDeclaration.swift:134 public func <(lhs: SourceDeclaration, rhs: SourceDeclaration) -> Bool {SourceDeclaration.swift:134 public func <(lhs: SourceDeclaration, rhs: SourceDeclaration) -> Bool {String+SourceKitten.swift:389 public func pragmaMarks(filename: String, excludeRanges: [NSRange], limitRange: NSRange?) -> [SourceDeclaration] {String+SourceKitten.swift:417 return SourceDeclaration(type: .Mark, location: location, extent: (location, location), name: markString,: ObjCDeclarationKind 0034 let location
JSONOutput.swift:77 set(.Kind, decl.type.rawValue)SourceDeclaration.swift:63 assert(type == .Property)SourceDeclaration.swift:93 type = cursor.objCKind()SourceDeclaration.swift:109 $0.type == .Property: SourceLocation 0035 let extent
ClangTranslationUnit.swift:67 .groupBy { $0.location.file }JSONOutput.swift:78 set(.FilePath, decl.location.file)JSONOutput.swift:79 set(.DocFile, decl.location.file)JSONOutput.swift:80 set(.DocLine, Int(decl.location.line))JSONOutput.swift:81 set(.DocColumn, Int(decl.location.column))SourceDeclaration.swift:16 guard let path = declarations.first?.location.file, let file = File(path: path) else {SourceDeclaration.swift:94 location = cursor.location()SourceDeclaration.swift:125 lhs.location == rhs.locationSourceDeclaration.swift:125 lhs.location == rhs.locationSourceDeclaration.swift:135 return lhs.location < rhs.locationSourceDeclaration.swift:135 return lhs.location < rhs.location: (start: SourceLocation, end: SourceLocation) 0036 let name
JSONOutput.swift:86 set(.ParsedScopeStart, Int(decl.extent.start.line))JSONOutput.swift:87 set(.ParsedScopeEnd, Int(decl.extent.end.line))SourceDeclaration.swift:45 return extent.start.rangeToEndLocation(extent.end)SourceDeclaration.swift:45 return extent.start.rangeToEndLocation(extent.end)SourceDeclaration.swift:95 extent = cursor.extent(): String? 0037 let usr
JSONOutput.swift:82 set(.Name, decl.name)SourceDeclaration.swift:96 name = cursor.name(): String? 0038 let declaration
JSONOutput.swift:83 set(.USR, decl.usr)SourceDeclaration.swift:64 guard let usr = usr else {SourceDeclaration.swift:97 usr = cursor.usr()SourceDeclaration.swift:113 return filter { !propertyGetterSetterUSRs.contains($0.usr!) }SourceDeclaration.swift:119 return usr?.hashValue ?? 0SourceDeclaration.swift:124 return lhs.usr == rhs.usr &&SourceDeclaration.swift:124 return lhs.usr == rhs.usr &&: String? 0039 let documentation
JSONOutput.swift:84 set(.ParsedDeclaration, decl.declaration)SourceDeclaration.swift:67 guard let declaration = declaration else {SourceDeclaration.swift:98 declaration = cursor.declaration(): Documentation? 0040 let commentBody
JSONOutput.swift:89 setA(.DocResultDiscussion, decl.documentation?.returnDiscussion.map(toOutputDictionary))JSONOutput.swift:90 setA(.DocParameters, decl.documentation?.parameters.map(toOutputDictionary))SourceDeclaration.swift:99 documentation = Documentation(comment: cursor.parsedComment()): String? 0041 var children
JSONOutput.swift:85 set(.DocumentationComment, decl.commentBody)JSONOutput.swift:93 if decl.commentBody != nil {SourceDeclaration.swift:100 commentBody = cursor.commentBody(): [SourceDeclaration] 0042 0043 /// Range 0044 var range
JSONOutput.swift:91 setA(.Substructure, decl.children.map(toOutputDictionary))SourceDeclaration.swift:25 varDeclaration.children = insertMarks(declaration.children, limitRange: range)SourceDeclaration.swift:25 varDeclaration.children = insertMarks(declaration.children, limitRange: range)SourceDeclaration.swift:101 children = cursor.flatMap(SourceDeclaration.init).rejectPropertyMethods(): NSRange { 0045 return extent.start.rangeToEndLocation(extent.end) 0046 } 0047 0048 /// Returns the USR for the auto-generated getter for this property. 0049 /// 0050 /// - warning: can only be invoked if `type == .Property`. 0051 var getterUSR
SourceDeclaration.swift:20 file.contents.byteRangeToNSRange(start: $0.range.location, length: $0.range.length) ?? NSRange()SourceDeclaration.swift:20 file.contents.byteRangeToNSRange(start: $0.range.location, length: $0.range.length) ?? NSRange()SourceDeclaration.swift:24 let range = file.contents.byteRangeToNSRange(start: declaration.range.location, length: declaration.range.length)SourceDeclaration.swift:24 let range = file.contents.byteRangeToNSRange(start: declaration.range.location, length: declaration.range.length): String { 0052 return generateAccessorUSR(getter: true) 0053 } 0054 0055 /// Returns the USR for the auto-generated setter for this property. 0056 /// 0057 /// - warning: can only be invoked if `type == .Property`. 0058 var setterUSR
SourceDeclaration.swift:111 [$0.getterUSR, $0.setterUSR]: String { 0059 return generateAccessorUSR(getter: false) 0060 } 0061 0062 private func generateAccessorUSR
SourceDeclaration.swift:111 [$0.getterUSR, $0.setterUSR](getter getter: Bool) -> String { 0063 assert(type == .Property) 0064 guard let usr = usr else { 0065 fatalError("Couldn't extract USR") 0066 } 0067 guard let declaration = declaration else { 0068 fatalError("Couldn't extract declaration") 0069 } 0070 let pyStartIndex = usr.rangeOfString("(py)")!.startIndex 0071 let usrPrefix = usr.substringToIndex(pyStartIndex) 0072 let fullDeclarationRange = NSRange(location: 0, length: (declaration as NSString).length) 0073 let regex = try! NSRegularExpression(pattern: getter ? "getter\\s*=\\s*(\\w+)" : "setter\\s*=\\s*(\\w+:)", options: []) 0074 let matches = regex.matchesInString(declaration, options: [], range: fullDeclarationRange) 0075 if matches.count > 0 { 0076 let accessorName = (declaration as NSString).substringWithRange(matches[0].rangeAtIndex(1)) 0077 return usrPrefix + "(im)\(accessorName)" 0078 } else if getter { 0079 return usr.stringByReplacingOccurrencesOfString("(py)", withString: "(im)") 0080 } 0081 // Setter 0082 let capitalFirstLetter = String(usr.characters[pyStartIndex.advancedBy(4)]).capitalizedString 0083 let restOfSetterName = usr.substringFromIndex(pyStartIndex.advancedBy(5)) 0084 return "\(usrPrefix)(im)set\(capitalFirstLetter)\(restOfSetterName):" 0085 } 0086 } 0087 0088 extension SourceDeclaration { 0089 init
SourceDeclaration.swift:52 return generateAccessorUSR(getter: true)SourceDeclaration.swift:59 return generateAccessorUSR(getter: false)?(cursor: CXCursor) { 0090 guard cursor.shouldDocument() else { 0091 return nil 0092 } 0093 type = cursor.objCKind() 0094 location = cursor.location() 0095 extent = cursor.extent() 0096 name = cursor.name() 0097 usr = cursor.usr() 0098 declaration = cursor.declaration() 0099 documentation = Documentation(comment: cursor.parsedComment()) 0100 commentBody = cursor.commentBody() 0101 children = cursor.flatMap(SourceDeclaration.init).rejectPropertyMethods() 0102 } 0103 } 0104 0105 extension SequenceType where Generator.Element == SourceDeclaration { 0106 /// Removes implicitly generated property getters & setters 0107 func rejectPropertyMethods
ClangTranslationUnit.swift:64 .flatMap { $0.cursor().flatMap(SourceDeclaration.init) }SourceDeclaration.swift:101 children = cursor.flatMap(SourceDeclaration.init).rejectPropertyMethods()() -> [SourceDeclaration] { 0108 let propertyGetterSetterUSRs = filter { 0109 $0.type == .Property 0110 }.flatMap { 0111 [$0.getterUSR, $0.setterUSR] 0112 } 0113 return filter { !propertyGetterSetterUSRs.contains($0.usr!) } 0114 } 0115 } 0116 0117 extension SourceDeclaration: Hashable { 0118 public var hashValue: Int { 0119 return usr?.hashValue ?? 0 0120 } 0121 } 0122 0123 public func ==(lhs: SourceDeclaration, rhs: SourceDeclaration) -> Bool { 0124 return lhs.usr == rhs.usr && 0125 lhs.location == rhs.location 0126 } 0127 0128 // MARK: Comparable 0129 0130 extension SourceDeclaration: Comparable {} 0131 0132 /// A [strict total order](http://en.wikipedia.org/wiki/Total_order#Strict_total_order) 0133 /// over instances of `Self`. 0134 public func <(lhs: SourceDeclaration, rhs: SourceDeclaration) -> Bool { 0135 return lhs.location < rhs.location 0136 } 0137
SourceDeclaration.swift:101 children = cursor.flatMap(SourceDeclaration.init).rejectPropertyMethods()