Created
June 16, 2016 16:36
-
-
Save olafurpg/7be8f1d5aa9d01853e9ce514838710cc to your computer and use it in GitHub Desktop.
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
object Test extends App { | |
import scala.meta._ | |
import scala.meta.tokens.Token.Comment | |
val tree = """ | |
|package io.buildo.baseexample | |
| | |
|package enums | |
| | |
|import ingredients.caseenum.annotations.enum | |
| | |
|// scalastyle:off number.of.types | |
|@enum trait CampingLocation { | |
| // Foobar | |
| object Seaside // trailing | |
| object Mountains | |
|} | |
""".stripMargin.parse[Source].get | |
case class AssociatedComments(leadingMap: Map[Token, Seq[Comment]], | |
trailingMap: Map[Token, Seq[Comment]]) { | |
def leading(tree: Tree): Seq[Comment] = | |
(for { | |
token <- tree.tokens.headOption | |
comments <- leadingMap.get(token) | |
} yield comments).getOrElse(Seq.empty[Comment]) | |
def trailing(tree: Tree): Seq[Comment] = | |
(for { | |
token <- tree.tokens.lastOption | |
comments <- trailingMap.get(token) | |
} yield comments).getOrElse(Seq.empty[Comment]) | |
def contains(tree: Tree) = | |
trailing(tree).nonEmpty || leading(tree).nonEmpty | |
} | |
def getAssociatedComments(tokens: Tokens): AssociatedComments = { | |
import scala.meta.tokens.Token._ | |
val leadingBuilder = Map.newBuilder[Token, Seq[Comment]] | |
val trailingBuilder = Map.newBuilder[Token, Seq[Comment]] | |
val leading = Seq.newBuilder[Comment] | |
val trailing = Seq.newBuilder[Comment] | |
var isLeading = true | |
var lastToken: Token = tokens.head | |
tokens.foreach { | |
case c: Comment => | |
if (isLeading) leading += c | |
else trailing += c | |
case _: `\n` => isLeading = true | |
case _: Trivia => | |
case currentToken => | |
val t = trailing.result() | |
if (t.nonEmpty) { | |
trailingBuilder += lastToken -> trailing.result() | |
trailing.clear() | |
} | |
val l = leading.result() | |
if (l.nonEmpty) { | |
leadingBuilder += currentToken -> leading.result() | |
leading.clear() | |
} | |
lastToken = currentToken | |
isLeading = false | |
} | |
AssociatedComments(leadingBuilder.result(), trailingBuilder.result()) | |
} | |
val comments = getAssociatedComments(tree.tokens) | |
tree.transform { | |
case t: Tree if comments.contains(t) => | |
println(t, t.getClass, comments.leading(t), comments.trailing(t)) | |
t | |
} | |
println(comments.leadingMap) | |
println(comments.trailingMap) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: