Code Beautifier tool. block = Alignment

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Code Beautifier tool. block = Alignment

Post 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()))

malleo, caput, bang. Ego, comprehendunt in tempore
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Code Beautifier tool. block = Alignment

Post 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:
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Code Beautifier tool. block = Alignment

Post by Josh »

Without having tried it, I like it because I always try to write my code in a similar way.
Last edited by Josh on Sun May 12, 2019 10:05 am, edited 1 time in total.
sorry for my bad english
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: Code Beautifier tool. block = Alignment

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

Re: Code Beautifier tool. block = Alignment

Post by RSBasic »

Useful tool Image
Image
Image
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Code Beautifier tool. block = Alignment

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Code Beautifier tool. block = Alignment

Post by Mijikai »

Nice idea :D
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Code Beautifier tool. block = Alignment

Post 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 "="].
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: Code Beautifier tool. block = Alignment

Post by Zebuddi123 »

Code Beautifier (wip) https://youtu.be/GPAtQspMSy8
Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Code Beautifier tool. block = Alignment

Post 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
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: Code Beautifier tool. block = Alignment

Post 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
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Code Beautifier tool. block = Alignment

Post 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)
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: Code Beautifier tool. block = Alignment

Post 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. :)
malleo, caput, bang. Ego, comprehendunt in tempore
Post Reply