PBTool C Style format-er for xxxxgadgets(

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

PBTool C Style format-er for xxxxgadgets(

Post by Zebuddi123 »

Hi to All if you like the C style formatting for xxxgadget( or openwindow( etc this tool will format

Code: Select all

ListIconGadget(5, 170, 50, 265, 200, "Column 1", 131)

to

Code: Select all

ListIconGadget(5,
	               170,
	               50,
	               265,
	               200,
	               "Column 1",
	               131); End_ListIconGadget


Takes 2 params in the tools setup %FILE %TEMPFILE as arguments, it will open another tab in the pbide with a filename CF_originalfilename.pb etc

1 from the tab (source code page) you want formatting just select the tool after setup and all done.

Its best to select all and Ctrl+i to auto indent for correct alignment.

I would like to send Ctrl+i to the ide to automatically indent the code after opening the file in a new tab. do`s anyone know how to sent this the ide ?

Zebuddi. :)

Code: Select all

Global regex.i = CreateRegularExpression(#PB_Any, "(([BCDEFHILMOPSTW]{1}).{3,19}(Gadget\s{0,9}\(.+\))|OpenWindow\(.*\)+)") 
; matchs all pb gadget`s and OpenWindow(

Global NewList file_lines.s() ; lines of the source code

Global Dim tmp$(0)

Global PbIdeHandle = Val(GetEnvironmentVariable("PB_TOOL_MainWindow"))
Global ScintillaHandle = Val(GetEnvironmentVariable("PB_TOOL_Scintilla"))

Procedure.s GetScintillaText() 
	
	; get the source code for the pbide  thanks kiffi:)
	
	Protected ReturnValue.s
	Protected length
	Protected buffer
	Protected processId
	Protected hProcess
	Protected result
	Protected  ScintillaHandle = Val(GetEnvironmentVariable("PB_TOOL_Scintilla"))
	
	If ScintillaHandle = 0 : ;End 
	EndIf
	
	length = SendMessage_(ScintillaHandle, #SCI_GETLENGTH, 0, 0) 
	If length
		length + 2
		buffer = AllocateMemory(length)
		If buffer   
			SendMessageTimeout_(ScintillaHandle, #SCI_GETCHARACTERPOINTER, 0, 0, #SMTO_ABORTIFHUNG, 2000, @result)
			If result
				GetWindowThreadProcessId_(ScintillaHandle, @processId)
				hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, processId)
				If hProcess
					ReadProcessMemory_(hProcess, result, buffer, length, 0)   
					ReturnValue = PeekS(buffer, -1, #PB_UTF8)
				EndIf
			EndIf
		EndIf
		FreeMemory(buffer)
	EndIf
	
	ProcedureReturn ReturnValue  ;  text from the source code
EndProcedure

Procedure cleanup()
	; garbage collection
	FreeList(file_lines())
	FreeRegularExpression(regex)
	FreeArray(tmp$())
EndProcedure

Procedure.s mod_string(string.s)
	
	; process the string seperating each param
	; onto a new line and padding the newline 
	; to match the position of the first param 
	; as in C style 
	Protected nbr.i, pos.i, i.i, pp$, r$ 
	nbr=CountString(string,",")
	pos=FindString(string,"(")
	
	Dim b$(nbr)
	For i=0 To nbr
		b$(i)=Trim(StringField(string,i+1,",")) ; grab each token into an array
	Next
	
	pp$=LSet(pp$,pos," "); pad for the string to match the first param on the above line
	
	For i=0 To nbr ;loop through and modify the existing sting on to sererate lines as in C 
		Select i
			Case 0
				r$+b$(i)+","+#CRLF$
				Debug r$
			Case 1 To (nbr-1)
				r$+pp$+b$(i)+","+#CRLF$
				Debug r$
			Case nbr
				r$+pp$+ReplaceString(b$(i),"|"," |"+#CRLF$+pp$)+"; End_"+Left(string,FindString(string,"(")-1)
				Debug r$
		EndSelect	
	Next
	
	If r$>""
		FreeArray(b$())
		ProcedureReturn r$
	EndIf
EndProcedure

Procedure.s  tidystring(string.s)
	
	; removes all white spaces from the string
	; but leave any strings in quote marks untouched
	
	Protected x.i, nbr.i
	nbr=CountString(string,Chr(34))
	; if no quote marks remove all white spaces 
	If nbr=0
		string=ReplaceString(string,Chr(32),"")
	Else
		spos=FindString(string,Chr(34),1)
		;find last quote position
		For i=1 To nbr
			epos=FindString(string,Chr(34),epos+1)
		Next
		;process white spaces before first quoted strings
		For i=1 To spos-1
			If Mid(string,i)=Chr(32) 
				string=ReplaceString(string, Chr(32),"",#PB_String, i,1)
			EndIf
		Next
		;process white spaces after last quoted strings
		For i=epos+1 To Len(string)
			If Mid(string,i)=Chr(32) 
				string=ReplaceString(string, Chr(32),"",#PB_String, i,1)
			EndIf
		Next
	EndIf
	ProcedureReturn string
EndProcedure

;-- Main

; get filename or temp filename
f$="CF_"+GetFilePart(ProgramParameter(0))
If f$=""
	f$="CF_"+GetFilePart(ProgramParameter(1))
EndIf

If f$
	scode.s =ReplaceString(GetScintillaText(), #CRLF$+#CRLF$,#CRLF$); make double white lines to single while lines
	nbrlines=CountString(scode, #CRLF$)
	
	For i=1 To nbrlines
		AddElement(file_lines())
		file_lines()=StringField(scode, i, #CRLF$)
		If MatchRegularExpression(regex, file_lines())
			ExtractRegularExpression(regex,file_lines(),tmp$())
			dummy$=tmp$(0) ; tmp string for matching the original unchanged string
			tmp$(0)=tidystring(tmp$(0)) ; remove eccess spaces
			jj$=mod_string(tmp$(0))+#CRLF$
			file_lines()= ReplaceString(file_lines(),dummy$,jj$) ; replaces the existing string with the modified string
			count+1
		EndIf
		tmp$(0)=""
	Next
	
	;create tmp file and run inthe pbide when finished
	If count > 1
		If CreateFile(0,GetTemporaryDirectory()+f$)
			ForEach file_lines()
				WriteStringN(0,file_lines())
			Next
			CloseFile(0)
			cleanup()
			RunProgram(GetTemporaryDirectory()+f$)
			End
		Else
			MessageRequester("error", " cant open "+GetTemporaryDirectory()+"mtmp.pb")
			End
		EndIf
	EndIf	
EndIf
End
malleo, caput, bang. Ego, comprehendunt in tempore