diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala index eafac513c7e1..6fd2ecd58a02 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala @@ -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 = @@ -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() @@ -258,6 +255,8 @@ final class PcInlineValueProvider( case _ => false } .map(_.fullNameBackticked) + val hasNextLineAfterEqualsSign = + definition.tree.sourcePos.startLine != definition.tree.rhs.sourcePos.startLine if conflictingSymbols.isEmpty then Right( Reference( @@ -265,7 +264,8 @@ final class PcInlineValueProvider( 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) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala index 541340bd3f38..74af1d1c2a08 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala @@ -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<>alue + |} + |""".stripMargin, + """|def foo = { + | def bar = + | val xx = + | val x = true + | x + |} + |""".stripMargin + ) + + @Test def `i7137b` = checkEdit( """|object O { | def foo = { @@ -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<>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,