Goz3rr / vscode-glualint

glualint for vscode
https://marketplace.visualstudio.com/items?itemName=goz3rr.vscode-glualint
MIT License
7 stars 4 forks source link

Do not attempt to format empty selection #15

Closed ZehMatt closed 9 months ago

ZehMatt commented 1 year ago

This solves the issue, range is not undefined for when there is no selection but passes an empty one. I'm not sure if this is some API change from VSCode but range seems to be never undefined on my end.

Rewrote the PR, it now skips the formating if the range is empty or the selected text is nothing but whitespaces.

ZehMatt commented 1 year ago

This doesn't seem to fully fix it, despite not having anything selected the range claims that I have.

Edit: I'm slightly confused about the range input, having the cursor here: image gives me a range of:

Start { Line = 46, Character = 0 }
End { Line = 46, Character = 4 }

rather than

Start { Line = 46, Character = 4 }
End { Line = 46, Character = 4 }
ZehMatt commented 1 year ago

I've added a second commit that trims the selection to see if its empty and then uses the whole document. It's a bit of a work-around but I think its reasonable to format the entire document whenever nothing is actually selected.

Goz3rr commented 1 year ago

Thanks for the PR!

I don't think there was an API change, range is just nullable so the formatDocument method can be called from both the handlers for "Format Selection" and "Format Document".

You're probably running the "Format Selection" with no selection, so glualint returns an empty string which makes the extension think it failed. vscode provides a "Format Document" action as well which formats the entire document.

That raises the question what the expected behavior should be when you call "Format Selection" with no selection. I think formatting the entire document that case is not the right action here, it should probably do nothing since the user specifically asked to format an empty selection.

ZehMatt commented 1 year ago

Thanks for the PR!

I don't think there was an API change, range is just nullable so the formatDocument method can be called from both the handlers for "Format Selection" and "Format Document".

You're probably running the "Format Selection" with no selection, so glualint returns an empty string which makes the extension think it failed. vscode provides a "Format Document" action as well which formats the entire document.

That raises the question what the expected behavior should be when you call "Format Selection" with no selection. I think formatting the entire document that case is not the right action here, it should probably do nothing since the user specifically asked to format an empty selection.

You are right, I ordinarily use Visual Studio where formatting is Ctrl+K+F where in VSCode this is just doing the selection, I later realized my wrong doing. As for the PR, I'm also not so sure where to go from here, the range specified is quite strange for when there is no actual selection. I can rewrite the PR to just bail if the selection is empty rather than formatting the entire document.

Goz3rr commented 1 year ago

Yeah, I think that's the sensible option

ZehMatt commented 1 year ago

Adjusted the changes.

Goz3rr commented 9 months ago

Thanks!