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

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

Open
ibrahima opened this issue Dec 22, 2023 · 1 comment
Open

Comments

@ibrahima
Copy link

ibrahima commented Dec 22, 2023

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.

@rafbm
Copy link

rafbm commented Jun 26, 2024

Similar to prettier/plugin-ruby#728

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

No branches or pull requests

2 participants