Comments
Single line comments
Single line comments are added to an arbitrary tree as follows:
tree withComment("comments here", ...)
For example,
import treehugger.forest._, definitions._, treehuggerDSL._
val tree = LIT(2) withComment("comments are useful",
"only if they provide more info than the code")
// tree: Commented = Commented(
// Modifiers(0L, TypeName(""), List()),
// List("comments are useful", "only if they provide more info than the code"),
// Literal(Constant(2))
// )
treeToString(tree)
// res0: String = """// comments are useful
// // only if they provide more info than the code
// 2"""
Scaladoc style comments
Scaladoc style comments are written using withDoc as follows:
tree withDoc("comments" | doctag, ...)
where doctag is a doctag defined as DocTag.See(IntClass), DocTag.Author("Somebody"), etc.
For example,
val tree2 = (DEF("x") := LIT(0)) withDoc(
"does something",
DocTag.See(IntClass))
// tree2: Commented = Commented(
// Modifiers(1L, TypeName(""), List()),
// List("does something", "@see scala.Int"),
// DefDef(
// Modifiers(0L, TypeName(""), List()),
// TermName("x"),
// List(),
// List(),
// TypeTree(),
// Literal(Constant(0))
// )
// )
treeToString(tree2)
// res1: String = """/**
// * does something
// * @see scala.Int
// */
// def x = 0"""