Skip to content

Commit 5ab1daf

Browse files
committed
fix: inline value when rhs starts in the same line as =
1 parent 9e782aa commit 5ab1daf

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ final class PcInlineValueProvider(
128128
end for
129129
end defAndRefs
130130

131-
private def stripIndentPrefix(rhs: String, refIndent: String, defIndent: String): String =
131+
private def stripIndentPrefix(rhs: String, refIndent: String, defIndent: String, hasNextLineAfterEqualsSign: Boolean): String =
132132
val rhsLines = rhs.split("\n").toList
133133
rhsLines match
134134
case h :: Nil => rhs
135135
case h :: t =>
136-
val header = if h.startsWith("{") then h else "\n" ++ refIndent ++ " " ++ h
136+
val header = if !hasNextLineAfterEqualsSign then h else "\n" ++ refIndent ++ " " ++ h
137137
header ++ t.map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n", "\n", "")
138138
case Nil => rhs
139139

@@ -255,14 +255,17 @@ final class PcInlineValueProvider(
255255
case _ => false
256256
}
257257
.map(_.fullNameBackticked)
258+
val hasNextLineAfterEqualsSign =
259+
definition.tree.sourcePos.startLine != definition.tree.rhs.sourcePos.startLine
258260
if conflictingSymbols.isEmpty then
259261
Right(
260262
Reference(
261263
occurrence.pos.toLsp,
262264
stripIndentPrefix(
263265
extendWithSurroundingParens(definition.tree.rhs.sourcePos),
264266
occurrence.tree.startPos.startColumnIndentPadding,
265-
definition.tree.startPos.startColumnIndentPadding
267+
definition.tree.startPos.startColumnIndentPadding,
268+
hasNextLineAfterEqualsSign
266269
),
267270
occurrence.parent.map(p =>
268271
RangeOffset(p.sourcePos.start, p.sourcePos.end)

presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala

+25
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,31 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
473473
|""".stripMargin
474474
)
475475

476+
@Test def `no-new-line` =
477+
checkEdit(
478+
"""|object O {
479+
| val i: Option[Int] = ???
480+
| def foo = {
481+
| val newValue = i match
482+
| case Some(x) => x
483+
| case None => 0
484+
| def bar =
485+
| val xx = new<<V>>alue
486+
| }
487+
|}
488+
|""".stripMargin,
489+
"""|object O {
490+
| val i: Option[Int] = ???
491+
| def foo = {
492+
| def bar =
493+
| val xx = i match
494+
| case Some(x) => x
495+
| case None => 0
496+
| }
497+
|}
498+
|""".stripMargin
499+
)
500+
476501
def checkEdit(
477502
original: String,
478503
expected: String,

0 commit comments

Comments
 (0)