tree-sitter-ruby: Wrong Ruby indent when entering a `.` before `end`

If you type . before an end, you get wrong indentation as seen below (in neovim). Before: image After: image

See video in: https://github.com/nvim-treesitter/nvim-treesitter/issues/3363

Based on what @theHamsta said in that issue, it sounds like this might be fixable in the grammar?

The problem is in the ruby parser. It parses self.end as function call. Is end allowed in Ruby as an identifier? There were plans to better support keyword forbidden in identifiers (https://github.com/tree-sitter/tree-sitter/pull/1635).

This is how the ruby parser sees your file in the moment you enter the .

class User < ApplicationRecord

def foo
  if true
    self.
  end
end

Here what vscode shows (which doesn’t use treesitter and doesn’t have the same issue): image

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 16

Commits related to this issue

Most upvoted comments

[1] pry(main)> class A
[1] pry(main)*   def end
[1] pry(main)*     puts 3
[1] pry(main)*   end  
[1] pry(main)* end  
=> :end
[2] pry(main)> A.new.end
3
=> nil

#end is a valid ruby method name. As such, parser does its job correctly.

hi! There hasn’t been any answer in some months and this effectively makes indentation unusable, is there any plan to fix it?

I don’t think one can really stick to end is technically a valid method if indentation is broken

You’re asking a third-party parser to fix something that ruby interpreter itself cannot deal with.

Create a file with the following contents, run it with ruby, check which line the interpreter complains about.

class A
  def a
    self.
  end

  def end
    42
  end

  private

  def even_better
    66.
  end
end

The only reason you know the indentation is wrong is because you’re operating in a dimension the parser doesn’t operate in - time. You intend to write more and so you know there will be a method that’s not #end. Without human input the situation in undecidable, because someone else could instead go and modify the code above to look like this:

class A
  def a
    self.
      end
  end

  def end
    42
  end

  private

  def even_better
    66.to_s
  end
end

Parser doesn’t know your intentions, it operates here and now, and here and now you’re calling #end.

You can update your editor behaviour to reindent when a new method call appears in the tree. You can propose a general tree-sitter update that will make it operate on current input and an optional previous tree state, introducing temporal dimension that way. You could go to main nvim-treesitter repo and argue that https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/ruby/indents.scm needs to be updated so that following lines are shifted to the right instead of current one shifting to the left.

What you can’t do is claim that this parser is working incorrectly in this case.

adding vim.cmd('autocmd FileType ruby setlocal indentkeys-=.') in my top level init.lua did the trick for me! very interesting conversation, reminding me that ruby doesn’t care about white space!

credit: https://github.com/nvim-treesitter/nvim-treesitter/issues/3363#issuecomment-1538607633

Thanks @sharkby7e your solution works nicely.

Here’s a Lua equivalent:

vim.api.nvim_create_autocmd("FileType", {
  pattern = { "ruby" },
  callback = function()
    vim.opt_local.indentkeys:remove(".")
  end,
})

For the Treesitter maintainers, I understand your position but as a Ruby developer this is something folks will encounter within a few minutes of using the tool and leaves an impression that something isn’t working correctly because the human expectation is what the workaround provides.

I disabled ruby indentation completely as I described here: https://github.com/nvim-treesitter/nvim-treesitter/issues/3363#issuecomment-2115177169