ruby-syntax-tree / syntax_tree

Interact with the Ruby syntax tree

Home Page:https://ruby-syntax-tree.github.io/syntax_tree/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constructor with block gets split onto new line even if it would fit

ibrahima opened this issue · comments

Hi! Thanks for building syntax_tree, I've started using it recently and am excited to enter a new world of not caring about code formatting in Ruby. I noticed one behavior that I'm not sure if it's intentional or not. When I have a constructor with a block argument, the constructor gets split onto a new line even if it would fit within the print width. The easiest example comes from the OptionParser documentation (in fact, I experienced this from code that I had based on this example). I don't know if it's specific to constructors or assignment, haven't really delved into other scenarios yet.

For this example I think I have print-width set to 100 (at least, I have a ~/.streerc with that set, though I am not 100% sure if it is read from the home directory).

The OptionParser example
require 'optparse'

Options = Struct.new(:name)

class Parser
  def self.parse(options)
    args = Options.new("world")

    opt_parser = OptionParser.new do |opts|
      opts.banner = "Usage: example.rb [options]"

      opts.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
        args.name = n
      end

      opts.on("-h", "--help", "Prints this help") do
        puts opts
        exit
      end
    end

    opt_parser.parse!(options)
    return args
  end
end
options = Parser.parse %w[--help]

How syntax_tree formats it:

require "optparse"

Options = Struct.new(:name)

class Parser
  def self.parse(options)
    args = Options.new("world")

    opt_parser =
      OptionParser.new do |opts|
        opts.banner = "Usage: example.rb [options]"

        opts.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
          args.name = n
        end

        opts.on("-h", "--help", "Prints this help") do
          puts opts
          exit
        end
      end

    opt_parser.parse!(options)
    return args
  end
end
options = Parser.parse %w[--help]

In particular, this is the part that surprises me:

-     opt_parser = OptionParser.new do |opts|
-       opts.banner = "Usage: example.rb [options]"
+     opt_parser =
+       OptionParser.new do |opts|
+         opts.banner = "Usage: example.rb [options]"

It seems kind of odd because by doing this it ends up indenting the block closing end one extra step, which looks slightly weird to me. I am guessing this is probably intentional, but I'm curious what the reason is behind this formatting. Thanks!


Addendum: It does look like it's just with any assignments that take a multi-line block.

It turns this:

a = [1, 2, 3]

b = a.each do |x|
  y = x**2 + 2 * x + 1
  [x, y]
end.to_h

p b

into this:

a = [1, 2, 3]

b =
  a
    .each do |x|
      y = x**2 + 2 * x + 1
      [x, y]
    end
    .to_h

p b

Truthfully, I feel like the output is pretty odd in this case, but it's kind of a pathological example with such short variable names.