Code Cleaner

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Code Cleaner

Post 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 :)
Image
Image
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: Code Cleaner

Post 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
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post 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?
Image
Image
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: Code Cleaner

Post 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?
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post 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.
Image
Image
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post by RSBasic »

Code Cleaner 1.0.3 has been released.

Changelog:
  • Bugfix: /SetCursorToTop
  • Bugfix: /SetCursorToBottom
Image
Image
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post 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.
Image
Image
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post 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.
Image
Image
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post by RSBasic »

Code Cleaner 1.0.6 has been released.

Changelog:
  • Bugfix: Umlaut problem with UTF8
Image
Image
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post by RSBasic »

Code Cleaner 1.0.7 has been released.

Changelog:
  • Bugfix: Umlaut problem with UTF8
Image
Image
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Re: Code Cleaner

Post 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
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post 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.)?
Image
Image
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Re: Code Cleaner

Post 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...
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Code Cleaner

Post 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
Image
Image
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Re: Code Cleaner

Post by highend »

Thanks for the addition! Works fine after a few quick tests :mrgreen:
Post Reply