Auto Formatting
Auto Formatting
I saw a quite nice feature in jaPBe. The auto formatting. It would be kinda nice, because my code gets very messy (even if it is sooo simple).
It just a thought, and it would be really nice to have it in the PB IDE :roll:
P.S.
I don't like the jaPBe for some reason... Dunno...
It just a thought, and it would be really nice to have it in the PB IDE :roll:
P.S.
I don't like the jaPBe for some reason... Dunno...
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Re: Auto Formatting
Yes, i kicked freak numerous times to implement this.xSoNiCaSx wrote:I saw a quite nice feature in jaPBe. The auto formatting. It would be kinda nice, because my code gets very messy (even if it is sooo simple).
It just a thought, and it would be really nice to have it in the PB IDE :roll:
P.S.
I don't like the jaPBe for some reason... Dunno...
There are a couple of tools available that do this...on a button press.
here is a pb source http://www.purebasic.fr/english/viewtop ... r&start=15
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Not code/code formatted difficult to open in PB IDE... Inserting format here...
Benny... I could not resist playing with it a bit... Sorry... Will delete if you insist!
Great job Beriko!
{{ DAMN Lucif3r beat me... to my own edit!!! }}
Code: Select all
; ------------------------------------------------------------
;
; Source Formatting By Berikco
;
; (c) 2002 - Benny Sels
;
; ------------------------------------------------------------
;
; For Purebasic Toolmenu version 3.40 and up
; Will format the source with TABS(Spaces)
; Thanks to PB for his help on the ';' comment finder
;
; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
; 2007 / 12 / 19
; Rook Zimbabwe updated code to PB4.1
;
; 2002/02/18
; some small changes
;
; 2002/10/09
; First version
; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
; Note: When writing PureBasic *.pb sourcefile, don't add CRLF after last line!
; This will produce strange results at end of code
Enumeration
#Window_0
EndEnumeration
Enumeration
#Button_MANGLE
#Text_2
#Text_1
#Text_0
#Combo_EXTRA
#Combo_INDENT
#Button_INFILE
#String_INFILE
#Source
#Destination
EndEnumeration
Structure VisualDesignerGadgets
Gadget.l
EventFunction.l
EndStructure
Global NewList EventProcedures.VisualDesignerGadgets()
Global DestFileName$
Global Numspaces
Global SelectTab
Procedure Button_MANGLE_Event(Window, Event, Gadget, Type)
Debug "#Button_MANGLE"
SourceFileName$ = DestFileName$+"2" ; The backup file *.pb2
Numspaces = GetGadgetData(#Combo_INDENT)
SelectTab = GetGadgetData(#Combo_EXTRA)
If FileSize(DestFileName$) < 1
MessageRequester("INPUT ERROR", "There is no file selected..."+Chr(10)+"Please Select a file...", #PB_MessageRequester_Ok); Do nothing if source empty
Goto Zoe
EndIf
If FileSize(SourceFileName$) ; If old backupFile exist, delete it
DeleteFile(SourceFileName$)
EndIf
If RenameFile(DestFileName$, SourceFileName$) ; Rename source to backup
If OpenFile(#Source,SourceFilename$) ; Open Source, now with *.pb2 extension
If CreateFile(#Destination,DestFileName$) ; Create new source
While Eof(#source)=false
; UseFile(#Source)
a$=ReadString(#source)
If Eof(#source)=false ; Check if read is last line in source
; If this is the case, must not write CRLF
a$=LTrim(a$) ;Remove old TABs (spaces in front)
a$=RTrim(a$) ;Remove all spaces at end
Tab+NextTab
NextTab=0
in=FindString(a$," ",1) ; Find PureBasic Keyword
If in>0
Vgl$=LCase(Left(a$,in-1)) ; Get only Keyword
Else
Vgl$=LCase(a$) ; Nothing after Keyword, use whole string
EndIf
; Check For Keyword that needs formatting
If Left(a$,1)=";"
;nope
ElseIf Vgl$="procedure" Or Vgl$="procedure.b" Or Vgl$="procedure.w" Or Vgl$="procedure.l" Or Vgl$="procedure.s" Or Vgl$="procedure$" Or Vgl$="datasection"
Tab=0
NextTab=1
ElseIf Vgl$="endprocedure" Or Vgl$="enddatasection"
Tab=0
ElseIf Vgl$="if" Or vgl$="select" Or Vgl$="for" Or Vgl$="repeat" Or Vgl$="while" Or Vgl$="structure" Or Vgl$="compilerif" Or Vgl$="opensubmenu"
b$=a$+";" ;little trick, add comment to line, only one routine needed to scan
For r=1 To Len(b$)
a=Asc(Mid(b$,r,1))
If a=34 : q=1-q : EndIf
If a=58 And q=0 : NextTab=0 : r=Len(b$) : EndIf
If a=59 And q=0
If Vgl$="select" And SelectTab=1
NextTab=2
Else
NextTab=1
EndIf
r=Len(b$)
EndIf
Next
ElseIf Vgl$="endif" Or Vgl$="endstructure" Or Vgl$="return" Or Vgl$="next" Or Vgl$="until" Or Vgl$="wend" Or Vgl$="forever" Or Vgl$="compilerendif" Or Vgl$="closesubmenu"
Tab-1
NextTab=0
ElseIf Vgl$="endselect"
If SelectTab=1
Tab-2
Else
Tab-1
EndIf
NextTab=0
ElseIf Vgl$="elseif" Or Vgl$="else" Or Vgl$="case" Or Vgl$="compilerelse" Or Vgl$="default"
Tab-1
NextTab=1
Else
b$=a$+";" ;little trick, add comment to line, only one routine needed to scan
KeyWord$=""
For r=1 To Len(b$)
a=Asc(Mid(b$,r,1))
If a=34 : q=1-q : EndIf
If a=59 And q=0
KeyWord$=LTrim(KeyWord$) ;Remove spaces in front
KeyWord$=LCase(RTrim(KeyWord$)) ;Remove all spaces at end and put lowercase
If KeyWord$="endif" Or KeyWord$="endstructure" Or KeyWord$="next" Or KeyWord$="until" Or KeyWord$="Wend" Or KeyWord$="ForEver"
If tab>0 : tab-1 : EndIf
EndIf
EndIf
keyword$+Chr(a)
If a=58 And q=0
If keyword$=a$
Tab=0
NextTab=1
r=Len(b$)
EndIf
KeyWord$=""
EndIf
Next
EndIf
Front$=Space(Tab * NumSpaces) ; Create Spaces needed
a$ = Front$ + a$ ; Put spaces before the line code
; UseFile(#Destination)
WriteStringN(#Destination,a$) ; Write in new source
Else
; UseFile(#Destination)
WriteString(#Destination,a$)
EndIf
Wend
CloseFile(#source)
CloseFile(#destination)
DeleteFile(SourceFileName$)
EndIf
EndIf
EndIf
Zoe:
EndProcedure
Procedure Text_2_Event(Window, Event, Gadget, Type)
Debug "#Text_2"
EndProcedure
Procedure Text_1_Event(Window, Event, Gadget, Type)
Debug "#Text_1"
EndProcedure
Procedure Text_0_Event(Window, Event, Gadget, Type)
Debug "#Text_0"
EndProcedure
Procedure Combo_EXTRA_Event(Window, Event, Gadget, Type)
Debug "#Combo_EXTRA"
EndProcedure
Procedure Combo_INDENT_Event(Window, Event, Gadget, Type)
Debug "#Combo_INDENT"
EndProcedure
Procedure Button_INFILE_Event(Window, Event, Gadget, Type)
Pattern$ = "PureBasic (*.pb)|*.pb|All files (*.*)|*.*"
Pattern = 0 ; use the first of the three possible patterns as standard
DestFileName$ = OpenFileRequester("Please choose file to load", "", Pattern$, Pattern)
If File$
SetGadgetText(#String_INFILE, DsetFileName$)
Else
MessageRequester("Information", "The requester was canceled.", 0)
EndIf
EndProcedure
Procedure String_INFILE_Event(Window, Event, Gadget, Type)
Debug "#String_INFILE"
EndProcedure
Procedure RegisterGadgetEvent(Gadget, *Function)
If IsGadget(Gadget)
AddElement(EventProcedures())
EventProcedures()\Gadget = Gadget
EventProcedures()\EventFunction = *Function
EndIf
EndProcedure
Procedure CallEventFunction(Window, Event, Gadget, Type)
ForEach EventProcedures()
If EventProcedures()\Gadget = Gadget
CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
LastElement(EventProcedures())
EndIf
Next
EndProcedure
Procedure Open_Window_0()
If OpenWindow(#Window_0, 5, 5, 400, 170, "Code Mangler 1.b - Use at your own risk!", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
If CreateGadgetList(WindowID(#Window_0))
StringGadget(#String_INFILE, 6, 12, 360, 18, "Select File to Abuse...")
RegisterGadgetEvent(#String_INFILE, @String_INFILE_Event())
ButtonGadget(#Button_INFILE, 366, 12, 24, 18, "...")
RegisterGadgetEvent(#Button_INFILE, @Button_INFILE_Event())
ComboBoxGadget(#Combo_INDENT, 132, 48, 60, 180)
RegisterGadgetEvent(#Combo_INDENT, @Combo_INDENT_Event())
AddGadgetItem(#Combo_INDENT, -1,"0")
AddGadgetItem(#Combo_INDENT, -1,"1")
AddGadgetItem(#Combo_INDENT, -1,"2")
AddGadgetItem(#Combo_INDENT, -1,"3")
AddGadgetItem(#Combo_INDENT, -1,"4")
AddGadgetItem(#Combo_INDENT, -1,"5")
AddGadgetItem(#Combo_INDENT, -1,"6")
AddGadgetItem(#Combo_INDENT, -1,"7")
AddGadgetItem(#Combo_INDENT, -1,"8")
AddGadgetItem(#Combo_INDENT, -1,"9")
ComboBoxGadget(#Combo_EXTRA, 312, 48, 66, 180)
RegisterGadgetEvent(#Combo_EXTRA, @Combo_EXTRA_Event())
AddGadgetItem(#Combo_EXTRA, -1,"0")
AddGadgetItem(#Combo_EXTRA, -1,"1")
AddGadgetItem(#Combo_EXTRA, -1,"2")
AddGadgetItem(#Combo_EXTRA, -1,"3")
AddGadgetItem(#Combo_EXTRA, -1,"4")
AddGadgetItem(#Combo_EXTRA, -1,"5")
AddGadgetItem(#Combo_EXTRA, -1,"6")
AddGadgetItem(#Combo_EXTRA, -1,"7")
AddGadgetItem(#Combo_EXTRA, -1,"8")
AddGadgetItem(#Combo_EXTRA, -1,"9")
TextGadget(#Text_0, 6, 48, 114, 18, "SPACES TO INDENT")
RegisterGadgetEvent(#Text_0, @Text_0_Event())
TextGadget(#Text_1, 216, 48, 90, 18, "EXTRA SPACING")
RegisterGadgetEvent(#Text_1, @Text_1_Event())
TextGadget(#Text_2, 12, 120, 390, 50, "This code was written by BERIKO (Benny Sels) and (c) 2002"+Chr(10)+"No warranty of any sort is implied"+Chr(10)+"Use at your own risk!", #PB_Text_Center)
RegisterGadgetEvent(#Text_2, @Text_2_Event())
ButtonGadget(#Button_MANGLE, 90, 90, 228, 24, "MANGLEIFY THE CODE")
RegisterGadgetEvent(#Button_MANGLE, @Button_MANGLE_Event())
EndIf
EndIf
EndProcedure
Open_Window_0()
SetGadgetState(#Combo_EXTRA, 0)
SetGadgetState(#Combo_INDENT, 3)
Repeat
Event = WaitWindowEvent()
Gadget = EventGadget()
Type = EventType()
Window = EventWindow()
Select Event
Case #PB_Event_Gadget
CallEventFunction(Window, Event, Gadget, Type)
EndSelect
Until Event = #PB_Event_CloseWindow
End
Benny... I could not resist playing with it a bit... Sorry... Will delete if you insist!

Great job Beriko!

{{ DAMN Lucif3r beat me... to my own edit!!! }}
Last edited by Rook Zimbabwe on Wed Dec 19, 2007 5:50 pm, edited 2 times in total.
Well... When I talked about it I was thinking about... Well... Like the CTRL-TAB shortcut which idents the code inside the IDE, because it is kinda uncomfortable to close Pure, run this, and then get back to it.
Perfect tabbing with no delay, it's only one shortcut away...
As I said, it would be nice
Perfect tabbing with no delay, it's only one shortcut away...

As I said, it would be nice

-
- 666
- Posts: 1033
- Joined: Mon Sep 01, 2003 2:33 pm
you can add it to the IDE tool menu you know. then you don't have to quit out of PB at all
even assign it a keyboard shortcut
working for PB4 now. Added "Enumeration" indenting too. hehe. Nice Job Berikco. Never seen this before either 
[edit] Nice spot Foz, code updated
[/edit]

Code: Select all
; ------------------------------------------------------------
;
; Source Formatting By Berikco
;
; (c) 2002 - Benny Sels
;
; ------------------------------------------------------------
;
; For Purebasic Toolmenu version 3.40 and up
; Will format the source with TABS(Spaces)
; Thanks to PB for his help on the ';' comment finder
;
; --- Toolbox settings ---------------------------------------
;
; Arguments:
; First argument "%FILE"
; Second argument #Number of spaces for 1 Tab
; Third argument if 1 extra Select indent
; Example: "%FILE" 2 [1]
;
; Wait Until tool Quits = On
; Reload source after program is finished = On
;
; ------------------------------------------------------------
;
; 2008/03/01 - Bug Fix for Enumeration indenting (Foz)
;
; 2007/12/24
; Updated for PB4.10 added Enumeration indenting (LuCiFeR[SD])
;
;
; 2002/02/18
; some small changes
;
;
; 2002/10/09
; First version
;
; Note: When writing PureBasic *.pb sourcefile, don't add CRLF after last line!
; This will produce strange results at end of code
;
DestFileName$ = ProgramParameter() ; Sourcefile to read
NumSpaces = Val(ProgramParameter()) ; Number of spaces
SelectTab= Val(ProgramParameter()) ; 1= Extra Indent At Select
SourceFileName$ = DestFileName$+"2" ; The backup file *.pb2
#Source=1
#Destination=2
If FileSize(DestFileName$) ; Do nothing if source empty
If FileSize(SourceFileName$) ; If old backupFile exist, delete it
DeleteFile(SourceFileName$)
EndIf
If RenameFile(DestFileName$, SourceFileName$) ; Rename source to backup
If OpenFile(#Source,SourceFilename$) ; Open Source, now with *.pb2 extension
If CreateFile(#Destination,DestFileName$) ; Create new source
While Eof(#source)=false
a$=ReadString(#Source)
If Eof(#source)=false ; Check if read is last line in source
; If this is the case, must not write CRLF
a$=LTrim(a$) ;Remove old TABs (spaces in front)
a$=RTrim(a$) ;Remove all spaces at end
Tab+NextTab
NextTab=0
in=FindString(a$," ",1) ; Find PureBasic Keyword
If in>0
Vgl$=LCase(Left(a$,in-1)) ; Get only Keyword
Else
Vgl$=LCase(a$) ; Nothing after Keyword, use whole string
EndIf
; Check For Keyword that needs formatting
If Left(a$,1)=";"
;nope
ElseIf Vgl$="procedure" Or Vgl$="procedure.b" Or Vgl$="procedure.w" Or Vgl$="procedure.l" Or Vgl$="procedure.s" Or Vgl$="procedure$" Or Vgl$="datasection"
Tab=0
NextTab=1
ElseIf Vgl$="endprocedure" Or Vgl$="enddatasection"
Tab=0
ElseIf Vgl$="if" Or vgl$="select" Or Vgl$="for" Or Vgl$="repeat" Or Vgl$="while" Or Vgl$="structure" Or Vgl$="compilerif" Or Vgl$="opensubmenu" Or Vgl$="enumeration" Or Vgl$="foreach"
b$=a$+";" ;little trick, add comment to line, only one routine needed to scan
For r=1 To Len(b$)
a=Asc(Mid(b$,r,1))
If a=34 : q=1-q : EndIf
If a=58 And q=0 : NextTab=0 : r=Len(b$) : EndIf
If a=59 And q=0
If Vgl$="select" And SelectTab=1
NextTab=2
Else
NextTab=1
EndIf
r=Len(b$)
EndIf
Next
ElseIf Vgl$="endif" Or Vgl$="endstructure" Or Vgl$="return" Or Vgl$="next" Or Vgl$="until" Or Vgl$="wend" Or Vgl$="forever" Or Vgl$="compilerendif" Or Vgl$="closesubmenu" Or Vgl$="endenumeration"
Tab-1
NextTab=0
ElseIf Vgl$="endselect"
If SelectTab=1
Tab-2
Else
Tab-1
EndIf
NextTab=0
ElseIf Vgl$="elseif" Or Vgl$="else" Or Vgl$="case" Or Vgl$="compilerelse" Or Vgl$="default"
Tab-1
NextTab=1
Else
b$=a$+";" ;little trick, add comment to line, only one routine needed to scan
KeyWord$=""
For r=1 To Len(b$)
a=Asc(Mid(b$,r,1))
If a=34 : q=1-q : EndIf
If a=59 And q=0
KeyWord$=LTrim(KeyWord$) ;Remove spaces in front
KeyWord$=LCase(RTrim(KeyWord$)) ;Remove all spaces at end and put lowercase
If KeyWord$="endif" Or KeyWord$="endstructure" Or KeyWord$="next" Or KeyWord$="until" Or KeyWord$="Wend" Or KeyWord$="ForEver"
If tab>0 : tab-1 : EndIf
EndIf
EndIf
keyword$+Chr(a)
If a=58 And q=0
If keyword$=a$
Tab=0
NextTab=1
r=Len(b$)
EndIf
KeyWord$=""
EndIf
Next
EndIf
Front$=Space(Tab * NumSpaces) ; Create Spaces needed
a$ = Front$ + a$ ; Put spaces before the line code
WriteStringN(#Destination,a$) ; Write in new source
Else
WriteString(#Destination,a$)
EndIf
Wend
CloseFile(#source)
CloseFile(#destination)
DeleteFile(SourceFileName$)
EndIf
EndIf
EndIf
EndIf
End

[edit] Nice spot Foz, code updated

Last edited by LuCiFeR[SD] on Thu Jan 03, 2008 11:30 pm, edited 2 times in total.
-
- 666
- Posts: 1033
- Joined: Mon Sep 01, 2003 2:33 pm
Just to note, you need to add this to line 87 otherwise looping through enumeration will screw up the indents
Code: Select all
Or Vgl$="foreach"
Tsk tsk. CodeCaddy has been doing this for ages (and lets you define how to indent) and it dates back to... what was it... 2005 or so. JAPBE did it earlier and realtime, and I think the oldest reformat code for PB that I've seen was by Soldat Inconnu, IIRC 

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Tssss, you are totally wrong Blueznlblueznl wrote:Tsk tsk. CodeCaddy has been doing this for ages (and lets you define how to indent) and it dates back to... what was it... 2005 or so. JAPBE did it earlier and realtime, and I think the oldest reformat code for PB that I've seen was by Soldat Inconnu, IIRC

In the original link, you can see the post is from 2002, so Codecaddy was not even born when this was released the first time

http://www.purebasic.fr/english/viewtop ... r&start=15
It was even posted on the first PB forum that used Snitz software and was hosted by Leigh, that's why the post is now under the BackupUser.
Your ages are going to fast

This tool existed in PureBasic 3.00 as standalone app when the IDE did not yet supported the tools.
I myself changed the IDE to remember the cursorposition and first visible line that are saved in the sourcode, so the source does not jump to the first line when the IDE reloaded the formated sourcecode.
And this tool also takes arguments for the number of indents, and the way to format the select-case.
Did not see Leigh for a long time, he is the 10the registered user on the first phpBB forum hosted by Fred.
