Blastula
Blastula

Reputation: 504

Vim Ruby multi-line method indentation

I take issue with the default line breaks I get from my vim setup. I think the only relevant plugin I'm using is vim-ruby.

I want code that auto-indents like this:

let(:account) do
  create :account,
    store: build(:live_store,
                 shop_version: build(:shop_version,
                                      name: "Example"
                                     )
                )
end

to instead auto-indent like this:

let(:account) do
  create :account,
    store: build(:live_store,
      shop_version: build(:shop_version,
        name: "Example"
      )
    )
end

Does this make sense or am I off in the weeds? I find the defaults very ugly and especially frustrating when trying to enforce 80 character lines.

Thank you!

Upvotes: 1

Views: 198

Answers (2)

anon
anon

Reputation:

The reason that vim-ruby indents your example so deeply is to support this coding style:

let(:account) do
  create :account,
    store: build(:live_store,
                 shop_version: build(:shop_version,
                                     name: "Example"))
end

A lot of people like this "hanging" style, which is why it's how vim-ruby indents if you use round brackets with the first argument on the same line. You could, as @Amadan points out, put all the arguments on new lines:

let(:account) do
  create :account,
    store: build(
      :live_store,
      shop_version: build(
        :shop_version,
        name: "Example"
      )
  )
end

Alternatively, my preferred supported indentation style uses curly brackets:

let(:account) do
  create :account,
    store: build(:live_store, {
    shop_version: build(:shop_version, {
      name: "Example"
    })
  })
end

There's a lot of different combinations of indentation preferences and we're limited in how much variability we can support (I'm a maintainer) -- the code is old and full of edge cases. I'm afraid these three styles are basically it, and I hope you can tweak your coding style to reach a compromise. It might be that at some future point I sit down and try some radical changes, but it's a difficult project to find time and energy for.

Upvotes: 1

Blastula
Blastula

Reputation: 504

I have found a reasonable workaround. Vim will indent automatically the way I want if I use hash literals in the code. For example, typing the above example like this works fine:

let(:account) do
  create :account,
    store: build(:live_store, {
      shop_version: build(:shop_version, {
        name: "Example"
      })
    })
end

This feels like a reasonable enough compromise.

Upvotes: 1

Related Questions