Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better behaviour for AST selection with string interpolation

  • Loading branch information...
commit 4cb9d3c3fc2e8ffc2818670bf2d416bcf9eef5d0 1 parent 43e2d21
@mdr authored
View
24 scalariform/src/main/scala/scalariform/astselect/AstSelector.scala
@@ -68,6 +68,12 @@ class AstSelector(source: String, scalaVersion: String = ScalaVersions.DEFAULT_V
token.associatedWhitespaceAndComments.rawTokens :+ token
}
+ private def previousToken(token: Token): Option[Token] =
+ tokens.indexOf(token) match {
+ case 0 | -1 None
+ case n Some(tokens(n - 1))
+ }
+
def expandSelection(initialSelection: Range): Option[Range] =
expandToToken(initialSelection) orElse
expandScaladocToAssociatedNode(initialSelection) orElse
@@ -83,13 +89,22 @@ class AstSelector(source: String, scalaVersion: String = ScalaVersions.DEFAULT_V
nodeRange associatedNode.rangeOpt // <-- should always be defined here, in fact
} yield scaladocComment.range mergeWith nodeRange
+ private def isPreviousTokenStringPart(token: Token) =
+ previousToken(token).exists(token token.tokenType == Tokens.STRING_PART)
+
+ private def expandToStringInterpolationDollarIfPossible(token: Token): Range =
+ if (isPreviousTokenStringPart(token))
+ token.range.expandLeft(1)
+ else
+ token.range
+
/**
* If the selection is a strict subrange of some token, expand to the entire token.
*/
private def expandToToken(initialSelection: Range): Option[Range] =
- allTokens find { token
+ allTokens.find { token
isSelectableToken(token) && (token.range contains initialSelection) && initialSelection.length < token.length
- } map { _.range }
+ }.map(expandToStringInterpolationDollarIfPossible)
private def findAssociatedAstNode(scaladocCommentToken: Token): Option[AstNode] =
compilationUnitOpt.flatMap { cu findAssociatedAstNode(cu, scaladocCommentToken) }
@@ -142,7 +157,10 @@ class AstSelector(source: String, scalaVersion: String = ScalaVersions.DEFAULT_V
} return Some(descendantRange)
if (nodeRange.strictlyContains(initialSelection) && isSelectableAst(node :: enclosingNodes))
- Some(nodeRange)
+ if (isPreviousTokenStringPart(node.firstToken)) // Grab $ from string interpolation
+ Some(nodeRange.expandLeft(1))
+ else
+ Some(nodeRange)
else
None
View
2  scalariform/src/main/scala/scalariform/utils/Range.scala
@@ -19,4 +19,6 @@ case class Range(offset: Int, length: Int) {
def intersects(other: Range) =
!(other.offset >= offset + length || other.offset + other.length - 1 < offset)
+ def expandLeft(n: Int): Range = Range(offset - n, length + n)
+
}
View
7 scalariform/src/test/scala/scalariform/astselect/AstSelectorTest.scala
@@ -288,7 +288,7 @@ class AstSelectorTest extends FlatSpec with ShouldMatchers {
""" s"my name is ${person.name}." """ ~
""" $$$$$$ " """ ~
""" $$$$$$$$$$$ " """ ~
- """ $$$$$$$$$$$$$ " """ ~
+ """ $$$$$$$$$$$$$$ " """ ~
""" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$ """
""" xyz"" """ ~
@@ -299,6 +299,11 @@ class AstSelectorTest extends FlatSpec with ShouldMatchers {
""" s"my name is $bob" """ ~
""" $ """ ~
""" $$$$$$$$$$$$$ """
+
+ """ s"my name is $bob." """ ~
+ """ $ """ ~
+ """ $$$$ """
+
}

0 comments on commit 4cb9d3c

Please sign in to comment.
Something went wrong with that request. Please try again.