Skip to content

Commit

Permalink
Merge pull request #305 from jpsim/jp-fix-missing-enum-case-objc-docs
Browse files Browse the repository at this point in the history
fix ObjC enum cases not being documented
  • Loading branch information
jpsim committed Dec 1, 2016
2 parents 93454d8 + 9763f2c commit 69ef6f6
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 44 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

##### Bug Fixes

* None.
* Fix Objective-C enum cases not being documented.
[JP Simard](https://github.com/jpsim)
[#304](https://github.com/jpsim/SourceKitten/issues/304)

## 0.15.1

Expand Down
15 changes: 4 additions & 11 deletions Source/SourceKittenFramework/Clang+SourceKitten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,8 @@ extension CXCursor {
}

func name() -> String {
let spelling = clang_getCursorSpelling(self).str()!
let type = objCKind()
if let usrString = usr(), spelling.isEmpty && type == .enum {
// libClang considers enums declared like `typedef enum {} name;` rather than `NS_ENUM()`
// to have a cursor spelling of "" (empty string). So we parse the USR to extract the actual name.
let prefix = "c:@EA@"
assert(usrString.hasPrefix(prefix))
let index = usrString.index(usrString.startIndex,
offsetBy: prefix.lengthOfBytes(using: .utf8))
return usrString.substring(from: index)
} else if type == .category, let usrNSString = usr() as NSString? {
if type == .category, let usrNSString = usr() as NSString? {
let ext = (usrNSString.range(of: "c:objc(ext)").location == 0)
let regex = try! NSRegularExpression(pattern: "(\\w+)@(\\w+)", options: [])
let range = NSRange(location: 0, length: usrNSString.length)
Expand All @@ -116,7 +107,9 @@ extension CXCursor {
} else {
fatalError("Couldn't get category name")
}
} else if type == .methodInstance {
}
let spelling = clang_getCursorSpelling(self).str()!
if type == .methodInstance {
return "-" + spelling
} else if type == .methodClass {
return "+" + spelling
Expand Down
1 change: 1 addition & 0 deletions Source/SourceKittenFramework/ClangTranslationUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public struct ClangTranslationUnit {
clangTranslationUnits = headerFiles.map { clangIndex.open(file: $0, args: cStringCompilerArguments) }
declarations = clangTranslationUnits
.flatMap { $0.cursor().flatMap({ SourceDeclaration(cursor: $0, compilerArguments: compilerArguments) }) }
.rejectEmptyDuplicateEnums()
.distinct()
.sorted()
.groupBy { $0.location.file }
Expand Down
14 changes: 14 additions & 0 deletions Source/SourceKittenFramework/SourceDeclaration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ extension Sequence where Iterator.Element == SourceDeclaration {
}
return filter { !propertyGetterSetterUSRs.contains($0.usr!) }
}

/// Reject one of an enum duplicate pair that's empty if the other isn't.
func rejectEmptyDuplicateEnums() -> [SourceDeclaration] {
let enums = filter { $0.type == .enum }
let enumUSRs = enums.map { $0.usr! }
let dupedEmptyUSRs = enumUSRs.filter { usr in
let enumsForUSR = enums.filter { $0.usr == usr }
let childCounts = Set(enumsForUSR.map({ $0.children.count }))
return childCounts.count > 1 && childCounts.contains(0)
}
return filter {
$0.type != .enum || !dupedEmptyUSRs.contains($0.usr!) || !$0.children.isEmpty
}
}
}

extension SourceDeclaration: Hashable {
Expand Down
3 changes: 3 additions & 0 deletions Tests/SourceKittenFrameworkTests/Fixtures/CommandantSPM.json
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@
},
{
"key.annotated_decl" : "<RelatedName usr=\"s:F6Resultoi2eeuRxS_14ResultProtocolwx5Errors9Equatablewx5ValueS2_rFTxx_Sb\">==&lt;T : ResultProtocol where T.Value : Equatable, T.Error : Equatable&gt;(_: T, _: T) -&gt; Bool<\/RelatedName>"
},
{
"key.annotated_decl" : "<RelatedName usr=\"s:ZFO6Result7NoErroroi2eeFTS0_S0__Sb\">==(_: NoError, _: NoError) -&gt; Bool<\/RelatedName>"
}
],
"key.bodyoffset" : 633,
Expand Down
Loading

0 comments on commit 69ef6f6

Please sign in to comment.