Skip to content

A way to keep initial indentation when formatting a "snippet" - or part of a file #1324

@pmorch

Description

@pmorch

Use case: I'm using Neoformat for vim, where I select a snippet (e.g. the insides of an if-statement), and run it through prettier. I'd like to keep the indentation of the first line and reformat the snippet accordingly. Assume this code:

    ....
    if (true) {
      doSomething(); doSomethingElse();
    }
    ....

Assume the doSomething(); doSomethingElse(); line starts on column 7 (First column is column 1).

Expected result: doSomething() starts on column 7 after being run through prettier and doSomethingElse() also starts on column 7 on the next line.

Actual result: Both lines start on column 1, not column 7.

Workaround

I can create a wrapper script that checks the amount of indentation of the first line. Subtract this number from 80 and give that to --print-width. Run prettier, and subsequently re-indent the entire prettier output that amount of spaces.

But it would be handy if prettier would do that for me - perhaps behind an option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions