Home > Net >  How do I get format on save to work with Deno?
How do I get format on save to work with Deno?

Time:09-26

Deno format on save isn't working for me. Initially my .vscode/settings.json was this:

{
  "deno.enable": true
}

Then based on this Stack Overflow question I tried this and it didn't work.

{
  "deno.enable": true,
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "denoland.vscode-deno"
}

I also tried this based on this comment in a GitHub issue and it didn't work.

{
    "deno.enable": true,
    "deno.lint": true,
    "deno.unstable": false,
    "editor.formatOnSave": true,
    "[typescript]": { "editor.defaultFormatter": "denoland.vscode-deno" },
}

This is my ~/Library/Application Support/Code/User/settings.json.

{
    "editor.minimap.enabled": false,
    "editor.tabSize": 2,
    "editor.fontSize": 14,
    "eslint.validate": ["javascript", "typescript"],
    "workbench.startupEditor": "none",
    "workbench.colorTheme": "One Dark Pro Darker",
    "atomKeymap.promptV3Features": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.formatOnPaste": true,
    "terminal.explorerKind": "external",
    "gitlens.currentLine.enabled": false,
    "gitlens.hovers.currentLine.over": "line",
    "gitlens.codeLens.enabled": false,
    "gitlens.statusBar.enabled": false,
    "gitlens.hovers.enabled": false,
    "gitlens.blame.avatars": false,
    "gitlens.blame.heatmap.enabled": false,
    "gitlens.changes.locations": [
        "gutter",
        "overview"
    ],
    "javascript.updateImportsOnFileMove.enabled": "always",
    "editor.lightbulb.enabled": false,
    "editor.scrollBeyondLastLine": false,
    "breadcrumbs.enabled": false,
    "security.workspace.trust.untrustedFiles": "open",
    "explorer.confirmDragAndDrop": false,
    "files.insertFinalNewline": true,
    "typescript.updateImportsOnFileMove.enabled": "always",
    "window.restoreWindows": "none",
    "explorer.autoReveal": false,
    "editor.formatOnSaveMode": "modifications",
    "editor.formatOnSave": true
}

Commenting out "editor.formatOnSave" doesn't fix it.

The docs aren't very helpful and googling around hasn't been either.

I've tried quitting and re-opening VSCode before trying all of this.

Running deno fmt in the command line does work.

CodePudding user response:

Install Deno extension: https://github.com/denoland/vscode_deno

Then on your .vscode/setting.json add the following settings

  "editor.codeActionsOnSave": {
    "source.fixAll": true,
    "source.organizeImports": true
  },
  "editor.defaultFormatter": "denoland.vscode-deno",
  "editor.formatOnSave": true,
  "editor.formatOnPaste": false,

By doing a fresh install of vscode_deno the above should work without any further configuration.

CodePudding user response:

You have configured a user setting which is preventing formatting: "editor.formatOnSaveMode"

The description for the setting is this:

Controls if format on save formats the whole file or only modifications. Only applies when #editor.formatOnSave# is enabled.

The setting has three options as of VS Code v1.71.2:

  • "file": Format the whole file.
  • "modifications": Format modifications (requires source control).
  • "modificationsIfAvailable": Will attempt to format modifications only (requires source control). If source control can't be used, then the whole file will be formatted.

You have currently configured it to "modifications".

I guess that — in your Deno workspace — there is either no source control configured or the file(s) that you are saving are not eligible change candidates, so no formatting action occurs when you save the file(s).

In order to get the behavior that you want, you need to configure the following settings:

  • "editor.formatOnSave": Set to true (This is already configured in your user settings.)

  • "editor.formatOnSaveMode": Set to either "file" or "modificationsIfAvailable" (Because you configured this to "modifications" in your user settings, I'm guessing that the one you'll want is "modificationsIfAvailable", and you can either update your user setting or override this value in the workspace settings.)

  • "deno.enable": Set to true (This should be configured in the workspace.)

  • "editor.defaultFormatter": Set to "denoland.vscode-deno" (This should be configured in the workspace.)

If you just want something to copy paste, use this for your workspace settings:

{
  "deno.enable": true,
  "editor.defaultFormatter": "denoland.vscode-deno"
  "editor.formatOnSave": true,
  "editor.formatOnSaveMode": "modificationsIfAvailable"
}

  • Related