From 9e782aa7b333c9ac75743dd2dd95f267e80f8603 Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Sat, 12 Apr 2025 21:15:32 +0200 Subject: [PATCH 1/2] fix: inline value when def indentation equals 2 Co-Authored-By: Robert Marek <64277069+susuro@users.noreply.github.com> --- .../tools/pc/PcInlineValueProvider.scala | 9 +++------ .../pc/tests/edit/InlineValueSuite.scala | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala index eafac513c7e1..816a016fa027 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala @@ -133,11 +133,8 @@ final class PcInlineValueProvider( 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 h.startsWith("{") 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() 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..e53b015d0d86 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 = { From 5ab1daf8dbdb97cf029dc6c7bcdb7440d2ee1f60 Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Mon, 28 Apr 2025 13:33:13 +0200 Subject: [PATCH 2/2] fix: inline value when rhs starts in the same line as `=` --- .../tools/pc/PcInlineValueProvider.scala | 9 ++++--- .../pc/tests/edit/InlineValueSuite.scala | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala index 816a016fa027..6fd2ecd58a02 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProvider.scala @@ -128,12 +128,12 @@ 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 header = if h.startsWith("{") then h else "\n" ++ refIndent ++ " " ++ h + val header = if !hasNextLineAfterEqualsSign then h else "\n" ++ refIndent ++ " " ++ h header ++ t.map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n", "\n", "") case Nil => rhs @@ -255,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( @@ -262,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 e53b015d0d86..74af1d1c2a08 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala @@ -473,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,