WolframResearch / vscode-wolfram

Official Visual Studio Code extension for Wolfram Language
MIT License
109 stars 7 forks source link

Formatting options #24

Open hifismith opened 1 year ago

hifismith commented 1 year ago

Sorry if this isn't technically an issue, but I was wondering what everyone was doing for formatting. The only option in VS code for "Document Format As" is Wolfram Language, but that has the following problem:

I would like the indents to be added correctly for new lines, and for my line breaks to be left untouched. But what's going on now is code that starts like this: image

Ends up like this after shift+opt+F image

If there's a better place to ask this question (Stack Exchange etc.) please point me there. Thanks

bostick commented 1 year ago

Probably the best place is opening an issue for the CodeFormatter repo: https://github.com/WolframResearch/codeformatter

There are various options for the formatter that are exposed as options for the functions in the CodeFormatter` context.

I talk about the CodeFormatter` package in this talk from WTC2020: Formatting the Wolfram Language (youtube)

There is not yet a convenient way of modifying settings through VSCode, but you can modify settings for the formatter by changing the command for the LSP server and settings various variables.

Here is an example command with line width of Infinity and using the experimental line-breaking method "LineBreakerV2":

"wolfram.command": [
    "`kernel`",
    "-noinit",
    "-noprompt",
    "-nopaclet",
    "-nostartuppaclets",
    "-noicon",
    "-run",
    "Needs[\"LSPServer`\"];CodeFormatter`$DefaultLineWidth=Infinity;CodeFormatter`$DefaultBreakLinesMethod=\"LineBreakerV2\";LSPServer`StartServer[]"
]

LineBreakerV2 tries to do a better job of understanding and using the tree-structure of the code. But there are still issues and it is not yet turned on by default.

Formatter-specific issues can be opened here: https://github.com/WolframResearch/codeformatter

hifismith commented 1 year ago

This is really great information, thank you!

The IntelliJ plugin is quadrupling in price, so I thought I'd check in on the VS Code plugin. Formatting is probably my biggest hurdle in switching, so I'll definitely review the links you sent. Btw, thanks for adding the section folding. It's working