Prettier-vscode: Weird auto-formatting of code upon paste

Created on 19 Oct 2017  路  16Comments  路  Source: prettier/prettier-vscode

Description

In JS files this extension will sometimes mess up indentation upon pasting. The circumstances under which this seems to happen is when having "editor.formatOnPaste": true enabled (something that Atom Keymap adds to your config automatically).

With prettier extension _disabled_:

vscode_prettier_ok
This is when having prettier disabled. I start out by marking the text and pressing CMD+C, place the cursor where I want to paste, and CMD+V. Everything pastes as one would expect.

With prettier extension _enabled_ and _including_ trailing comma

vscode_prettier_fail
This is after enabling prettier. Again, I mark the text, copy it, paste it. Split second after pasting prettier has formatted everything in a horrific manner.

With prettier extension _enabled_ an _excluding_ trailing comma

I then found out that if I do not mark the last comma in the selection I want to copy+paste, it works even with prettier enabled.

vscode_prettier_enabled_ok

Snippet:

module.exports = ({ config }) => {
  return [
    {
      key: 'some value',
      fn: async (req, res) => {
        return 'some value'
      },
    },
  ]
}

My config:

{
  "workbench.colorTheme": "Oceanic Next Italic",
  "window.nativeTabs": true,
  "editor": {
    "formatOnSave": true,
    "fontFamily": "Fira Code",
    "fontSize": "16",
    "fontLigatures": true,
    "tabSize": 2,
    "tabCompletion": true,
  },
  "prettier": {
    "singleQuote": true,
    "semi": false,
    "trailingComma": "es5",
    "printWidth": 100
  },
  // added by atom keymap
  "atomKeymap.promptV3Features": true,
  "editor.multiCursorModifier": "ctrlCmd",
  "editor.formatOnPaste": true
  // end atom keymap
}
bug locked

Most helpful comment

I think "editor.formatOnPaste": false, fixed my issue.

All 16 comments

Yep same thing happens by calling the range formatter (Format selection). FormatOnPaste calls the range formatter.

It messes the output if the trailing-comma is selected/pasted.
It throws if it is not selected/pasted.

I experience the same, also added a gif inside another issue because I first thought VS code was causing the behaviour. https://github.com/Microsoft/vscode/issues/33522

This should be fixed in prettier 1.8 (version we are now bundling)

I'm still experiencing this issue with the latest extension 1.5.0

Me too in VSCode!

I think "editor.formatOnPaste": false, fixed my issue.

"editor.formatOnPaste": false did not fix my issue.. the way it imports and formats is whack, but undo fixes it.

This worked for me: Go to VS Code Settings -> from the User Settings tab -> Text Editor -> Formatting -> Untick the first option 'Format On Paste'

same issue, very annoying

Thanks @faiz021 , that worked for me

This was driving me insane. Found the config and turned it off and I'm happy again :) Of course, now I find the fix here :)

This is still an outstanding issue on 1.9.0.
Disabling "Format On Paste" is not a good solution, but merely a workaround.

My "Format on Paste" is disabled since long time ago, but I still experience this!

Still happening... this is so annoying.
VS Code v1.36.1

I am setup "editor.formatOnPaste": true, and this is fix my issue. I am use Prettier + ESLint + Airbnb Style Guide

VS Code v1.36.1

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SkeLLLa picture SkeLLLa  路  4Comments

Connorelsea picture Connorelsea  路  4Comments

Glinkis picture Glinkis  路  4Comments

mshehadeh picture mshehadeh  路  4Comments

screendriver picture screendriver  路  4Comments