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

Format: correctly indent block of indented method chain #5195

Conversation

makenowjust
Copy link
Contributor

Fixed #5193

@oprypin
Copy link
Member

oprypin commented Oct 27, 2017

I'm not sure what you mean by "correctly". I think this is entirely invalid and looks atrocious.
I did not see any calls for action, rather the opposite from the core team.

In the end, it boils down to user preference, something that the formatter doesn't allow.

Disallowing such chaining would probably be best for readability.

Also here's a more widely accepted perception of "correct":
https://google.github.io/styleguide/cppguide.html#Function_Declarations_and_Definitions

@straight-shoota
Copy link
Member

I think this is much more readable. Just look at the reformatted code in src/compiler/crystal/tools/expand.cr. In the current version it is difficult to spot where each indentation level belongs to. This is better with this PR.

Obviously such pieces should be formatted differently to be really good, but this is one step in the right direction.

This would be much cleaner:

expansion
  .expanded_sources
  .zip(expansion.expanded_macros)
  .each_with_index do |(expanded_source, expanded_macro), j|		                                    
    expanded_macro.each do |a_macro|
      name = a_macro[:name]
      # ...
    end
    io << "~> "
  end

I'm not sure what you mean with such chaining exactly, but I'm pretty confident that it shouldn't be generally disallowed.

@asterite
Copy link
Member

I actually think it's less readable. Before everything fit in 80 columns, now you have to scroll right to see what's going on.

The current formatting is just fine.

I know, it's a personal opinion, and that's why we will never agree on this.

@RX14
Copy link
Contributor

RX14 commented Oct 27, 2017

I also think this is worse. And I'm also glad crystals code base only changes formatting in one file, it means we don't have much code that's messy in this way.

@Sija
Copy link
Contributor

Sija commented Oct 27, 2017

I know, it's a personal opinion, and that's why we will never agree on this.

@asterite let me ask you a question then - which version would you prefer to write/see?

expansion.expanded_sources.zip(expansion.expanded_macros)
                          .each_with_index do |(expanded_source, expanded_macro), j|
  expanded_macro.each do |a_macro|
    # ...
  end
end

or

expansion.expanded_sources
  .zip(expansion.expanded_macros)
  .each_with_index do |(expanded_source, expanded_macro), j|
    expanded_macro.each do |a_macro|
      # ...
    end
  end

What I'm heading at is placement of .zip(expansion.expanded_macros), which in this case was IMO too far out (pun intended) ;)

@@ -21,16 +21,16 @@ module Crystal
io.puts
expansion.expanded_sources.zip(expansion.expanded_macros)
.each_with_index do |(expanded_source, expanded_macro), j|
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should refactor this code in the future, but formatter is not wrong. I believe this indentation is correct and old indent is bad, as @straight-shoota and @Sija said.

@RX14
Copy link
Contributor

RX14 commented Oct 28, 2017

Thinking about it again, I think @Sija's right.

I think that

expansion.expanded_sources.zip(expansion.expanded_macros)
                          .each_with_index do |(expanded_source, expanded_macro), j|
  expanded_macro.each do |a_macro|
    # ...
  end
end

should format to

expansion.expanded_sources.zip(expansion.expanded_macros)
    .each_with_index do |(expanded_source, expanded_macro), j|
      expanded_macro.each do |a_macro|
        # ...
      end
    end

personally (4 spaces to indent a method chain broken onto multiple lines).

I'd then be encouraged to do this, which I believe to be my favourite formatting:

expansion.expanded_sources
    .zip(expansion.expanded_macros)
    .each_with_index do |(expanded_source, expanded_macro), j|
      expanded_macro.each do |a_macro|
        # ...
      end
    end

@makenowjust
Copy link
Contributor Author

I totally agree with @RX14's idea. Dot based alignment is painful. (and why align to last dot on previous line?)

@straight-shoota
Copy link
Member

@RX14 I'm totally fine with 4 spaces indentation for method chained dot-lines. Though I'm not sure if this needs to be enforced by the formatter.

@makenowjust
Copy link
Contributor Author

@asterite Why does the formatter align method chain with the last dot on previous line? So, why the formatter keeps this:

foo.bar.baz
       .quz

This behavior makes too deep indentation and I think it is wrong. If the formatter aligns method chain with first dot, this PR fix does not lose readability in many case.

@asterite
Copy link
Member

By now I think the formatter should always use 2 spaces to indent.

@RX14
Copy link
Contributor

RX14 commented Oct 29, 2017

@asterite I agree normally, but I think that's wrong in the case of multi-line statements. Adding more indent makes it clear that it's a continuation of the previous statement, not just a normal control-flow indent.

@makenowjust
Copy link
Contributor Author

I implemented @RX14's idea at fix/crystal-format/multiline-call-4-space-indent branch. And this is the diff to apply a new crystal tool format to crystal source tree. It looks totally good, especially these lines become quite good.

What do you think?

@makenowjust
Copy link
Contributor Author

Opened a new pull request #5234.

@RX14 RX14 closed this in #5234 Jan 2, 2018
@makenowjust makenowjust deleted the fix/crystal-format/5193-format-indentation-block branch January 2, 2018 16:01
@RX14 RX14 mentioned this pull request May 23, 2018
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.

None yet

6 participants