notepad4 icon indicating copy to clipboard operation
notepad4 copied to clipboard

Compress Code Whitespace

Open meteorquake opened this issue 4 months ago • 9 comments

There are two forms of compressing whitespace

  1. The current position where all whitespace is compressed (using Alt-W)
  2. The case where white-space of code is compressed but white-space within string quotations is left untouched, unless you've just highlighted fully within a single string in which case method 1

There's a need for this second option. Obviously it can appear under Edit - Selection - 'Compress Code Whitespace' below 'Compress Whitespace'. An example use case is when you highlight some code, Ctrl-J to turn it into a single line, Alt-W to despace that code, thus minimalising it, and paste into a string in a serverside script that serves it up as JS within an overall htm output. For a keyboard shortcut, it would be good if Alt-W applied case 2 and if Alt-W was pressed again without any change of selection it would then apply method 1, because I think generally in a code environment you want either to compress code whitespace or a string's whitespace, but not both at the same time.

d

meteorquake avatar Aug 27 '25 14:08 meteorquake

and paste into a string in a serverside script that serves it up as JS within an overall htm output

Use Tools -> Action on Selection -> CSS/JS/JSON Compress.

Image

zufuliu avatar Aug 28 '25 11:08 zufuliu

That's useful to know, and removes comments too. It was in an unexpected menu location when thinking about trimming spaces!!

What would be really helpful is a shortcut that pulls up a dialog of all commands as a list (whether on menu or not), you type at the top to filter and double-click the one of interest to run it, as this is much faster than looking for something on menus!

meteorquake avatar Aug 28 '25 11:08 meteorquake

The menus were added from issue #614, whole implementation may require external tools.

zufuliu avatar Aug 28 '25 12:08 zufuliu

I've been using this a lot since you mentioned it. The main shortcoming is that the JS line continuation character \ is not handled, so

a="this \
and that";

doesn't become a="this and that"; as a result it has to be handled with a manual find and replace to remove \[newline]

meteorquake avatar Sep 09 '25 12:09 meteorquake

as a result it has to be handled with a manual find and replace to remove [newline]

Implemented by commit 1ab6573e7634c989ba3739282d1cc3e111e1a6c0.

2. The case where white-space of code is compressed but white-space within string quotations is left untouched, unless you've just highlighted fully within a single string in which case method 1

This (determinate string styles in current scheme) can be Implemented by using AllStringStyleMask in EditAutoC.cpp. https://github.com/zufuliu/notepad4/blob/1ab6573e7634c989ba3739282d1cc3e111e1a6c0/src/EditAutoC.cpp#L561-L563

zufuliu avatar Sep 09 '25 13:09 zufuliu

I think additionally since everything else is compressed into a single line, JS backtick-type strings that quote linefeeds literally should normally have their linefeeds converted to \n (the linefeed removed), certainly when 2-byte linefeeds are used in an environment where linefeeds are generic. I suspect it would also help for consistency to do so with 1-byte linefeeds even though it might add a byte, because the number of occurrences would be small. Compression is as much about tidying code into 1 unobtrusive line as it is about reducing every last byte count. I find typical uses where I have a test-environment web page with JS and comments, then compress the code to single line and include in other pages in some fashion. I can see there might be exceptions such as when JS is constructing binary data but I would think it should be a rule of thumb that linefeeds in backticks are not used in constructing binary data. d

meteorquake avatar Sep 10 '25 11:09 meteorquake

That may break some code, e.g. str.replace('\r\n', '<br>').

zufuliu avatar Sep 10 '25 11:09 zufuliu

If I understand you may mean -

`text
text`.replaceAll(linefeed,linefeedcode);

which is probably an edge case due to reliance on the exact newline choice in use, with more reliable alternatives, especially as some scenarios may automatically alter the file's entire set of linefeeds. I think generally it's unfortunate that edge cases often scupper a procedure that's helpful for normal scenarios where the user knows it's not an edge case :)

meteorquake avatar Sep 10 '25 13:09 meteorquake

JS backtick-type strings that quote linefeeds literally should normally have their linefeeds converted to \n (the linefeed removed

UglifyJS (https://www.npmjs.com/package/uglify-js) does this. I'm not going to implement it as line ending conversion is simple:

Image

zufuliu avatar Sep 11 '25 14:09 zufuliu