scalacenter / scala-syntax

Scalameta pretty printer for better Scalafix refactorings
Apache License 2.0
17 stars 6 forks source link

Comments property #31

Closed MasseGuillaume closed 6 years ago

MasseGuillaume commented 6 years ago

We want to add support for comments. Our current strategy is the following:

Find the associated comment from the tree root. This gives us a way to retrieve leading and trailing comments from a Token. When we print the tree, recursively (in TreePrinter), we query tokens from the tree. For example for if:

case t: Term.If =>
  def body(expr: Term) = expr match {
    case b: Term.Block => space + dBlock(b.stats)
    case _: Term.If => space + print(expr)
    case _ => (line + print(expr)).nested(2)
  }
  val delse =
    if (t.elsep.tokens.isEmpty) empty
    else line + t.`else` + body(t.elsep)

  (
    t.`if` + space + t.`(` + print(t.cond) + t.`)` +
    body(t.thenp) +
    delse

  ).grouped
olafurpg commented 6 years ago

The tokens* prefix convention is up for debate, I think it has some nice benefits

MasseGuillaume commented 6 years ago

@olafurpg I still have a long way to go. I would say 25%.

I'm doing every case of CoverageSuite.

codecov[bot] commented 6 years ago

Codecov Report

Merging #31 into master will decrease coverage by 3.75%. The diff coverage is 63.27%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
- Coverage   86.22%   82.46%   -3.76%     
==========================================
  Files          15       17       +2     
  Lines         813      964     +151     
  Branches       33       45      +12     
==========================================
+ Hits          701      795      +94     
- Misses        112      169      +57
Impacted Files Coverage Δ
...cala/meta/internal/format/AssociatedComments.scala 0% <0%> (ø)
...main/scala/org/scalafmt/internal/TreePrinter.scala 100% <100%> (ø) :arrow_up:
.../main/scala/org/scalafmt/internal/TreeDocOps.scala 99.23% <100%> (ø) :arrow_up:
...in/scala/scala/meta/internal/format/Comments.scala 32.14% <19.04%> (-61.98%) :arrow_down:
...cala/org/scalafmt/tests/BaseScalaPrinterTest.scala 69.73% <47.05%> (-0.54%) :arrow_down:
...ain/scala/org/scalafmt/internal/SyntaxTokens.scala 98% <98%> (ø)
format/src/main/scala/org/scalafmt/Format.scala 5.88% <0%> (+5.88%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 84457fd...c294054. Read the comment docs.