R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
129 stars 15 forks source link

Change default indentation style? #98

Closed wurli closed 3 months ago

wurli commented 3 months ago

One of the most important changes for me when setting up R.nvim was to change the default indentation style:

vim.g.r_indent_align_args = 0

This changes the indentation behaviour from this:

paste(
      ... # <- carriage return aligns cursor with `(` from prev line

To this:

paste(
  ... # <- carriage return adds a single layer of indentation

People of course have different opinions about which style is better, so in my view it would be best to:

# Good
do_something_very_complicated(
  something = "that",
  requires = many,
  arguments = "some of which may be long"
)

# Bad
do_something_very_complicated("that", requires, many, arguments,
                              "some of which may be long"
                              )

(From The tidyverse style guide, on Syntax/Long lines)

I'm enjoying this plugin so much - thank you for creating it and generously making it available to the world!

jalvesaq commented 3 months ago

The indentation is defined in the official Vim runtime file indent/r.vim, not by R.nvim or Nvim-R. I used to be the maintainer of the Vim runtime files for R, but no longer. See: https://github.com/vim/vim/blob/master/runtime/indent/r.vim

I'm no longer the maintainer of the Vim runtime files for R because I only use Neovim which (with tree-sitter) doesn't use the syntax and indent scripts from Vim. The ftplugin scripts are still used, but lately, I was wasting my time adding features that Lua plugins don't require to work. For example, in this commit, I turned the value 'commentstr' dynamic, but Comment.nvim uses tree-sitter instead of 'commentstr' to define the comment string. So, I implemented a feature that would be useful only for Vim, not Neovim while I no longer use Vim to see if the feature works well or has bugs.

jalvesaq commented 3 months ago

Please, see also: https://github.com/jalvesaq/Nvim-R/issues/577#issuecomment-1892587154

wurli commented 3 months ago

Thank you, your explanation is much appreciated.

So if I understand correctly, the vim.g.r_indent_align_args setting comes from the vim runtime file indent/r.vim, but Neovim uses treesitter for indentation. Does treesitter then make use of this parameter? I'm a bit confused as to why setting vim.g.r_indent_align_args = 0 worked if Neovim doesn't use the indentation scripts from Vim.

jalvesaq commented 3 months ago

Neovim still uses the syntax and indent files by default, but Neovim's team is investing in a tree-sitter-based solution as a replacement. Currently, you have to install nvim-treesitter and explicitly enable highlighting and indenting via treesitter. I already have syntax highlighting via treesitter enabled, but not indenting yet. Anyway, the indent/r.vim script was the one I disliked most because it's too big, slow, and still has bugs. I decided to abandon the whole R-Vim-runtime repository and patiently wait for the treesitter indenting to become better than Vim's one.

jalvesaq commented 3 months ago

In summary, at least for R and Quarto:

wurli commented 3 months ago

I see - all makes sense now. Hopefully the unmaintained Vim indentation will suffice to carry things over until treesitter indentation matures.

Many thanks again!

PMassicotte commented 3 months ago

Just chiming in, I am using this to help me with indentation.

return {
    "Wansmer/treesj",
    keys = {
        {
            "<leader>m",
            "<CMD>TSJToggle<CR>",
            "Toggle Treesitter Join",
        },
    },
    dependencies = { "nvim-treesitter/nvim-treesitter" },
    config = function()
        require("treesj").setup({
            max_join_length = 240,
        })
    end,
}
ssh352 commented 1 month ago

In summary, at least for R and Quarto:

  • Syntax highlighting is better with treesitter than with Vim's syntax scripts.
  • We don't need complex features in ftplugin scripts if using treesitter.
  • Vim's indenting is still better in my opinion, but I'm tired of maintaining it.

sorry for digging this old thread.

When I write code in other languages: Java indent is taken care of by google-java-format. Rust indent is taken care of by rustfmt.

R indent is probably taken care of by styler or in the future a better formatter. It is quite tedious to write code to certain indent style. The formatter can already take care of it automatically.

What do you think?