简体   繁体   中英

I wish to know how I can prevent Prettier to format my js code in this strange form

I started to use Prettier inside my Microsoft Visual Studio Code (MVSC), but, when formatting a javascript file, I get some strange formatting.

For example, if I have this code:

    if ($("#epgprogramdescription" + channelListIndexOffset + ":nth-of-type(" + (programListIndexOffset + 1) + ")" ).length) {
        $("#epgprogramdescription" + channelListIndexOffset + ":nth-of-type(" + programListIndexOffset + ")").removeClass("epgtablerowselected");
        $("#epgprogramdescription" + channelListIndexOffset + ":nth-of-type(" + (programListIndexOffset + 1) + ")").addClass("epgtablerowselected");
    } else {
        programListIndexOffset--;
    }

It get it formatted as:

    if (
        $(
            "#epgprogramdescription" +
                channelListIndexOffset +
                ":nth-of-type(" +
                (programListIndexOffset + 1) +
                ")"
        ).length
    ) {
        $(
            "#epgprogramdescription" +
                channelListIndexOffset +
                ":nth-of-type(" +
                programListIndexOffset +
                ")"
        ).removeClass("epgtablerowselected");
        $(
            "#epgprogramdescription" +
                channelListIndexOffset +
                ":nth-of-type(" +
                (programListIndexOffset + 1) +
                ")"
        ).addClass("epgtablerowselected");
    } else {
        programListIndexOffset--;
    }

Any way to configure Prettier to maintain the original formatting?

Thanks.

Prettier gonna always try to give line break your code if it exceeds your print width configuration.

You can change that by accessing vscode settings

  1. Go to File -> Preferences -> Settings
  2. Search for "Prettier"
  3. Look for configuration called "Prettier: Print Width"
  4. And change it to suit your needs !

Give it bigger value if you want your code to stay in the same line. Prettier only break your code into new lines if it exceeds the given configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM