Skip to content
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

Fix issue where extensions would pick up documentation from previous tokens #104

Merged
merged 1 commit into from
Nov 21, 2015
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ None.
[JP Simard](https://github.com/jpsim)
[jazzy#374](https://github.com/realm/jazzy/issues/374)

* Fix issue where Swift extensions would pick up documentation from previous
tokens.
[JP Simard](https://github.com/jpsim)
[#65](https://github.com/jpsim/SourceKitten/issues/65)


## 0.6.1

Expand Down
5 changes: 5 additions & 0 deletions Source/SourceKittenFramework/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ public struct File {
public func getDocumentationCommentBody(dictionary: XPCDictionary, syntaxMap: SyntaxMap) -> String? {
return SwiftDocKey.getOffset(dictionary).flatMap { offset in
return syntaxMap.commentRangeBeforeOffset(Int(offset)).flatMap { commentByteRange in
let commentEndLine = (contents as NSString).lineAndCharacterForCharacterOffset(commentByteRange.endIndex)?.line
let tokenStartLine = (contents as NSString).lineAndCharacterForCharacterOffset(Int(offset))?.line
guard commentEndLine == tokenStartLine else {
return nil
}
return contents.byteRangeToNSRange(start: commentByteRange.startIndex, length: commentByteRange.endIndex - commentByteRange.startIndex).flatMap { nsRange in
return contents.commentBody(nsRange)
}
Expand Down
15 changes: 15 additions & 0 deletions Source/SourceKittenFramework/String+SourceKitten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ extension NSString {
return nil
}

public func lineAndCharacterForCharacterOffset(offset: Int) -> (line: Int, character: Int)? {
let range = NSRange(location: offset, length: 0)
var numberOfLines = 0, index = 0, lineRangeStart = 0, previousIndex = 0
while index < length {
numberOfLines++
if index > range.location {
break
}
lineRangeStart = numberOfLines
previousIndex = index
index = NSMaxRange(lineRangeForRange(NSRange(location: index, length: 1)))
}
return (lineRangeStart, range.location - previousIndex + 1)
}

/**
Returns a copy of `self` with the trailing contiguous characters belonging to `characterSet`
removed.
Expand Down
10 changes: 3 additions & 7 deletions Source/SourceKittenFramework/SyntaxKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ public enum SyntaxKind: String {
/// `typeidentifier`.
case Typeidentifier = "source.lang.swift.syntaxtype.typeidentifier"

/**
Returns true if the input is a comment-like syntax kind string.

- parameter string: Input string.
*/
internal static func isCommentLike(string: Swift.String) -> Bool {
return [Comment, CommentMark, CommentURL, DocComment, DocCommentField].map({ $0.rawValue }).contains(string)
/// Returns the valid documentation comment syntax kinds.
internal static func docComments() -> [SyntaxKind] {
return [CommentURL, DocComment, DocCommentField]
}
}
4 changes: 2 additions & 2 deletions Source/SourceKittenFramework/SyntaxMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public struct SyntaxMap {
*/
public func commentRangeBeforeOffset(offset: Int) -> Range<Int>? {
let tokensBeforeOffset = tokens.filter { $0.offset < offset }
let commentTokensImmediatelyPrecedingOffset = filterLastContiguous(tokensBeforeOffset) {
SyntaxKind.isCommentLike($0.type)
let commentTokensImmediatelyPrecedingOffset = filterLastContiguous(tokensBeforeOffset) { token in
SyntaxKind.docComments().map({$0.rawValue}).contains(token.type)
}
return commentTokensImmediatelyPrecedingOffset.first.flatMap { firstToken in
return commentTokensImmediatelyPrecedingOffset.last.map { lastToken in
Expand Down
2 changes: 0 additions & 2 deletions Source/SourceKittenFrameworkTests/Fixtures/Commandant.json
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@
{
"key.kind" : "source.lang.swift.decl.extension",
"key.offset" : 3535,
"key.doc.comment" : "Attempts to parse a value from the given command-line argument.",
"key.namelength" : 3,
"key.bodyoffset" : 3564,
"key.bodylength" : 119,
Expand Down Expand Up @@ -811,7 +810,6 @@
{
"key.kind" : "source.lang.swift.decl.extension",
"key.offset" : 3686,
"key.doc.comment" : "Attempts to parse a value from the given command-line argument.",
"key.modulename" : "Swift",
"key.namelength" : 6,
"key.bodyoffset" : 3718,
Expand Down