skip to Main Content

Let’s say there is an if statement like this:

if( a && b )
    c();

Is there a way to auto-format it to having extra spaces around AND (&&) / OR (||) operators using VSCode’s default formatter (editor.formatOnSave)?

if( a  &&  b )
    c();

Most formatting settings I found, except for this one.

2

Answers


  1. VS Code’s default JavaScript and TypeScript formatter come from TypeScript. The closest thing to what you want that I’m aware of that providing is javascript.format.insertSpaceBeforeAndAfterBinaryOperators and typescript.format.insertSpaceBeforeAndAfterBinaryOperators. But I’m pretty sure that only makes one space and not two.

    If you want something relatively vanilla, you could use search and replace as a workaround. Use the search view’s replace feature, turn on regex mode, put s*(&&|||)s* in the search field, and put $1 in the replace field, and click the triple dots to open up the extra menu and put *.ts,*.js in the "files to include" field.

    Login or Signup to reply.
  2. So use the Prittier extension. It will be easy for you. And also you can configure it from the VS Code setting.json according to the languages.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search