alexanderweiss / nova-prettier

Prettier extension for Nova
MIT License
35 stars 6 forks source link

Format Selection #41

Closed vichudson1 closed 3 years ago

vichudson1 commented 3 years ago

It would be nice to have a menu command to just format selected lines in a file instead of the entire file very time.

alexanderweiss commented 3 years ago

Yes I've seen that in other extensions. I'm happy to look at adding it. How / when do you use it though? I've never understood why I'd want to use it that way.

vichudson1 commented 3 years ago

Sometimes the formatter does weird things to parts of your file so it’s nice to just format it in chunks around that.

Other times you’ve just added new stuff to a file and may have previously mentioned stuff you don’t want broken, so it’s easier to just format the new chunk.

I do a lot of work in Hugo static templates and it can wreak some havoc on the Go bits.

Vic Hudson

On Oct 18, 2020, at 12:48, Alexander Weiss notifications@github.com wrote:

 Yes I've seen that in other extensions. I'm happy to look at adding it. How / when do you use it though? I've never understood why I'd want to use it that way.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

alexanderweiss commented 3 years ago

The latest version v2.0.0 now has a command for this: Editor > Prettier > Format selection. I hope it works well for you!

vichudson1 commented 3 years ago

Hi @alexanderweiss. First let me say thanks for adding this feature. I'm not sure if this is your problem or a Prettier problem but I'm encountering a bug. I'll try to explain and demonstrate as best I can. I'm going to inline all the code examples but if it's helpful you can go straight for my repo. The file we're using for this demo is here.

Here's what I'm seeing when trying to format selection in my project. This is my file.

{{- with .Params.audio.file -}}
    <div class="row">
        <div class="col">
            <div class="single-player" id="single-player">
                <div class="podcast">
                    <div class="podcast__meta">
                        <audio width="100%">
                            <source src="{{ $.Render "audio/audioURL" }}" />
                            Your browser does not support the audio tag.
                        </audio>
                    </div>
                </div>
            </div>
        </div>
    </div>
{{- end -}}

If I try to format everything between the first and last GO elements, {{- with .Params.audio.file -}} to {{- end -}}, I get this after formatting.

<BPGT14EPGT>
    <div class="row">
        <div class="col">
            <div class="single-player" id="single-player">
                <div class="podcast">
                    <div class="podcast__meta">
                        <audio width="100%">
                            <source src="BPGT15EPGT" />
                            Your browser does not support the audio tag.
                        </audio>
                    </div>
                </div>
            </div>
        </div>
    </div>
</BPGT14EPGT>

{{- with .Params.audio.file -}} and {{- end -}} were outside the selection, but still unhelpfully get transformed into <BPGT14EPGT> & </BPGT14EPGT>.

Interestingly if I just format the whole page that doesn't happen. In this case I can just format the whole page, but even with a Go plugin for prettier, also in my repo if it helps, sometimes really bad things happen when formatting the whole page. This is why I originally requested format selection.

Thanks again for the great extension. Let me know if there is anything else I can provide to help or if you'd prefer I copy this into a new issue. :-)

alexanderweiss commented 3 years ago

Hey! Did you figure this out? Sorry, I've been a bit slow. I think this actually would behave the same way in VSCode, etc. no? The extension just passes the start and end positions of your selection and Prettier does stuff based on that.

vichudson1 commented 3 years ago

I have not figured it out. No worries about the slow response. It's been a while since I played with VS Code, but I don't remember ever seeing this issue there. I'll try and get a few moments to try it out there and report back soon.