Page 1 of 1

Code Cleaner

Posted: Sun Jun 23, 2019 12:50 pm
by RSBasic
Hello :)

With this tool you can clean up the selected or full source code in the editor.
What is cleaned can be defined with the parameters.
But there are also other functions, e.g. the conversion of all characters into upper case or lower case letters.

The following parameters can be passed:
  • /OnlySelectedCode
  • /RemoveEmptyLines
  • /RemoveDebugLines
  • /RemoveComments
  • /RemoveDoubleSpaces
  • /RemoveAllSpaces
  • /RemoveSpacesAtEndOfLines
  • /RemoveSpacesAtStartOfLines
  • /ConvertTextToLowerCaseLetters
  • /ConvertTextToUpperCaseLetters
  • /Fold
  • /Unfold
  • /ConvertTabToSpace
  • /ConvertSpaceToTab
  • /SetCursorToTop
  • /SetCursorToBottom
  • /KeepCursorAndFolds
You can combine several parameters or add the tool several times.

If you want more parameters, post here.

Installation:
Image

Download: https://www.rsbasic.de/downloads/downlo ... leaner.zip
Image

I would be very pleased about feedbacks, improvement suggestions, error messages or wishes. Thanks :)

Re: Code Cleaner

Posted: Sun Jun 23, 2019 11:14 pm
by forumuser
Imagine you have this:

Code: Select all

  EndIf
  
  
  
  
  ; Restart ourselves if necessary (UAC protected directory)
Between the EndIf and the comment line are 4 lines, all of them with two spaces
If I select these 4 lines and execute code cleaner with /RemoveSpacesAtEndOfLines
I would expect that I still have the EndIf and the comment line and that all 4 lines in between do not have any spaces
anymore but... they still do

Re: Code Cleaner

Posted: Mon Jun 24, 2019 3:25 pm
by RSBasic
Code Cleaner 1.0.1 has been released.

Changelog:
  • Added: "/Fold" to collapse.
  • Added: "/Unfold" to unfold.
  • Added: "/ConvertTabToSpace" (The number of spaces is determined from the PB settings file.)
  • Added: "/ConvertSpaceToTab" (The number of spaces is determined from the PB settings file.)
  • Added: "/SetCursorToTop" to set the cursor to the start.
  • Added: "/SetCursorToBottom" to set the cursor to the end.
  • Changed: The order of the parameters is determined and all parameters are executed in this order.
  • Changed: If nothing was selected, then always the complete code is processed.

@forumuser
When I test your code, all spaces of each line including all spaces of the empty lines are removed.
Do you have a pb file for me?

Re: Code Cleaner

Posted: Mon Jun 24, 2019 4:14 pm
by forumuser
@RSBasic

No, sorry, commercial project...

Had some more time.
Again, only the 4 inner lines (and their spaces are selected)

Arguments: /RemoveEmptyLines /RemoveSpacesAtEndOfLines
Executing it, nothing is edited

but without /RemoveEmptyLines:
Spaces are stripped

so it seems it doesn't like the combination of the two options as
long as only lines without any other content than spaces are selected?

Re: Code Cleaner

Posted: Mon Jun 24, 2019 7:18 pm
by RSBasic
Code Cleaner 1.0.2 has been released.

Changelog:
  • Optimized: Improves performance during folding/unfolding.

@forumuser
Hm, my test code:

Code: Select all

  EndIf 
 
          
  
                           
  ; Restart ourselves if necessary (UAC protected directory)               
When I run /RemoveSpacesAtEndOfLines:

Code: Select all

  EndIf




  ; Restart ourselves if necessary (UAC protected directory)
When I run "/RemoveEmptyLines /RemoveSpacesAtEndOfLines":

Code: Select all

  EndIf
  ; Restart ourselves if necessary (UAC protected directory)
Both versions are correct here. Tested with plain text and UTF8.
It's the question of what's different in your code.

Re: Code Cleaner

Posted: Mon Jun 24, 2019 10:05 pm
by RSBasic
Code Cleaner 1.0.3 has been released.

Changelog:
  • Bugfix: /SetCursorToTop
  • Bugfix: /SetCursorToBottom

Re: Code Cleaner

Posted: Tue Jun 25, 2019 7:26 pm
by RSBasic
Code Cleaner 1.0.4 has been released.

Changelog:
  • Added: /OnlySelectedCode: Only the selected code is edited with this parameter. If no code has been selected, nothing is edited.

Re: Code Cleaner

Posted: Thu Jun 27, 2019 2:42 pm
by RSBasic
Code Cleaner 1.0.5 has been released.

Changelog:
  • Added: /AllTabs: All tabs can be edited with this parameter. The parameter "/OnlySelectedCode" is ignored.

Re: Code Cleaner

Posted: Wed Jul 03, 2019 1:50 pm
by RSBasic
Code Cleaner 1.0.6 has been released.

Changelog:
  • Bugfix: Umlaut problem with UTF8

Re: Code Cleaner

Posted: Fri Jul 05, 2019 7:39 pm
by RSBasic
Code Cleaner 1.0.7 has been released.

Changelog:
  • Bugfix: Umlaut problem with UTF8

Re: Code Cleaner

Posted: Fri Oct 06, 2023 5:06 pm
by highend
Great tool, thanks for sharing it!

I'm invoking it with just /RemoveSpacesAtEndOfLines and it works fine for the whole document.
One thing is bothering me, though...

The current view (which lines of the code are visible) and cursor position is reset. Would it be possible to introduce a new parameter /KeepCursorPosition | /RestoreLastView so that after doing all cleaning stuff it restores the last cursor position and the range of visible lines?

Thank you

Re: Code Cleaner

Posted: Sat Oct 07, 2023 3:49 pm
by RSBasic
I read Scintilla's documentation, but I couldn't find anything.

@all
Does anyone know if there is a Scintilla function/constant to save and restore the current view setting (folds etc.)?

Re: Code Cleaner

Posted: Sat Oct 07, 2023 4:42 pm
by highend
I haven't read anything about folds (don't use them myself) in the docs.
For unfolded code there are:
#SCI_GETFIRSTVISIBLELINE + #SCI_SETFIRSTVISIBLELINE

Ofc depending on what operation is done (e.g. removing of comment lines), you'd probably needs some counters of how many lines have been removed to adapt the first visible line value...

And there are:
#SCI_GETCURRENTPOS
#SCI_LINEFROMPOSITION
and
#SCI_GOTOLINE

To put the cursor back into the last line the caret was in before the changes (- values for adaption like above).

Seems (depending on the operation parameter) there would be some work involved to get this right.

You don't need to do it. I've found some code here: viewtopic.php?p=518534#p518534
that allows me to do at least the removal of trailing whitespaces and restoring the last view port...

Re: Code Cleaner

Posted: Tue Oct 10, 2023 8:32 pm
by RSBasic
Code Cleaner 1.0.8 has been released.

Changelog:
  • Added: /KeepCursorAndFolds: Cursor position and all folding states are retained.
  • Removed: /AllTabs: In rare cases, the PB-IDE may crash if multiple tabs are edited by this tool at the same time.

highend wrote: Fri Oct 06, 2023 5:06 pm The current view (which lines of the code are visible) and cursor position is reset. Would it be possible to introduce a new parameter /KeepCursorPosition | /RestoreLastView so that after doing all cleaning stuff it restores the last cursor position and the range of visible lines?
Done

Re: Code Cleaner

Posted: Wed Oct 11, 2023 11:11 am
by highend
Thanks for the addition! Works fine after a few quick tests :mrgreen: