Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check the whole caret position in the testkit #1259

Merged
merged 4 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case class AssertDelta(
sameKey(assert.key) &&
(assert.caretPosition match {
case Some(carPos) =>
(carPos.start == lintDiagnostic.position.start) &&
carPos.lineCaret == lintDiagnostic.position.lineCaret &&
assert.expectedMessage.forall(_.trim == lintDiagnostic.message.trim)
case None =>
sameLine(assert.anchorPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import scalafix.internal.util.PositionSyntax._
// For example:
//
// ```scala
// Option(1).get // assert: Disable.get
// class Foo {
// def bar(x: Int = 42) = 1 // assert: DisableSyntax.defaultArgs
// }
// ```
//
// You can also use the multiline variant. This isuseful two visually
Expand All @@ -22,14 +24,13 @@ import scalafix.internal.util.PositionSyntax._
// For example:
//
// ```scala
// Option(1).get /* assert: Disable.get
// ^
// Option.get is the root of all evilz
//
// If you must Option.get, wrap the code block with
// // scalafix:off Option.get
// ...
// // scalafix:on Option.get
// class Foo {
// def bar(x: Int = 42) = 1 /* assert: DisableSyntax.defaultArgs
// ^^
// Default args makes it hard to use methods as functions.
// */
// }
// ```
// */
case class CommentAssertion(
anchorPosition: Position,
Expand Down Expand Up @@ -97,19 +98,21 @@ object MultiLineAssertExtractor {
)
}

val caretOffset = lines(1).indexOf('^')
if (caretOffset == -1)
val caretOffsetStart = lines(1).indexOf('^')
if (caretOffsetStart == -1)
throw new Exception("^ should be on the second line of the assert")
val offset = caretOffset - token.pos.startColumn
val caretStart = token.pos.start + offset
val caretOffsetEnd = lines(1).lastIndexOf('^')
val offsetStart = caretOffsetStart - token.pos.startColumn
val offsetEnd = caretOffsetEnd - token.pos.startColumn + 1
val caretStart = token.pos.start + offsetStart
val caretEnd = token.pos.start + offsetEnd
val message = lines.drop(2).mkString("\n")

Some(
CommentAssertion(
anchorPosition = token.pos,
key = key,
caretPosition = Some(
Position.Range(token.pos.input, caretStart, caretStart)
Position.Range(token.pos.input, caretStart, caretEnd)
),
expectedMessage = Some(message)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object DisableSyntaxMoreRules {

class Foo {
def bar(x: Int = 42) = 1 /* assert: DisableSyntax.defaultArgs
^
^^
Default args makes it hard to use methods as functions.
*/
}
Expand Down Expand Up @@ -58,11 +58,11 @@ Default args makes it hard to use methods as functions.

class Bar { type Foo = Int; def foo = 42 }
def foo(a: { type Foo = Int; def foo: Foo } = new Bar): Int = a.foo /* assert: DisableSyntax.defaultArgs
^
^^^^^^^
Default args makes it hard to use methods as functions.
*/
def foo2(a: { type Foo = Int; def foo: Foo } = {val a = new Bar; a}): Int = a.foo /* assert: DisableSyntax.defaultArgs
^
^^^^^^^^^^^^^^^^^^^^
Default args makes it hard to use methods as functions.
*/

Expand Down
2 changes: 1 addition & 1 deletion scalafix-tests/input/src/main/scala/test/NoFinalize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case object NoFinalize {

class Example {
override protected def finalize() = () /* assert: DisableSyntax.noFinalize
^
^^^^^^^^
finalize should not be used
*/
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ case object DisableSyntaxBase {


null /* assert: DisableSyntax.null
^
^^^^
null should be avoided, consider using Option instead
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ case object DisableSyntaxNoValPatterns {
val (works6, _) = (1, Left(42))
case class TestClass(a: Int, b: Int)
val TestClass(a, b) = TestClass(1, 1) /* assert: DisableSyntax.noValPatterns
^
^^^^^^^^^^^^^^^
Pattern matching in val assignment can result in match error, use "_ match { ... }" with a fallback case instead.*/
val TestClass(c, _) = TestClass(1, 1) /* assert: DisableSyntax.noValPatterns
^
^^^^^^^^^^^^^^^
Pattern matching in val assignment can result in match error, use "_ match { ... }" with a fallback case instead.*/
}