Skip to content

pc: fix: inline value when def indentation equals 2 #22990

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

Merged
merged 2 commits into from
Apr 29, 2025
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 @@ -128,16 +128,13 @@ final class PcInlineValueProvider(
end for
end defAndRefs

private def stripIndentPrefix(rhs: String, refIndent: String, defIndent: String): String =
private def stripIndentPrefix(rhs: String, refIndent: String, defIndent: String, hasNextLineAfterEqualsSign: Boolean): String =
val rhsLines = rhs.split("\n").toList
rhsLines match
case h :: Nil => rhs
case h :: t =>
val noPrefixH = h.stripPrefix(refIndent)
if noPrefixH.startsWith("{") then
noPrefixH ++ t.map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n","\n", "")
else
((" " ++ h) :: t).map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n", "\n", "")
val header = if !hasNextLineAfterEqualsSign then h else "\n" ++ refIndent ++ " " ++ h
header ++ t.map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n", "\n", "")
case Nil => rhs

private def definitionRequiresBrackets(tree: Tree)(using Context): Boolean =
Expand Down Expand Up @@ -232,7 +229,7 @@ final class PcInlineValueProvider(
var idx = source.startOfLine(offset)
val pad = new StringBuilder
while (idx != offset && idx < source.content().length && source.content()(idx).isWhitespace) {
pad.append(if (idx < source.content().length && source.content()(idx) == '\t') '\t' else ' ')
pad.append(source.content()(idx))
idx += 1
}
pad.result()
Expand All @@ -258,14 +255,17 @@ final class PcInlineValueProvider(
case _ => false
}
.map(_.fullNameBackticked)
val hasNextLineAfterEqualsSign =
definition.tree.sourcePos.startLine != definition.tree.rhs.sourcePos.startLine
if conflictingSymbols.isEmpty then
Right(
Reference(
occurrence.pos.toLsp,
stripIndentPrefix(
extendWithSurroundingParens(definition.tree.rhs.sourcePos),
occurrence.tree.startPos.startColumnIndentPadding,
definition.tree.startPos.startColumnIndentPadding
definition.tree.startPos.startColumnIndentPadding,
hasNextLineAfterEqualsSign
),
occurrence.parent.map(p =>
RangeOffset(p.sourcePos.start, p.sourcePos.end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,25 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
)

@Test def `i7137a` =
checkEdit(
"""|def foo = {
| val newValue =
| val x = true
| x
| def bar =
| val xx =new<<V>>alue
|}
|""".stripMargin,
"""|def foo = {
| def bar =
| val xx =
| val x = true
| x
|}
|""".stripMargin
)

@Test def `i7137b` =
checkEdit(
"""|object O {
| def foo = {
Expand All @@ -454,6 +473,31 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
|""".stripMargin
)

@Test def `no-new-line` =
checkEdit(
"""|object O {
| val i: Option[Int] = ???
| def foo = {
| val newValue = i match
| case Some(x) => x
| case None => 0
| def bar =
| val xx = new<<V>>alue
| }
|}
|""".stripMargin,
"""|object O {
| val i: Option[Int] = ???
| def foo = {
| def bar =
| val xx = i match
| case Some(x) => x
| case None => 0
| }
|}
|""".stripMargin
)

def checkEdit(
original: String,
expected: String,
Expand Down
Loading