Skip to content

Add support for preserve_single_line_if::Bool#937

Open
alexandergunnarson wants to merge 5 commits into
JuliaEditorSupport:masterfrom
alexandergunnarson:patch-1
Open

Add support for preserve_single_line_if::Bool#937
alexandergunnarson wants to merge 5 commits into
JuliaEditorSupport:masterfrom
alexandergunnarson:patch-1

Conversation

@alexandergunnarson
Copy link
Copy Markdown

@alexandergunnarson alexandergunnarson commented Aug 8, 2025

Before formatting (or with preserve_single_line_if = true):

function f()
    if rand() < 0.5 return 2 end
    0
end

After formatting with preserve_single_line_if = false (default):

function f()
    if rand() < 0.5
        return 2
    end
    0
end

Resolves #516.

@alexandergunnarson alexandergunnarson changed the title Add support for preserve_single_line_if = x::Bool Add support for preserve_single_line_if = ::Bool Aug 8, 2025
@alexandergunnarson alexandergunnarson changed the title Add support for preserve_single_line_if = ::Bool Add support for preserve_single_line_if::Bool Aug 8, 2025
Copy link
Copy Markdown
Collaborator

@domluna domluna left a comment

Choose a reason for hiding this comment

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

please add some test cases

looks good otherwise.

Comment thread src/options.jl Outdated
@penelopeysm
Copy link
Copy Markdown
Member

This patch no longer works on the current version:

julia> using JuliaFormatter: format_text

julia> s = "if rand() < 0.5 return 2 end"
"if rand() < 0.5 return 2 end"

julia> format_text(s; preserve_single_line_if=true) |> println
if rand() < 0.5
    return 2
end

@alexandergunnarson Are you still interested in working on this? No worries if not as I can pick it up (at some point in time....).

- Fix the preserve_single_line_if option which was broken after merging
  with master. The previous approach only controlled ignore_single_line
  on the block context, but didn't handle the add_node! calls that
  force newlines via max_padding.

- Now properly checks if the entire if statement is on a single line
  using on_same_line(), and when preserve_single_line=true:
  - Adds block content with join_lines=true instead of max_padding
  - Adds else/elseif/end keywords with join_lines=true
  - Inserts explicit Whitespace(1) between elements
  - Skips indent adjustments (not needed for single-line)

- Add comprehensive test cases covering:
  - Basic single-line if
  - Default behavior (expand single-line if)
  - Multi-line if unaffected by the option
  - Single-line if-else
  - Single-line if-elseif-else
  - Multi-line if-else unaffected
  - Nested single-line if
  - Single-line if inside a function
  - Expansion when option is false
@alexandergunnarson
Copy link
Copy Markdown
Author

alexandergunnarson commented May 28, 2026

@domluna Sorry — added test cases in the most recent commit.

@penelopeysm — Thanks for alerting me! Patch now works with latest version.

@penelopeysm
Copy link
Copy Markdown
Member

penelopeysm commented May 28, 2026

Thanks 😊 This looks generally great. I have one question about what the intended behaviour should be when overflowing the max width. Currently we have this:

julia> using JuliaFormatter: format_text

julia> format_text("if x > 0 return 1 end"; preserve_single_line_if=true, margin=7) |> println
if x >
   0 return 1 end

julia> format_text("if x > 0 return 1 end"; preserve_single_line_if=true, margin=8) |> println
if x > 0 return 1 end

The issue is that the x > 0 gets nested if it goes over the margin. While the result parses fine, I think it's a bit unprincipled for the if-condition to determine whether there's a line break. I think there are two possible options for preserve_single_line_if = true:

  1. If the entire if ... end exceeds the margin, then we split it up.

  2. Never split it up.

I would lean towards (2) Actually, I'm kind of undecided :/ -- what do you think?

end
elseif !JuliaSyntax.is_whitespace(c)
add_node!(t, Whitespace(1), s)
add_node!(t, pretty(style, c, s, ctx, lineage), s; join_lines = true)
Copy link
Copy Markdown
Member

@penelopeysm penelopeysm May 28, 2026

Choose a reason for hiding this comment

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

(If you agree with (2), then I believe this is the appropriate fix:)

Suggested change
add_node!(t, pretty(style, c, s, ctx, lineage), s; join_lines = true)
ctx2 = preserve_single_line_if ? newctx(ctx; nonest=true) : ctx
add_node!(t, pretty(style, c, s, ctx2, lineage), s; join_lines = true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(1) looks trickier to accomplish to me. I haven't attempted to figure out how that would be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow preserving style for single-line if expressions

3 participants