Would be really great if the autoIndent worked better, preferably according to standard style guides. (see https://google.github.io/styleguide/Rguide.xml and http://adv-r.had.co.nz/Style.html)
When a line ends with an open bracket of some sort ((, [, {) indentation kicks in according to whatever setting you have. This example already works as it should:
a <- b[
c >= 3 &
d == 2
]
However when a bracket is open, but there is something after it on the same line, indentation should be all the way to the opening bracket, not just the default number of spaces. For example:
bad way:
a <- b[c >= 3 &
d == 2]
correct:
a <- b[c >= 3 &
d == 2]
That is, when pressing enter after the & sign, indent should be 7 spaces instead of 2. Right now, when one presses enter after the & sign, no indent is applied at all.
Maybe one could implement the styler package to do it automatically (http://styler.r-lib.org) using the formatOnSave option in VsCode.
@adamaltmejd Thank you for your good idea!
It will improve readability.
I will search to similar function from other VS Code extensions.
It looks like the formatter API should do the trick.
Here's the example in the docs:
// 馃憤 formatter implemented using API
vscode.languages.registerDocumentFormattingEditProvider('foo-lang', {
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
const firstLine = document.lineAt(0);
if (firstLine.text !== '42') {
return [vscode.TextEdit.insert(firstLine.range.start, '42\n')];
}
}
});
We could probably use this as the first step towards a variable viewer as well.
vscode.languages.registerDocumentFormattingEditProvider('foo-lang', {
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
const lastLine = document.lineAt(getLastLineNumber());
if (lastLine.indexOf('<-')) {
const rVariable = getVariableFromLine(lastLine);
addVariableToDisplay(rVariable);
}
}
});
Another question, I just want to make sure this is not just on my end. Is the auto-indenting also not working with other multi-line constructs? E.g. this is what I get when I do the following:
# when creating a new line, does not indent in any case
# using the R v0.6.1 + R-LSP v0.0.7 extensions in VSCode 1.26.1
foo <- c(1, 2, 3)
bar <- c(4,
5,
6)
mon <- function(x,
y,
z)
zun <- foo +
bar
hun <- zun %>%
sum() %>%
function2(.,
arg = 'a',
arg2 = 'b') %>%
another_function(.,
another_arg = 1,
last_one = Inf)
When it should be:
# manually styled per convention
foo <- c(1, 2, 3)
# additional function args line up with first arg
bar <- c(4,
5,
6)
mon <- function(x,
y,
z)
# line continuations use standard indentation for both (+, %>%)
zun <- foo +
bar
# mixed standard indention for continued lines + function args lining up
hun <- zun %>%
sum() %>%
function2(.,
arg = 'a',
arg2 = 'b') %>%
another_function(.,
another_arg = 1,
last_one = Inf)
# if there is an empty paren ending the line, fall back to standard indentation
# within the function however, follow the parent's indentation rule
last <- function(
arg1 = c(2, 3,
4)
)
Just want to make sure this is also a current non-feature. Thanks!
Bump! Just want to ^ above, @Ikuyadeu - wondering if its something I should look into helping with. Thanks!
The version of the language server on GitHub supports customised styler formatting: https://github.com/REditorSupport/languageserver/blob/master/README.md#customizing-formatting-style
I think customised indenting should be supported via the styler package rather than directly in vscode-R, so I鈥檓 going to close this. All the indenting formats described in this issue look good to me, so if they鈥檙e not already implemented in styler please consider adding them there!
An alternative point of view is that the requested indentation is a widely used standard for R, rather than being a customization that should be delegated to another tool.
The two other tools that I have used for editing R code (RStudio and PyCharm) both implement this indentation out of the box.
For me, having this standard indentation supported by the plugin without requiring external restyling would be a really valuable enhancement.
I'm thinking that a fast indention formatter would be nice, at least it could be faster than the indention created by the languageserver on-type-formatting implemented by https://github.com/REditorSupport/languageserver/pull/209.
I use styler to create the indention mainly to walk-around the complexity of the expression detection but it clearly relies on the correctness of the syntax of the expression under cursor but it is not trivial to cover some cases. Moreover, there's a significant lag between hitting the key ()]}\n) that triggers the on-type-formatting and getting the result because styler package is probably too slow for live editing purpose.
I agree with the MarkCButler's view that this can be considered an appropriate default. Perhaps a nice starting point is the following https://github.com/kbrose/vsc-python-indent which does the requested for Python code!
Most helpful comment
I'm thinking that a fast indention formatter would be nice, at least it could be faster than the indention created by the languageserver on-type-formatting implemented by https://github.com/REditorSupport/languageserver/pull/209.
I use styler to create the indention mainly to walk-around the complexity of the expression detection but it clearly relies on the correctness of the syntax of the expression under cursor but it is not trivial to cover some cases. Moreover, there's a significant lag between hitting the key (
)]}\n) that triggers the on-type-formatting and getting the result because styler package is probably too slow for live editing purpose.