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

Fix extracting values for fewer braces #7164

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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",
)

}
Loading