jcfaria/Vim-R-plugin

Indent rules for line after “{”

klmr opened this issue · 4 comments

klmr commented

The indent rules for Vim-R are generally very sensible but they fail in cases such as this (testthat code, but the same situation is generally applicable):

test_that('the function works', {
          |
})

The position of the cursor is indicated by “|”, and I think it’s uncontroversial to call this wrong: in such a case, we want to simply shift the indent by shiftwidth from the previous level, rather than aligning it with the previous opening parenthesis.

I’ve read the FAQ and I realise this may be related to #77 but it looks sufficiently dissimilar – maybe there’s hope?

I added the file that I use to test the indentation to the github repository:
https://github.com/jcfaria/Vim-R-plugin/blob/master/r-plugin/indent_test.R

And I managed to add a rule that fixed the bug you reported and did not break anything in the previous 500 lines of indent_test.R: 4b3abb8

Note: I think that the correct indentation of the closing brace is aligned with the opening parenthesis.

klmr commented

Note: I think that the correct indentation of the closing brace is aligned with the opening parenthesis.

I disagree. First of all, this means that the block between the braces is indented extremely widely. Secondly, it would mean that the statements inside the block are no longer indented at a multiple of shiftwidth, which brings a whole bag of problems.

Compare (shiftwidth = 4):

test_that('my unit test', {
▸---------------------  if (some_condition)
▸------------------------  result---------------------  else------------------------  other_result------------------  })

With:

test_that('my unit test', {
▸---if (some_condition)
▸------result---else------other_result
})

I had made a mistake while copying you example. I did not set 'paste' and the underline was converted into <-. I updated the r-plugin/indent_test.R. I also added a new file: r-plugin/indent_test_noalign.R.
The indent/r.vim script supports two main styles:

  1. Function arguments are aligned with the opening parenthesis.
  2. Function arguments follow the general rule of 1 shiftwidth for each opening bracket (parenthesis, square bracket or braces).

Other options control how to align comments and top level functions (:h ft-r-indent).

I think that the indentation is closer to what you want when r_indent_align_args=0, but some bugs remain. I have tried to fix them, but did not succeed. So, we may let this issue open.

For people wanting to improve the indent/r.vim script, I suggest the following:

  1. Put the cursor on the line that is being wrongly indented.
  2. In Normal mode do :debug call GetRIndent() then press s, press n and press <Enter> to see the function working. For details, see :h :debug.

I deleted from the Vim-R-plugin the files that are already distributed as official Vim runtime files. Now, there is a new repository with these files: https://github.com/jalvesaq/R-Vim-runtime

Since the official runtime files are no longer in this repository, I'm closing this bug. This bug is already described in the Known bugs section of the README.md, and new bugs should be reported in the new repository.