vscode-R: Magrittr pipes trigger indent on newline

Not sure if this is the right place for this request, please delete if not !

Would it be possible to get magrittr’s pipes %>% to be treated akin to brackets where an automatic indent happens if you press enter on a line whose last symbol is the pipes?

i.e. having

iris %>% mutate(...)

and pressing enter after the %>% but before the mutate(...) would result in

iris %>%
    mutate(...)

At the moment when I do this it just comes out as:

iris %>%
mutate(...)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 20 (7 by maintainers)

Most upvoted comments

@gowerc https://github.com/REditorSupport/languageserver/pull/209 implements on-type-formatting on user input at expression-level based on a backward parsing strategy. With editor.formatOnType = true and vscode-r-lsp and languageserver installed, the formatting is exactly what you want.

Guys I am having something strange.

My vscode-R with R LSP Client and languageserver installed in a Intel Mac and a M1 Mac. All configurations the same. I cannot get the:

"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.formatOnPaste": true

to work in the M1 and it is flawless in the Intel. Same VS Code versions and extensions installed in both systems.

I’m curious about how the text edits are handled if editor.formatOnType is enabled and languageserver also performs on-type-formatting and indention, and vscode-R also makes some indention?

Just to add my findings so far though (as it’s not overly well documented)

You can set the following in the configuration.json file:

increaseIndentPattern - Permanently increases the indent counter by 1 decreaseIndentPattern - Permanently decreases the indent counter by 1 indentNextLinePattern - Adds 1 to the indent count for just the next line unIndentedLinePattern - Stops line from being considered for evaluation by the other pattern rules (thus preserving the current indent regardless of its pattern)

You can then also set additional on-enter rules which are well documented, though one thing I noticed is that any indentation that is added here i.e. IndentAction.Indent overwrites The indentation rules set in the configuration.json file.

The problem I was running into is that there is no meaningful way to distinguish the first use of the pipe (to thus do the initial indent) from subsequent uses of the pipe. I tried to play around with multiple line regex’s using (?m) however it doesn’t look like vscode supports these 😦 Thus I settled on just defining an on-enter rule for use of the <- and %>% as this more likely characterises the like first usage of the pipe.