volarjs / services

MIT License
134 stars 29 forks source link

Any reason why newlines are added when copy pasting text? #15

Closed braincomb closed 1 year ago

braincomb commented 1 year ago

I can see this behavior in both prettier and prettyhtml plugins:

newText = '\n' + newText.trim() + '\n';

Is there a technical reason why this is done? When copying and pasting text in .vue files, this creates unnecessary newlines which is frankly quite annoying.

Genuinely curious.

Thanks!

johnsoncodehk commented 1 year ago

In my memory prettier / prettyhtml line rules can't work pretty with upper <template></template>, but I'm not sure currently. Could you share your breaking case?

braincomb commented 1 year ago

Hi @johnsoncodehk ,

Here're a few GIFs demonstrating the issue, copy pasting creates new lines around the line being pasted in.

demo1 demo2

Edit: after some experimenting with different versions, I can see that newText = '\n' + newText.trim() + '\n'; is also present in 1.0.4, but I'm not getting any issues with extra newlines as above.

patarapolw commented 1 year ago

A possible fix is ignoring range parameter altogether; using full document.getText() instead.

    return {
        format(document, _, opts) {
            ...

            const fullText = document.getText();
            ....
            return [
                {
                    range: {
                        start: document.positionAt(0),
                        end: document.positionAt(fullText.length),
                    },
                    newText: newText,
                },
            ];
        },
    };
braincomb commented 1 year ago

Thank you for looking into this @patarapolw . I installed version 1.1.5 and can confirm the issue has been resolved.