Skip to content

Instantly share code, notes, and snippets.

@witzatom
Created May 10, 2019 12:40
Show Gist options
  • Save witzatom/ee89e88c646fdf141c98956d46962caa to your computer and use it in GitHub Desktop.
Save witzatom/ee89e88c646fdf141c98956d46962caa to your computer and use it in GitHub Desktop.
MoveDotsToStartsOfLines
package fix
import scalafix.v1._
import scala.meta._
class MoveDotsToStartsOfLines extends SyntacticRule("MoveDotsToStartsOfLines") {
override def fix(implicit doc: SyntacticDocument): Patch = {
val (matches, _) = doc.tokens.foldLeft((Seq.empty[Seq[Token]], Seq.empty[Token])){
case ((matchingSequences: Seq[Seq[Token]], matchingSoFar: Seq[Token]), token: Token) =>
if(matchingSoFar.nonEmpty) {
token match {
case _: Token.Space => (matchingSequences, matchingSoFar :+ token)
case _: Token.CR => (matchingSequences, matchingSoFar :+ token)
case _: Token.LF => (matchingSequences, matchingSoFar :+ token)
case _: Token.Comment => (matchingSequences, matchingSoFar :+ token)
case _ =>
val matchesRule = matchingSoFar.count{
case _: Token.CR => true
case _: Token.LF => true
case _ => false
} > 0
if(matchesRule && matchingSoFar.lengthCompare(2) > 0)
(matchingSequences :+ (matchingSoFar :+ token), Seq.empty)
else
(matchingSequences, Seq.empty)
}
} else{
token match {
case _: Token.Dot => (matchingSequences, Seq(token))
case _ => (matchingSequences, matchingSoFar)
}
}
}
val patchSequence = matches.flatMap{ matchingSequence =>
val removeDot = Patch.removeToken(matchingSequence.head)
val addDot = Patch.addLeft(matchingSequence.last, ".")
Seq(removeDot, addDot)
}
Patch.fromIterable(patchSequence)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment