languageserver
languageserver copied to clipboard
Formatting and linting are inconsistent in terms of spacing a^(b)
Note that I am using https://github.com/REditorSupport/vscode-r-lsp, but I feel that particular implementation is not the core issue, so I'll try here first.
My problem is this line:
x^(y + z)
which I feel is nicely spaced. However, lintr
says
Place a space before left parenthesis, except in a function call.
So when I do this,
x^ (y + z)
the lintr
message disappears, but a subsequent "Format Document With ... - R LSP Client" in VS Code will remove the space and I end up with the first line and its lintr
message. (The same is true for x ^ (y + z)
.)
I guess the problem is that lintr
follows Hadley Wickham's style guide (source), which states
Place a space before left parentheses, except in a function call.
and
Place spaces around all infix operators
(emphasis mine) and has an y ^ x
example in the code. So it's hard to argue that lintr
is wrong.
At the same time, styler
follows the tidyverse
style guide (source), which does not require spaces around all parentheses and has an exception for certain infix operators, including ^
:
There are a few exceptions, which should never be surrounded by spaces:
- The operators with high precedence:
::
,:::
,$
,@
,[
,[[
,^
, unary-
, unary+
, and:
.
This is more of inconsistencies between styler and lintr. We are using styler to do formatting and lintr for diagnostics.
I think you can see from my original question that you basically rephrased my analysis ;) The question is, what should be done about it?
I did my own research and found https://github.com/jimhester/lintr/issues/302 - which is fixed but for some reason, the fix maybe has not made it into CRAN yet.
Here's another example:
a <- -(b)
Place a space before left parenthesis, except in a function call.
a <- - (b)
No lintr message, but "Format Document" reverts it back to the above code.
Since the lintr
documentation has changed since I reported this (it now says
Most of the default linters are based on Hadley Wickham's The tidyverse style guide.
I will also report this in https://github.com/jimhester/lintr/issues/508