LazyNick7
LazyNick7

Reputation: 83

Vim ruby identation

I'm learning Vim and need to do the following: Suppose I typed this:

class MyClass
private
end

After typing the gg=G I want the private to remain at the level with the class declaration, like this:

class MyClass
private
end

But it looks wrong (indent moves private to the right):

class MyClass
  private
end

How i can add custom rule for autoidentation of private?

Upvotes: 4

Views: 117

Answers (2)

anon
anon

Reputation:

If you'd like to keep the access modifier at the level of the class declaration, you can get Vim to respect that by putting this in your .vimrc:

let g:ruby_indent_access_modifier_style = 'outdent'

This is a configuration setting coming from Vim's ruby support, vim-ruby (documentation).

Note that this will only work with a recent enough Vim version. I'm not quite sure which, but, if it doesn't work for you, install the vim-ruby plugin manually, like an ordinary plugin -- this'll give you access to the latest runtime files, including this setting (which might be a mixed blessing, there's currently some oddness around heredoc highlighting).

Upvotes: 1

B.G.
B.G.

Reputation: 6016

As the comments already mentioned, vim uses the recommended indentation: https://github.com/bbatsov/ruby-style-guide#indent-public-private-protected

However, if you want to change it, you can by setting a custom indentexpr

autocmd FileType ruby setlocal indentexpr=YourCustomFunction()

However writing such a function wil be a lot of work. Better go with the sane function your vim already uses and keep it to the official recommendation.

P.S. Rubocop has nothing against it, the error you get probably refers to an useless private statement because there is no function afterwards.

Upvotes: 2

Related Questions