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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

Although, I think that the h.startsWith("{") is wrong too 🙈
The else branch should most likely only be performed if the start line number of the rhs is different than the end line number of the definition.

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 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 Down
Loading