Page 1 of 2

Code Beautyfier for the IDE (also useful for jaPBe)

Posted: Sun Jan 06, 2008 5:17 am
by inc.
While working on the OOP parser which outputs the converted code also in a beautyfied way, I thought this also would be nice as a standalone plugin.

Its a Plugin primary build for the IDE but it also does it's job with jaPBe properly.

The beautyfing will be performed on the selected code area.

It also does handle code to be indented within Macros properly like ...

Code: Select all

Macro TestCode()
  If x < 0
    If r = j
      z = x
EndMacro
And ... it does re-arrange the tokens of the selected code.
Like:

Code: Select all

Procedure.l Functions (a,b,c, e.l, string="default" ) 
  Protected j.l =0 
  if a+b /c= gVar 
   string="hello" 
  Endif 
EndProcedure:FreeMemory( *mem) 

#CONSTANT_A = $FF 
#ERROR = 0 
#DELETE_KEYWORD = "del" 
gets polished to ...

Code: Select all

Procedure.l Functions(a, b, c, e.l, string = "default") 
  Protected j.l = 0 
  if a + b / c = gVar 
   string = "hello" 
  Endif 
EndProcedure : FreeMemory(*mem) 


#CONSTANT_A     = $FF 
#ERROR          = 0 
#DELETE_KEYWORD = "del"
Also all unneeded white spaces at the end of codelines will be deleted.

To apply this function

The code is a simple merge of two modules: The code from Remi meiers lexer in combination with a Tokenarray and the Indentation tool of HeX0R.
http://www.purebasic.fr/german/viewtopic.php?t=8691
http://www.purebasic.fr/german/viewtopic.php?t=10418

In case of jaPBe Im forced to use "%FILE" instead of the argument "%TEMPFILE" as used in the IDE. This makes jaPBe overwriting the existing original code, so you can decide by altering the setup of the tool if you first want to try by letting the plugin writing the cleaned up code into a new file instead of overwriting the existing one.

Here's a howTo install of the pluigin for the IDE:

Image

Now there is a new syntax for the arguments used.
When using this plugin with the IDE the argument "%TEMPFILE" is still obligatory. Beside Indentation there can be done a codebeautifying as well, just add the argument -b and for a proper alignment of PB contants Lines just add -c.
You also can set up individual entries in the tools section where only the indentation will be applied, or combined with -b, or even combined with -b and -c.

The argument syntax "%TEMPFILE" "%SELECTION" "%BEAUTIFY" will be no longer supported.


And for jaPBe:

Image

In case of jaPBe instead of just "%TEMPFILE" you HAVE TO use "%FILE" "%SCI and if wanted followed by -b and -c (see above). Also here you can setup individual entries in the jaPBe's tools section using individual arguments.
As said, be aware the in the example above jaPBe does reload the cleaned up code into the current source. As this state actually is beta, you should work with copies for testing purposes or let the tool reload the code into a new source.
Means: This is beta, so no warranty.

Update: http://www.file-upload.net/download-600 ... y.exe.html
Here still a link to the old version - just in case.




An alternate version where the operators wont be separated by whitespaces:
http://www.file-upload.net/download-600 ... s.exe.html
Here as well a link to the old version.

Code: Select all

Procedure.l Functions (a,b,c, e.l, string="default" )
  Protected j.l =0
  if a+ b /c= gVar
   string= #PB_Compiler_Home + "File.pbi"
  Endif
EndProcedure:FreeMemory( *mem)
... becomes ...

Code: Select all

Procedure.l Functions(a, b, c, e.l, string="default")
  Protected j.l=0
  if a+b/c=gVar
   string=#PB_Compiler_Home+"File.pbi"
  Endif
EndProcedure : FreeMemory(*mem)


Changelog:

09.01.2007:
- New Syntax of arguments
- Selection now also supported for jaPBe
- Constand assignments now can be aligned horizontally to "=" (like in jaPBe's Select+ALT+STR+TAB)

06.01.2007:
-Bugfix if "If \number" was used, no whitespace Separator has been applied.

Posted: Sun Jan 06, 2008 5:26 am
by Mistrel
I prefer to remove white space from between my operators in the exact opposite way of your beautifier. :roll:

Thanks for making this tool though. I know that a lot of people here do like that style.

Posted: Sun Jan 06, 2008 5:27 am
by inc.
Then, just deactivate the beautfy function.

EDIT: If other beautifying styles are wanted, maybe give me an example where finally individual styles can be archived by using a prefernce file.

Posted: Sun Jan 06, 2008 1:14 pm
by inc.
Added a version to the first post where Operators won't be whitespace-separated.

Posted: Tue Jan 08, 2008 4:07 am
by yrreti
Thanks inc.

Nice programs.

Posted: Tue Jan 08, 2008 10:27 pm
by Henrik
Many Thanks inc. 8) :D

Best Regards Henrik.

Posted: Wed Jan 09, 2008 10:35 am
by inc.
Update.
See changelog.

Posted: Fri Jan 11, 2008 9:36 am
by Amundo
Very nice inc.! Beautify-full :D

Any chance of indenting StartDrawing/StopDrawing with optional assignment of the returned handle (e.g. hdc = StartDrawing())

The more whitespace, the better I say. While we're on the whitespace track - any chance of adding spaces after/before brackets "()"? Or the source code? Or an INI file with which options to enabledisable?

Cheers inc.!!!!


P.S.

Code: Select all

	Define.d ang_div =(#PI / 45)
Brackets next to equals sign throws it off.

Posted: Fri Jan 11, 2008 9:55 am
by inc.
Hi Amundo,

as Startdrawing() isn't a Keyword but a command, so indentation is not planned on this.
But in future this Plugin will check the IDE's (and also jaPBe's, when providing EnVars while compiling will be officially supported by jaPBe) preferences for user folding keywords, where these also will be added to the indentation. So thats how you could add individual Tokens to the indentation list.

Whitespaces before and after brackets in general would mess up things like

Code: Select all

Procedure TestFuntion ()

NewList LongTypeList ()
AddElement ( LongTypeList () )
LongTypeList () = 100
see the point? ;-)

Posted: Fri Jan 11, 2008 10:03 am
by Amundo
I was just thinking about spaces after opening / before closing brackets, but that's okay.

Was editing my original post (about a bug) when you posted :)

Posted: Sun Jan 20, 2008 9:03 pm
by iNX
I'm using this tool with the original PB IDE. Nice job, thank you!
While i was using it, i've noticed something strange:

Start with a "beautified" source, modify a line so that it will be modified by the tool, then select some lines which are already beautified (don't select the modified one) and execute the beautifier. Nothing will be modified and this is ok.

Then if you run the tool again with nothing selected, the "bad" line won't be modified, while this time it should be.

The arguments are: "%TEMPFILE" -b -c and actually i can't figure out whether the problem lies in your tool or in the ide itself, since i don't understand how the information about the selected lines is passed to codebeautifier.

Posted: Sun Jan 20, 2008 9:29 pm
by inc.
... Then if you run the tool again with nothing selected ...
Sorry Ive forgot to mention in here: You must select an area to be beautyfied, like in jaPBes's STRG+TAB where you also actually do indent the selected area.

Posted: Sun Jan 20, 2008 9:55 pm
by iNX
inc. wrote:
... Then if you run the tool again with nothing selected ...
Sorry Ive forgot to mention in here: You must select an area to be beautyfied, like in jaPBes's STRG+TAB where you also actually do indent the selected area.
Ah, ok, i see.. i've looked into it further and i think i was misleaded by the fact that it indents the line where the cursor is, even if it's not selected. So i had wrongly assumed it was working also with nothing selected.

Very useful tool! thanks again.

Posted: Sat Jul 12, 2008 5:26 pm
by loadstone
"%TEMPFILE" "%SELECTION" "%BEAUTIFY"

The version from pureare.net downloaded, if that is the latest version ?

Posted: Wed Jul 16, 2008 11:26 am
by Perkin
This works pretty well, however one 'hitch' is that comments get indented to the level of any code, is there an easy way to stop this happening.
Thanks

<edit>
Also if '>=' and '<=' could not be spaced into '> =' & '< ='
</edit>