BeautifyCode (tidyup)

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

BeautifyCode (tidyup)

Post by Zebuddi123 »

Hi to All Another part of something i`m working on. This part help`s tidy up my lazy coding style, maybe of use to some. Modifies spacing around , + / ( ) ; { } etc.

Still WIP but this can be made into a stand alone IDE tool. Needs testing to make sure there are no syntax errors in the code and the code compiles correctly.

As is, is set to run in compile or debug mode and opens a tempfile so as not to alter any original code.

:?: How to send crtl+a + ctrl+i to the ide :D anyone please :!:

zebuddi. :)

Before:
Image

After:
Image

Code: Select all

Procedure.s BeautifyCode(Source.s)
	Protected q$, t.i, k, t$=Source.s, i.i, delimiter.s, dp.s
	For i=1 To 16
		Select i
			Case 1
				delimiter=";"
				dp=" "+delimiter+" "
			Case 2
				delimiter="("
				dp=delimiter+" "
			Case 3
				delimiter=")"
				dp=" "+delimiter+" "
			Case 4
				delimiter=","
				dp=" "+delimiter+" "
			Case 5
				delimiter=":"
				dp=" "+delimiter+" "
			Case 6
				delimiter="<>"
				dp=" "+delimiter+" "
			Case 7
				delimiter="<="
				dp=" "+delimiter+" "
			Case 8
				delimiter=">="
				dp=" "+delimiter+" "
			Case 9
				delimiter="=>"
				dp=" "+delimiter+" "
			Case 10
				delimiter="=<"
				dp=" "+delimiter+" "
			Case 11
				delimiter="+"
				dp=" "+delimiter+" "
			Case 12
				delimiter="|"
				dp=" "+delimiter+" "
			Case 13
				delimiter="="
				dp=" "+delimiter+" "
			Case 14
				delimiter="; {"
				dp=";{ "
			Case 15
				delimiter="; }"
				dp=";{ "
			Case 16
				delimiter=") \"
				dp=")\"
		EndSelect
		t=CountString(t$, delimiter)+1
		For k = 1 To t
			If k<t
				q$+Trim(StringField(t$, k, delimiter))
				q$+dp
			Else
				q$+Trim(StringField(t$, k, delimiter))
			EndIf
		Next
		t$=q$ : q$=""
	Next
	ProcedureReturn t$
EndProcedure

 f$=OpenFileRequester("","","", 0)
 
 Global j$

If ReadFile(0,f$)
	While Eof(0)=0
		a$=Trim(ReadString(0, ReadStringFormat(0)))
		a$=RemoveString(a$, Chr(10)+Chr(13))
		a$=RemoveString(a$, Chr(9))
		t$+a$+#CRLF$
	Wend
	CloseFile(0)
	
	j$= BeautifyCode(t$)
	SetClipboardText(j$)
	
	If CreateFile(0, f$)
		WriteString(0, j$)
		CloseFile(0)
		RunProgram(f$)	
	Else
		MessageRequester("file error",GetLastErrorAsText(GetLastError()))
	EndIf
EndIf
End
Last edited by Zebuddi123 on Wed Jan 15, 2014 1:12 am, edited 1 time in total.
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: BeautifyCode (tidyup)

Post by skywalk »

Zebuddi123 wrote::?: How to send crtl+a + ctrl+i to the ide :D anyone please :!:
Code snippet...I use Infratec's SendKeys()

Code: Select all

  Protected.i np = CountProgramParameters()
  If np
    Protected.i hSci = Val(GetEnvironmentVariable("PB_TOOL_Scintilla"))
    Protected.i hIDE = Val(GetEnvironmentVariable("PB_TOOL_MainWindow"))
    If hSci
      Protected.s r$
      Select UCase(ProgramParameter())  ; 0-based index
      Case "COPYALL"    ; Arguments: COPYALL, Shortcut = [Ctrl+Shift+k]
        SendMessageTimeout_(hSci,#SCI_SELECTALL,0,0,#SMTO_ABORTIFHUNG,#SCI_TMO_MS,@np)
        Delay(10)
        win_SendKeys(hIDE, #NULL$, "{CONTROLDOWN}c{CONTROLUP}")
      Case "INDENTALL"  ; Arguments: INDENTALL, Shortcut = [Ctrl+Shift+i]
        SendMessageTimeout_(hSci,#SCI_SELECTALL,0,0,#SMTO_ABORTIFHUNG,#SCI_TMO_MS,@np)
        Delay(10)
        win_SendKeys(hIDE, #NULL$, "{CONTROLDOWN}i{CONTROLUP}")
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: BeautifyCode (tidyup)

Post by Zebuddi123 »

Thanks skywalk :) was just giving up with a headache from banging my head against the screen. About 10 pbide crashes, was expecting a BSOD next :lol:

Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
Post Reply