Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: better auto indent #62

Closed
adamaltmejd opened this issue Apr 18, 2018 · 12 comments
Closed

Feature request: better auto indent #62

adamaltmejd opened this issue Apr 18, 2018 · 12 comments

Comments

@adamaltmejd
Copy link

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.

@Ikuyadeu
Copy link
Member

@adamaltmejd Thank you for your good idea!
It will improve readability.
I will search to similar function from other VS Code extensions.

@Ladvien
Copy link
Collaborator

Ladvien commented Apr 25, 2018

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);
        }
    }
});

@robertamezquita
Copy link

robertamezquita commented Aug 22, 2018

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!

@robertamezquita
Copy link

Bump! Just want to ^ above, @Ikuyadeu - wondering if its something I should look into helping with. Thanks!

@andycraig
Copy link
Collaborator

andycraig commented Oct 24, 2019

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’m going to close this. All the indenting formats described in this issue look good to me, so if they’re not already implemented in styler please consider adding them there!

@MarkCButler
Copy link

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.

@renkun-ken
Copy link
Member

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 REditorSupport/languageserver#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.

@MarkDVerhagen
Copy link

MarkDVerhagen commented Jan 5, 2021

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!

@Fred-Wu
Copy link

Fred-Wu commented Sep 8, 2021

Just found an extension called "Indent To Bracket" that would do indent to open bracket.

@ChaoXu1997
Copy link

Just found an extension called "Indent To Bracket" that would do indent to open bracket.

The extension is nice,

@ArunFrey
Copy link

I still seem to be running into this formatting issue. Has there been any update on this, or is there a way to customise the indentation options to align with other IDEs, like @MarkCButler suggested? Thanks!

@markjrieke
Copy link

markjrieke commented Nov 12, 2023

@robertamezquita --- did you ever find a workable solution? A few years down the line & it looks like mirroring RStudio's formatting in VS Code still isn't possible (& I find the formatting in styler to be pretty yucky)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests