Ruby indenting in Neovim
One annoyance I had to work through when I started using Neovim was an auto-indenting issue for Ruby code.
Whenever I would write a method, the first line inside the method that should be indented one step would be reset to align with the previous line as soon as I typed a ..
# I would start by writing this:
def index
@posts =
# But as soon as I entered a ".", the indentation would change to this:
def index
@posts = Post.
Ruby’s buffer-local indentkeys configuration includes ., so typing a period tells Neovim to recalculate the current line’s indentation using the active indentexpr. Tree-sitter’s indentation expression can then calculate the wrong indentation while the Ruby expression is still incomplete.
Tree-sitter parsing, highlighting, and indentation are separate concerns. You can continue using Tree-sitter for parsing and highlighting while changing this indentation behaviour.
The most reliable fix is to create the following file in your Neovim configuration:
~/.config/nvim/after/indent/ruby.vim
Add this line to the file:
setlocal indentkeys-=.
Files inside after/indent/ run after Neovim’s standard Ruby indentation configuration. This ensures that . is removed after the other Ruby indentation settings have loaded, so typing a period no longer triggers the unwanted reindentation.
See nvim-treesitter issue #2566 for the original discussion.