Page 1 of 1

Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 8:38 am
by Zebuddi123
Hi to All. Here is a small IDE tool part of a code beautifier (wip) mostly done. I often get code from the forum/Git ect were the code formatting is not in my style, so now a couple of mouse clicks ect all formatted the way I like it.

This part as you can see from the images Aligns the = in a block via finding the space required to align the block. Hope some find it useful

Zebuddi. :)

Simply set up as a tool no params
1. select the group to align, RCM select copy, select tool name from menu or click tool icon if set up that way and the paste to the already highlighted block.
Alignment is mostly instantaneous, error msg's and beep if no alignment required or no = symbols found

Image

Image

On a side note of all the programming languages I have and keep playing with! I always come back here and to pb. I was going to try something like this in C++ but being 58 it's a bit late in life. :shock: :P

Code: Select all

Procedure.s AlignEqualsInGroup(sGroup.s)
	EnableExplicit	
	Protected iRegex = CreateRegularExpression(#PB_Any, "(.+\=)((?<=\=).*)", #PB_RegularExpression_MultiLine)
	
	If MatchRegularExpression(iRegex, sGroup)
		Protected iDiffFlag.i, iNbr.i, iIndex.i, iMaxSpace.i, sReplacementString.s, sReturnString.s, Dim t$(0)
		iNbr = ExtractRegularExpression(iRegex, sGroup, t$())	
		; *** Init 1st Element for Comparison
		iMaxSpace = FindString(t$(0), Chr(61))
		;
		For iIndex = 1 To (iNbr-1) ; Start Loop at 2nd Element
			If FindString(t$(iIndex), Chr(61)) > iMaxSpace
				iMaxSpace = FindString(t$(iIndex), Chr(61))
				iDiffFlag + 1
			EndIf
		Next
		
		If Bool(iDiffFlag) > 0  ; If previously done 0r nothing to change
			For iIndex = 0 To iNbr-1
				If  ExamineRegularExpression(iRegex, t$(iIndex))
					NextRegularExpressionMatch(iRegex)
					sReplacementString = Left(RegularExpressionGroup(iRegex, 1),Len(RegularExpressionGroup(iRegex, 1))-2)  + 
					                     Space(iMaxSpace - (Len(RegularExpressionGroup(iRegex, 1))-2)) + 
					                     Chr(61)	+ RegularExpressionGroup(iRegex, 2)
					sReturnString + sReplacementString
				EndIf	
			Next	
		Else			; iDiffFlag = 0 Nothing to Align
			MessageBeep_(1000)
			MessageRequester("Equals Alignment ERR", "Nothing to Align")
		EndIf
		
		;{ *** CleanUp 
		FreeRegularExpression(iRegex)
		FreeArray(t$())
		;}
		ProcedureReturn sReturnString
	Else
		MessageBeep_(1000)
		MessageRequester("Equals Alignment ERR", "No Equals Statements Found")
		ProcedureReturn #Null$
	EndIf
	DisableExplicit
EndProcedure

SetClipboardText(AlignEqualsInGroup(GetClipboardText()))


Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 9:50 am
by Marc56us
Very good tool. Adopted. 8)

:idea: Would it be possible to align also on the right (i.e. put only one space after the sign = if there are several)

I'm also trying to simplify so that the IDE tool menu uses the %SELECTION variable to avoid a cut and paste, but for the moment I can't do it.

I dream that one day CTRL + I will be able to do this type of alignment

:wink:

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 9:59 am
by Josh
Without having tried it, I like it because I always try to write my code in a similar way.

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 10:02 am
by Zebuddi123
Hi Marc56us yes I`m working on the options gui for the Code Beautifier, as you can imagine there will be quite a few options. This will run on 1st use/or change preferences, which obviously your seleted prefs will be saved and as your style in the future etc

Then it with be just a click away, 90% of the beautifier is done needs tweaking 5% of Gui lol.
I'll do a vid capture of the CB in action, You or any of the forum members are most welcome to change make addtions and post the code in the same topic. Also the Cb code when I have finished and published.

Zebuddi. :)

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 12:11 pm
by RSBasic
Useful tool Image

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 4:21 pm
by Michael Vogel
Looks fine, but a first check (copied and pasted the code from above) resulted in an error "negative Number for Space()' when interpreting the regex group containing the source code defining the regular expression...

But it's a cool idea and I'll do some changes to keep the equal sign also on the left side (maybe even with no space gap) and peek for the tab settings in the IDE (replace spaces by real tab). This won't be a big deal when using console fonts but may be tricky for proportional fonts.

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 4:30 pm
by Mijikai
Nice idea :D

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 6:02 pm
by skywalk
Interesting but I often search my variables for assignment?
"MyVar ="
If they are indented, I am forced to search for "MyVar" only.
Or write some Tool code to scan "MyVar" + [next non-space character is "="].

Re: Code Beautifier tool. block = Alignment

Posted: Sun May 12, 2019 11:22 pm
by Zebuddi123
Code Beautifier (wip) https://youtu.be/GPAtQspMSy8
Zebuddi. :)

Re: Code Beautifier tool. block = Alignment

Posted: Mon May 13, 2019 12:00 pm
by HeX0R
I don't want to hijack your thread, just to let you know, that something like this existed already since 2006(!) :wink: :
https://www.purebasic.fr/german/viewtop ... 11&t=10418

Re: Code Beautifier tool. block = Alignment

Posted: Mon May 13, 2019 1:35 pm
by Zebuddi123
Hi HeXOR now it doesn't suprise me some one has already done this :D

Just a shame I had not found this myself. Just downloaded Compliled bla bla bla ect work`s a treat (Great) so thank you for pointing it out and ill be examining your code :shock: :D :D :D

Zeduddi. :)

P.S HeXOR code should be made a Sticky Mods. :D :D :D :D

Re: Code Beautifier tool. block = Alignment

Posted: Mon May 13, 2019 6:44 pm
by ar-s
Very usefool thanks.
In that exemple, the first = with the "if" is not align to the others when i use the tool.

Code: Select all

    If k(name) = GetGadgetText(#LISTKEY)
      New.info\Name = k(name)
      New.info\Main = k(Main)
      New.info\OrderName = k(OrderName)
      New.info\COMMAND = k(COMMAND)
      New.info\IcoPath = k(IcoPath)
      New.info\Position = k(Position)

Re: Code Beautifier tool. block = Alignment

Posted: Tue May 14, 2019 9:29 am
by Zebuddi123
Hi ar-s if I`m understanding you correctly? There is no sudo intelligence in the alignment tool it will align all = in a given block regardless of tabbed indentation. I thought it was better this way and quicker for my uses at the moment. But I would point other forum users to take a look at HeXOR`s code :) aswell.

Zebuddi. :)