Skip to content
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

Formatter inserts unexpected whitespace in if with comments after macro #14767

Open
homonoidian opened this issue Jun 29, 2024 · 2 comments
Open

Comments

@homonoidian
Copy link
Contributor

Bug Report

This:

macro x

end

if true    #
  a = "xy" #
end

... formats to this:

macro x
end

if true #
  a = "x   y" #
end

Macro with leading/trailing newlines. Code could be between the macro and ifs, in my case that was several hundred lines:

macro x

end

dofoo(1)
foodo(2)

if true    #
  a = "xy" #
end

... becomes:

macro x
end

dofoo(1)
foodo(2)

if true #
  a = "x   y" #
end

Similarly with multiple ifs:

macro x

end

if n0 #
  a = "xy" #
end

if n01 #
  a = "xy" #
end

if n012 #
  a = "xy" #
end

if n0123 #
  a = "xy" #
end

if n01234 #
  a = "xy" #
end

if n012345 #
  a = "xy" #
end

... formats to:

macro x
end

if n0 #
  a =      "xy" #
end

if n01 #
  a = "    xy" #
end

if n012 #
  a = "x   y" #
end

if n0123 #
  a = "xy  " #
end

if n01234 #
  a = "xy"  #
end

if n012345 #
  a = "xy" #
end

There's also this:

macro x
  
end

if 1 #
  foo #
  xyzzy = "hello world" #
end

... which formats to:

macro x
end

if 1 #
  foo                    #
  xyzz                  y = "hello world" #
end

... and so on.

kamil-gwozdz added a commit to kamil-gwozdz/crystal that referenced this issue Jul 21, 2024
kamil-gwozdz added a commit to kamil-gwozdz/crystal that referenced this issue Jul 21, 2024
@kamil-gwozdz
Copy link

kamil-gwozdz commented Jul 21, 2024

I wrote a failing spec for it: kamil-gwozdz@bdc5756

I think the problem is that lines and @comment_columns are missmatched here:

max_column = nil
lines.each_with_index do |line, i|
comment_column = @comment_columns[i]?
if comment_column
if max_column
lines[i] = align_comment line, i, comment_column, max_column
else
max_column = find_max_column(lines, i + 1, comment_column)
lines[i] = align_comment line, i, comment_column, max_column
end
else
max_column = nil
end
end

# lines
["macro x", "end", "", "if true #", "  a = \"xy\" #", "end"] # 6 elements
# @comment_columns
[nil, nil, nil, nil, 8, 11, nil, nil] # 8 elements

I guess it's due to how macros are handled.

@kamil-gwozdz
Copy link

It seems like empty lines were removed from lines but they are still referenced in @comment_columns.

kamil-gwozdz added a commit to kamil-gwozdz/crystal that referenced this issue Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants