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

Don't indent correct for chaining if add comment at end of line. #41

Open
zw963 opened this issue May 8, 2022 · 2 comments
Open

Don't indent correct for chaining if add comment at end of line. #41

zw963 opened this issue May 8, 2022 · 2 comments
Labels

Comments

@zw963
Copy link
Contributor

zw963 commented May 8, 2022

e.g. following code when indent with emacs builtin ruby-mode (without LSP enabled), it will indent like this:

# test.rb, it correct.
result = (42..47).to_a                          # (1) => [42, 43, 44, 45, 46, 47]
           .sort { |n, m| m <=> n }             # (2) => [47, 46, 45, 44, 43, 42]
           .reject { |n| n.odd? }               # (3) => [46, 44, 42]
           .map { |n| n * n }                   # (4) => [2116, 1936, 1764]
           .select { |n| n % 4 == 0 }           # (5) => [2116, 1936, 1764]
           .tap { |arr| puts "#{arr.inspect}" } # (6) => [2116, 1936, 1764]
           .sort!                               # (7) => [1764, 1936, 2116]
           .any? { |num| num > 2000 }           # (8) => true

but, if switch to crystal-mode, it indent like this:

# test1.cr,  indent not correct

result = (42..47).to_a                          # (1) => [42, 43, 44, 45, 46, 47]
  .sort { |n, m| m <=> n }             # (2) => [47, 46, 45, 44, 43, 42]
    .reject { |n| n.odd? }               # (3) => [46, 44, 42]
      .map { |n| n * n }                   # (4) => [2116, 1936, 1764]
        .select { |n| n % 4 == 0 }           # (5) => [2116, 1936, 1764]
          .tap { |arr| puts "#{arr.inspect}" } # (6) => [2116, 1936, 1764]
            .sort!                               # (7) => [1764, 1936, 2116]
              .any? { |num| num > 2000 }           # (8) => true

But, if remove the those comment from line ending, it indent correct again.

# test2.cr, indent correct.
result = (42..47).to_a
         .sort { |n, m| m <=> n }
         .reject { |n| n.odd? }
         .map { |n| n * n }
         .select { |n| n % 4 == 0 }
         .tap { |arr| puts "#{arr.inspect}" }
         .sort!
         .any? { |num| num > 2000 }
@yxhuvud
Copy link
Contributor

yxhuvud commented May 8, 2022

Neither of those indents are correct if we go by what the result of crystal format is. Then the correct is

result = (42..47).to_a                 # (1) => [42, 43, 44, 45, 46, 47]
  .sort { |n, m| m <=> n }             # (2) => [47, 46, 45, 44, 43, 42]
  .reject { |n| n.odd? }               # (3) => [46, 44, 42]
  .map { |n| n * n }                   # (4) => [2116, 1936, 1764]
  .select { |n| n % 4 == 0 }           # (5) => [2116, 1936, 1764]
  .tap { |arr| puts "#{arr.inspect}" } # (6) => [2116, 1936, 1764]
  .sort!                               # (7) => [1764, 1936, 2116]
  .any? { |num| num > 2000 }           # (8) => true

There is a function in this mode to apply it to the current file. It could perhaps be used so that we don't have to reinvent the wheel, but there are two problems with that. First is that it affects the whole file, not just the current position, and the second is that it doesn't work at all if the file doesn't currently have valid syntax. These together make it suitable to format existing code, but not to format code as it is written.

@zw963
Copy link
Contributor Author

zw963 commented May 9, 2022

and the second is that it doesn't work at all if the file doesn't currently have valid syntax

It seem like most of emacs programming mode is this same behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants