Created
May 10, 2019 12:40
-
-
Save witzatom/ee89e88c646fdf141c98956d46962caa to your computer and use it in GitHub Desktop.
MoveDotsToStartsOfLines
This file contains hidden or 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
| 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