posit-dev / positron

Positron, a next-generation data science IDE
Other
1.48k stars 43 forks source link

ark: Support indentation of vertically aligned arguments #574

Open jmcphers opened 1 year ago

jmcphers commented 1 year ago

A common R code formatting convention is to vertically align function arguments. In RStudio, this behavior is called "Vertically align arguments in auto-indent" (Code -> Editing).

We should support this convention in Positron to make it easier for people to format their code nicely, and to make it easier to transition from RStudio.

Currently our auto-indent rules are declarative:

https://github.com/rstudio/positron/blob/main/extensions/positron-r/language-configuration.json#L37-L47

Note however that LSPs can supply as-you-type formatters, which might allow us to adopt more complex or contextual behavior.

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_onTypeFormatting

kevinushey commented 1 year ago

Note that formatting only happens in response to trigger characters, and so is typically used to format after inserting special characters (e.g. {).

One could imagine using this for, say, , to fix up indentation after-the-fact, but I personally like that RStudio is able to produce the "right" indentation as the user types.

It also implies that formatting could becoming laggy if there was some delay communicating with the LSP; I found this incredibly annoying with the C / C++ extension.

https://github.com/microsoft/vscode-cpptools/issues/6156#issuecomment-693637960

Compare this to the approach done by the popular Python Indent VSCode extension, which uses a custom command:

https://github.com/kbrose/vsc-python-indent/blob/master/package.json#L42-L56

and then performs all the work in the extension itself to compute the indentation:

https://github.com/kbrose/vsc-python-indent/blob/master/src/indent.ts

lionel- commented 3 months ago

This is discussed on the VS Code side in https://github.com/microsoft/vscode/issues/66235

A VS Code dev suggested (https://github.com/microsoft/vscode/issues/66235#issuecomment-452626231) that group capture rules (https://github.com/microsoft/vscode/issues/17281) could be used to specify vertical alignment. The capture group issue (as well as many others related to indentation!) was self-assigned by another dev two weeks ago so we might see progress on this within the next months.