Dart-code: support format on save

Created on 13 Oct 2016  路  9Comments  路  Source: Dart-Code/Dart-Code

VS Code 1.6.0
Dart Code 0.11.2
Ubuntu

I can format a file using the "Format Code" command (Control-Shift-I for me) but formatting the same file using format-on-save has no effect.

To reproduce, set:
"editor.formatOnSave": true,

Change the file so it needs formatting and save the file.

Format-on-save does work for other file types, such as the settings.json file.

(Although, I'd rather have format-on-save turned on only for Dart. I filled a feature request: Microsoft/vscode/issues/13547)

fixed in vs code is enhancement

Most helpful comment

@nozzlegear VS Code supports language-specific settings, see here:

https://code.visualstudio.com/docs/getstarted/settings#_language-specific-editor-settings

You can access them through the command palette though it basically just groups them in your user settings like this:

{
  "[dart]": {
    "editor.formatOnSave": false,
    "editor.formatOnPaste": false
  }

Info on trailing commas can be found here:

https://flutter.io/formatting/#using-trailing-commas

Code-completion for flutter inserts them automatically to guide people into it. Whether it's great that trailing commas are used to affect formatting is IMO questionable but that's how it is for now at least!

All 9 comments

Based on this code I think Code might be calling the range formatter instead of the full document formatter - something Dart Code doesn't provide (because the analysis server doesn't provide it). I've added comments to https://github.com/Microsoft/vscode/issues/13690 to see if they can clarify - I hope it's something they might change if that's the case.

MS confirmed it only works with range formatters. They've tagged the issue as a bug for the October release, so unfortunately this won't work till 1.7, but should then!

This has been fixed in VS Code 1.7 (currently Insiders) :-)

1.7 has been released, so this should now work as expected!

Sorry to revive a long-closed issue: is there a way to disable formatting for Dart? I'm working with React and the auto-formatting is really making my render functions hard to parse.

You can disable formatOnSave and formatOnType but I think both are off by default. It's not clear from your message when the formatting is occurring.

If you raise a new issue, it'll be easier to track if we end up having to make changes to support it.

Btw, if you add trailing commas to your method calls, it'll also change the formatting in a way that might be better for React-style code (it was done for Flutter which is very similar).

Sorry, should have been more clear. I do have VS Code's formatOnSave turned on. I like it in most languages and would prefer to leave it on, but with Dart it's applied a little too liberally and makes my React renders difficult to read. I was looking for a setting to turn the formatting off just for Dart files and leave it on for everything else. I'll try the trailing commas though (didn't know about that!) and file a new issue when I get back home.

@nozzlegear VS Code supports language-specific settings, see here:

https://code.visualstudio.com/docs/getstarted/settings#_language-specific-editor-settings

You can access them through the command palette though it basically just groups them in your user settings like this:

{
  "[dart]": {
    "editor.formatOnSave": false,
    "editor.formatOnPaste": false
  }

Info on trailing commas can be found here:

https://flutter.io/formatting/#using-trailing-commas

Code-completion for flutter inserts them automatically to guide people into it. Whether it's great that trailing commas are used to affect formatting is IMO questionable but that's how it is for now at least!

Thank you! I didn't know you could turn them off per language, I was looking for an extension-specific setting instead. Trailing commas are working too, though I agree it's a little questionable 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CaferPoyrazoglu picture CaferPoyrazoglu  路  4Comments

shamrin picture shamrin  路  5Comments

atreeon picture atreeon  路  5Comments

DanTup picture DanTup  路  4Comments

mayorbyrne picture mayorbyrne  路  5Comments