Skip to content

Commit 48e8328

Browse files
kasiaMareksusuro
andauthored
pc: fix: inline value when def indentation equals 2 (#22990)
Co-authored-by: Robert Marek <[email protected]>
1 parent 08f2e4a commit 48e8328

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,13 @@ 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 noPrefixH = h.stripPrefix(refIndent)
137-
if noPrefixH.startsWith("{") then
138-
noPrefixH ++ t.map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n","\n", "")
139-
else
140-
((" " ++ h) :: t).map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n", "\n", "")
136+
val header = if !hasNextLineAfterEqualsSign then h else "\n" ++ refIndent ++ " " ++ h
137+
header ++ t.map(refIndent ++ _.stripPrefix(defIndent)).mkString("\n", "\n", "")
141138
case Nil => rhs
142139

143140
private def definitionRequiresBrackets(tree: Tree)(using Context): Boolean =
@@ -236,7 +233,7 @@ final class PcInlineValueProvider(
236233
var idx = source.startOfLine(offset)
237234
val pad = new StringBuilder
238235
while (idx != offset && idx < source.content().length && source.content()(idx).isWhitespace) {
239-
pad.append(if (idx < source.content().length && source.content()(idx) == '\t') '\t' else ' ')
236+
pad.append(source.content()(idx))
240237
idx += 1
241238
}
242239
pad.result()
@@ -262,14 +259,17 @@ final class PcInlineValueProvider(
262259
case _ => false
263260
}
264261
.map(_.fullNameBackticked)
262+
val hasNextLineAfterEqualsSign =
263+
definition.tree.sourcePos.startLine != definition.tree.rhs.sourcePos.startLine
265264
if conflictingSymbols.isEmpty then
266265
Right(
267266
Reference(
268267
occurrence.pos.toLsp,
269268
stripIndentPrefix(
270269
extendWithSurroundingParens(definition.tree.rhs.sourcePos),
271270
occurrence.tree.startPos.startColumnIndentPadding,
272-
definition.tree.startPos.startColumnIndentPadding
271+
definition.tree.startPos.startColumnIndentPadding,
272+
hasNextLineAfterEqualsSign
273273
),
274274
occurrence.parent.map(p =>
275275
RangeOffset(p.sourcePos.start, p.sourcePos.end)

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

+44
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,25 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
430430
)
431431

432432
@Test def `i7137a` =
433+
checkEdit(
434+
"""|def foo = {
435+
| val newValue =
436+
| val x = true
437+
| x
438+
| def bar =
439+
| val xx =new<<V>>alue
440+
|}
441+
|""".stripMargin,
442+
"""|def foo = {
443+
| def bar =
444+
| val xx =
445+
| val x = true
446+
| x
447+
|}
448+
|""".stripMargin
449+
)
450+
451+
@Test def `i7137b` =
433452
checkEdit(
434453
"""|object O {
435454
| def foo = {
@@ -454,6 +473,31 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
454473
|""".stripMargin
455474
)
456475

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+
457501
def checkEdit(
458502
original: String,
459503
expected: String,

0 commit comments

Comments
 (0)