-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Angel Garcia
authored and
Angel Garcia
committed
Jun 22, 2018
1 parent
0009e04
commit 8d6615c
Showing
4 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Sources/SwiftKotlinFramework/plugins/CommentsAdditionTransformPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// CommentsAdditionTransformPlugin.swift | ||
// SwiftKotlinPackageDescription | ||
// | ||
// Created by Angel Garcia on 22/06/2018. | ||
// | ||
|
||
import Foundation | ||
import Transform | ||
import AST | ||
import Source | ||
|
||
public class CommentsAdditionTransformPlugin: TokenTransformPlugin { | ||
public var name: String { | ||
return "Comments addition" | ||
} | ||
|
||
public var description: String { | ||
return "Adds parse comments to closest generated element" | ||
} | ||
|
||
public init() {} | ||
|
||
public func transform(tokens: [Token], topDeclaration: TopLevelDeclaration) throws -> [Token] { | ||
var newTokens = [Token]() | ||
var sortedComments = topDeclaration.comments.sorted { $0.location.line < $1.location.line } | ||
|
||
var position = 0 | ||
while position < tokens.count && !sortedComments.isEmpty { | ||
let token = tokens[position] | ||
let comment = sortedComments[0] | ||
var consumeComment = false | ||
|
||
if let tokenRange = token.sourceRange, | ||
tokenRange.isValid { | ||
|
||
if tokenRange.start.isAfter(location: comment.location) { | ||
consumeComment = true | ||
} | ||
} | ||
|
||
if consumeComment, let node = token.node { | ||
newTokens.append(node.newToken(.comment, comment.fomattedContent())) | ||
sortedComments.removeFirst() | ||
} else { | ||
newTokens.append(token) | ||
position += 1 | ||
} | ||
} | ||
|
||
newTokens += tokens[position...] | ||
|
||
while !sortedComments.isEmpty { | ||
let comment = sortedComments[0] | ||
newTokens.append(topDeclaration.newToken(.comment, comment.fomattedContent())) | ||
sortedComments.removeFirst() | ||
} | ||
|
||
return newTokens | ||
} | ||
} | ||
|
||
extension Comment { | ||
func fomattedContent() -> String { | ||
if content.contains("\n") { | ||
return "/*\(content)*/\n" | ||
} else { | ||
return "//\(content)\n" | ||
} | ||
} | ||
} | ||
|
||
extension Token { | ||
var sourceRange: SourceRange? { | ||
return (origin as? SourceLocatable)?.sourceRange | ||
} | ||
} | ||
|
||
extension SourceRange { | ||
func contains(location: SourceLocation) -> Bool { | ||
return start.isBefore(location: location) && end.isAfter(location: location) | ||
} | ||
} | ||
|
||
extension SourceLocation { | ||
func isBefore(location: SourceLocation) -> Bool { | ||
guard line != location.line else { | ||
return column < location.column | ||
} | ||
return line < location.line | ||
} | ||
|
||
func isAfter(location: SourceLocation) -> Bool { | ||
return !isBefore(location: location) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters