Skip to content

Commit

Permalink
Consider semicolons when finding comments
Browse files Browse the repository at this point in the history
Fixes #70
NeQuissimus committed Jun 11, 2020
1 parent 3f4319a commit 5c4e4cc
Showing 5 changed files with 55 additions and 13 deletions.
14 changes: 14 additions & 0 deletions input/src/main/scala/fix/javastylewithcomments.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rule = SortImports
SortImports.blocks = [
"java",
"scala"
]
*/
import java.math.BigInteger;
import java.util.Map; // Map interface
import java.util.HashSet;

object JavaStyleWithComments {
// Add code that needs fixing here.
}
7 changes: 7 additions & 0 deletions output/src/main/scala/fix/javastylewithcomments.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Map; // Map interface

object JavaStyleWithComments {
// Add code that needs fixing here.
}
31 changes: 19 additions & 12 deletions rules/src/main/scala/fix/ImportGroup.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fix

import scala.collection.mutable.ListBuffer
import scala.meta.contrib.AssociatedComments
import scala.meta.contrib.{ AComments, AssociatedComments }
import scala.meta.inputs.Position
import scala.meta.tokens.Token
import scala.meta.{ Import, Tokens, Traverser, Tree }
@@ -42,23 +42,30 @@ case class ImportGroup(value: List[Import]) extends Iterable[Import] {
def trailedBy(pos: Position): Boolean =
pos.start == value.last.pos.end

def trailingComment(comments: AssociatedComments): Map[Import, Token.Comment] =
value
.map(currentImport => currentImport -> comments.trailing(currentImport).headOption)
.collect {
case (imp, comment) if comment.nonEmpty => (imp, comment.get)
}
.toMap
def trailingComment(tokens: Tokens, comments: AssociatedComments): Map[Import, Token.Comment] = {
val trailingMap = AComments.trailingMap(comments)

value.flatMap { currentImport =>
val sc = ImportGroup.semicolons(tokens, currentImport)
val cs: IndexedSeq[List[Token.Comment]] = sc.flatMap(s => trailingMap.get(s))

(currentImport -> comments.trailing(currentImport).headOption) +: cs.map(c => currentImport -> c.headOption)
}.collect {
case (imp, Some(comment)) => (imp, comment)
}.toMap
}

def trailingSemicolon(tokens: Tokens): Set[Import] =
value.filter { imp =>
val semicolons = tokens.collect { case t: Token.Semicolon if imp.pos.end == t.start => t }
semicolons.nonEmpty
}.toSet
value.filter(imp => ImportGroup.semicolons(tokens, imp).nonEmpty).toSet

override def isEmpty: Boolean = value.isEmpty

override def foreach[U](f: Import => U): Unit = value.foreach(f)

override def iterator: Iterator[scala.meta.Import] = value.iterator
}

object ImportGroup {
def semicolons(tokens: Tokens, imp: Import) =
tokens.collect { case t: Token.Semicolon if imp.pos.end == t.start => t }
}
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/SortImports.scala
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ class SortImports(config: SortImportsConfig) extends SyntacticRule("SortImports"
val importGroups: List[ImportGroup] = importGroupsWithEmptyLines.filter(_.nonEmpty)

// Trailing comments
val comments: Map[Import, Comment] = ImportGroup(importGroups.flatten).trailingComment(doc.comments)
val comments: Map[Import, Comment] = ImportGroup(importGroups.flatten).trailingComment(doc.tokens, doc.comments)

// Remove all newlines within import groups
val removeLinesPatch: List[Patch] = importGroups.flatMap { importGroup =>
14 changes: 14 additions & 0 deletions rules/src/main/scala/fix/treetokens.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scala
package meta
package contrib

import Token.Comment

object AComments {
def trailingMap(ac: AssociatedComments): Map[Token, List[Comment]] = {
val f = classOf[AssociatedComments].getDeclaredField("trailingMap")
f.setAccessible(true)

f.get(ac).asInstanceOf[Map[Token, List[Comment]]]
}
}

0 comments on commit 5c4e4cc

Please sign in to comment.