Permalink
Browse files

More redundant semi stuff

  • Loading branch information...
1 parent 713d5c3 commit f412008c96f235736ce27e90323daede064ec50e @mdr mdr committed Feb 14, 2012
@@ -2,6 +2,8 @@ package scalariform.lexer
import scalariform.utils.Range
import scalariform.utils.Utils._
+import scalariform.utils.TextEdit
+import scalariform.utils.TextEditProcessor
object RedundantSemicolonDetector extends App {
@@ -13,17 +15,23 @@ object RedundantSemicolonDetector extends App {
def isRedundant(semi: Token, index: Int): Boolean = {
val sourceWithoutSemi = deleteRange(source, semi.range)
- val tokensWithoutSemi = ScalaLexer.tokenise(sourceWithoutSemi)
+ val tokensWithoutSemi = ScalaLexer.tokenise(sourceWithoutSemi, forgiveErrors = true)
val replacementToken = tokensWithoutSemi(index)
replacementToken.isNewline || replacementToken.tokenType == Tokens.EOF || replacementToken.tokenType == Tokens.RBRACE
}
- ScalaLexer.tokenise(source).zipWithIndex.collect {
+ ScalaLexer.tokenise(source, forgiveErrors = true).zipWithIndex.collect {
case (token, index) if token.tokenType == Tokens.SEMI && isRedundant(token, index) token
}
}
+ def removeRedundantSemis(s: String): String =
+ TextEditProcessor.runEdits(s, getEditsToRemoveRedundantSemis(s))
+
+ def getEditsToRemoveRedundantSemis(s: String): List[TextEdit] =
+ findRedundantSemis(s).map(_.range).map(TextEdit.delete)
+
}
object Demo extends App {
@@ -35,8 +43,10 @@ object Demo extends App {
};"""
val redundantSemis = RedundantSemicolonDetector.findRedundantSemis(source)
val annotated = redundantSemis.reverse.foldLeft(source) { (s, semi) replaceRange(s, semi.range, "<;>") }
+ val purged = RedundantSemicolonDetector.removeRedundantSemis(source)
println(source)
println("-------------------------------------------------")
println(annotated)
-
+ println("-------------------------------------------------")
+ println(purged)
}
@@ -1,8 +1,19 @@
package scalariform.utils
+object TextEdit {
+
+ def delete(range: Range): TextEdit = delete(range.offset, range.length)
+
+ def delete(position: Int, length: Int): TextEdit = TextEdit(position = position, length = length, replacement = "")
+
+}
+
case class TextEdit(position: Int, length: Int, replacement: String) {
+
require(position >= 0, "position must be positive: " + position)
+
require(length >= 0)
+
override lazy val toString = {
val replacementDisplay = replacement.replace("\n", """\n""").replace("\r", """\r""")
getClass.getSimpleName + "(position = " + position + ", length = " + length + ", replacement = '" + replacementDisplay + "')"
@@ -11,29 +11,29 @@ import scalariform.utils.Utils._
class RedundantSemicolonDetectorTest extends FlatSpec with ShouldMatchers {
implicit def stringToCheckable(s: String) = new { def check = checkSemis(s) } // Expected redundant semicolons are indicated with <;>
- ;
+;
"""
class A {
def foo = 42<;>
def bar = 123; def baz = 1234
}<;>
- """.check;
-
+ """.check
+;
"""
{
println("Foo")<;>
}
- """.check;
-
+ """.check
+;
"""
class A {
for (
x <- 1 to 10;
y <- 1 to 10
) yield x + y<;>
}
- """.check;
-
+ """.check
+;
private def checkSemis(encodedSource: String) {
val ordinarySource = encodedSource.replaceAllLiterally("<;>", ";")
val semis = RedundantSemicolonDetector.findRedundantSemis(ordinarySource)

0 comments on commit f412008

Please sign in to comment.