Crystal: Formatter indentation not matching with what I'd expect

Created on 2 Dec 2015  路  9Comments  路  Source: crystal-lang/crystal

Hi there!

Learning more every day, and ran into this. Since I know the formatter is new and under active discussions, I thought I'd show what I'm seeing, and see if I'm way wrong about this.

Consider a Ruby class that has a few paramters that need to be supplied during initialize:

module Foo
  class Bar
    def initialize(
      name,
      value,
      type
    )
      # ... snip
    end
  end
end

The Ruby Style Guide will indent each paramter on a new line, indented from the opening paren, with the closing paren on its own line, indented to the opening declaration.

If I apply a similar approach to Crystal 0.9.1:

module Foo
  class Bar
    def initialize(
      @name : String,
      @value : (Int32 | Float),
      @type : String
    )
      # ... snip
    end
  end
end

Running the formatter tool gets converted to:

module Foo
  class Bar
    def initialize(
                   @name : String,
                   @value : (Int32 | Float),
                   @type : String)
      # ... snip
    end
  end
end

Which leaves me the closing paren almost "invisible" at the end of the @type line.

I tried to find a simple way to scaffold a test for the formatters - but I'm a bit behind on reading how to work with the core language - any pointers would be helpful.

feature topicformatter

Most helpful comment

@miketheman You are right. Right now this:

class Foo
  def initialize(
    @x,
    @y
    )
  end
end

is incorrectly realign to:

class Foo
  def initialize(
                 @x,
                 @y)
  end
end

Specs are here and the code is here, though the alignment part is a bit tricky. I will try to fix this.

All 9 comments

Yes, I think the first style makes more sense. It's also easier to read because code doesn't go too much to the right of the screen. I'll fix it :-)

@miketheman Actually, where in the [Ruby Style Guide[(https://github.com/bbatsov/ruby-style-guide) it says that? I can't find it. And I don't know why the closing parentheses' visibility is important: you can tell where the method body starts by the indentation level:

module Foo
  class Bar
    def initialize(
                   @name : String,
                   @value : (Int32 | Float),
                   @type : String)
      this_is_the_body
    end
  end
end

Having it be like this:

module Foo
  class Bar
    def initialize(
                   @name : String,
                   @value : (Int32 | Float),
                   @type : String
                  )
      this_is_the_body
    end
  end
end

isn't better in my opinion.

@asterite You're absolutely right - I assumed it was in the style guide - because bbatsov also maintains Rubocop formatted the Ruby code for me in the first example.

The closing parens at the end of String) had me visually parsing that trailing parens as part of a grouping, like on the prior line - and had to ask myself if I missed something.

As for the alignment of the params under the opening parens - I'm in favor of the 2-space instead of all that leading whitespace.

The way I'd format it myself is like the second example by @miketheman.

I wanted to revisit this briefly after finding the section in the Ruby Style Guide that discusses param indentation.

After reviewing the section, I can now conclude that the alignment of the params does indeed follow the style described, despite me being unfamiliar with it.

The rules and specs allow for both options - so I'd be happy if the formatter tool recognized a regular indentation.

I'd be happy to try to figure this out in the formatter code, if pointed in the right direction - I also didn't readily find any specs for the formatter tool - are they hiding?

@miketheman You are right. Right now this:

class Foo
  def initialize(
    @x,
    @y
    )
  end
end

is incorrectly realign to:

class Foo
  def initialize(
                 @x,
                 @y)
  end
end

Specs are here and the code is here, though the alignment part is a bit tricky. I will try to fix this.

+1 for the two space option, it would also give you more space to group related params, like

module Foo
  class Bar
    def initialize(
      @name : String, @sur_name : String, @age : Int32,
      @address : String, @state : String, @country : String
      @phone : String, @email : String
    )
      # ... snip
    end
  end
end

I've been trying to figure out a simple way to work on the formatter, but it's a bit hard to wrap one's head around from the outset, and there's little inline guidance on how to walk through the 4000+ lines in the formatter.cr

I'm looking for some way that I could execute the formatter tool, and step through lines in the compiler, i.e. with a breakpoint and pry, as one might do in Ruby - how might I go about doing that in Crystal?

We should ping the formatter master @MakeNowJust , just in case :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cjgajard picture cjgajard  路  3Comments

Papierkorb picture Papierkorb  路  3Comments

nabeelomer picture nabeelomer  路  3Comments

asterite picture asterite  路  3Comments

asterite picture asterite  路  3Comments