Skip to content

Commit

Permalink
Fix extracting values for fewer braces
Browse files Browse the repository at this point in the history
  • Loading branch information
majk-p authored and tgodzik committed Jan 31, 2025
1 parent 3349fa2 commit 1f7cae4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class ExtractValueCodeAction(
term <- allTrees
names = MetalsNames(term, "newValue")
stats <- lastEnclosingStatsList(term)
argument <- findRangeEnclosing(term, range)
extractedValue <- findRangeEnclosing(term, range)
// avoid extracting lambdas (this needs actual type information)
if isNotLambda(argument)
if isNotLambda(extractedValue)
stat <- stats.find(stat => stat.pos.encloses(term.pos))
name = names.createNewName()
source <- buffers.get(path)
Expand All @@ -65,13 +65,17 @@ class ExtractValueCodeAction(
val replacementText =
term match {
case apply: Term.Apply
if argument.is[Term.Block] && !applyHasParens(apply) =>
if extractedValue.is[Term.Block] && !applyHasParens(apply) =>
s"($name)"
case _ => name
}
val valueText = s"$keyword$name = ${argument.toString()}"

val redactedValue = ExtractValueCodeAction.removeFewerBracesBlock(
extractedValue.toString()
)
val valueText = s"$keyword$name = ${redactedValue}"
val replacedArgument =
new l.TextEdit(argument.pos.toLsp, replacementText)
new l.TextEdit(extractedValue.pos.toLsp, replacementText)
// we will insert `val newValue = ???` before the existing statement containing apply
(
withInsertNewValueDef(
Expand All @@ -81,7 +85,7 @@ class ExtractValueCodeAction(
blank,
replacedArgument,
),
argument.toString(),
extractedValue.toString(),
)
}

Expand All @@ -94,6 +98,7 @@ class ExtractValueCodeAction(
}

}

private def applyArgument(argument: Term): Term =
argument match {
// named parameter
Expand Down Expand Up @@ -350,9 +355,14 @@ class ExtractValueCodeAction(

object ExtractValueCodeAction {
def title(expr: String): String = {
val trimmed = expr.trim.stripPrefix("{").stripSuffix("}").trim()
val trimmed = removeFewerBracesBlock.andThen(stripBraces)(expr.trim)
if (trimmed.length <= 10) s"Extract `$trimmed` as value"
else s"Extract `${trimmed.take(10)}` ... as value"
}

private def removeFewerBracesBlock(extractedValue: String): String =
extractedValue.toString().dropWhile(_ == ' ').stripPrefix(":")

private def stripBraces(input: String): String =
input.stripPrefix("{").stripSuffix("}").trim()
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,39 @@ class ExtractValueLspSuite
| )
|}""".stripMargin,
)
check(
"extract-value-fewer-braces",
"""|import scala.util.Try
|
|def main =
| val x = Try:
| So<<m>>e(new Exception)""".stripMargin,
s"""${ExtractValueCodeAction.title("Some(new E` ... as value")}""",
"""|import scala.util.Try
|
|def main =
| val newValue =
| Some(new Exception)
| val x = Try(newValue)""".stripMargin,
scalaVersion = "3.3.4",
)
check(
"extract-value-fewer-braces-multiline",
"""|import scala.util.Try
|
|def main =
| val x = Try:
| println("hello")
| So<<m>>e(new Exception)""".stripMargin,
s"""${ExtractValueCodeAction.title("println(\"h` ... as value")}""",
"""|import scala.util.Try
|
|def main =
| val newValue =
| println("hello")
| Some(new Exception)
| val x = Try(newValue)""".stripMargin,
scalaVersion = "3.3.4",
)

}

0 comments on commit 1f7cae4

Please sign in to comment.