Tab, and indenting code
Posted: Sun Jun 26, 2016 7:34 am
the example code used here to load a file PB
then see the result in the debugger
do not forget that here are True tabs Tabs (Chr (9)) so it may seem impressive
in the debugger, but in a Scintilla editor or another, we can easily adjust the size of the Tabs
this procedure is rather made to work with an editor (a real, not the debugger)
improvement of indentations Select Cases, and alignment with the IF Else
I would also account decrements being on the same line
eg If xxxx: endif // For the ixxxx: next etc ...
I tested on well-twisted codes, it works fine
then see the result in the debugger
do not forget that here are True tabs Tabs (Chr (9)) so it may seem impressive
in the debugger, but in a Scintilla editor or another, we can easily adjust the size of the Tabs
this procedure is rather made to work with an editor (a real, not the debugger)

improvement of indentations Select Cases, and alignment with the IF Else
I would also account decrements being on the same line
eg If xxxx: endif // For the ixxxx: next etc ...
I tested on well-twisted codes, it works fine
Code: Select all
Declare.S tabulation(txt.S)
Global NewMap decrement.S()
decrement.S("endenumeration")="endenumeration"
decrement.S("endstructure")="endstructure"
decrement.S( "endprocedure")="endprocedure"
decrement.S( "enddatasection")="enddatasection"
decrement.S( "endselect")="endselect"
decrement.S( "stopdrawing")="stopdrawing"
decrement.S( "endinterface")="endinterface"
decrement.S( "endstructure")="endstructure"
decrement.S( "endimport")= "endimport"
decrement.S( "endmacro")= "endmacro"
decrement.S( "endwith")="endwith"
decrement.S( "endstructureunion")="endstructureunion"
decrement.S("stop3d")="stop3d"
decrement.S("compilerendif")="compilerendif"
decrement.S("compilerendselect")="compilerendselect"
decrement.S("endmodule")="endmodule"
decrement.S("enddeclaremodule")="enddeclaremodule"
decrement.S("closefile")="closefile"
decrement.S("next")="next"
decrement.S("until")="until"
decrement.S("forever")="forever"
decrement.S("wend")="wend"
decrement.S( "endif")="endif"
; *******************************************
; attention l'ordre des termes a son importance
Global NewMap increment.S()
increment.S( "enumeration")="enumeration"
increment.S( "datasection")="datasection"
increment.S( "startdrawing")= "startdrawing"
increment.S( "structure")= "structure"
increment.S( "proceduredll")= "proceduredll"
increment.S( "procedurecdll")= "procedurecdll"
increment.S( "procedurec")="procedurec"
increment.S( "procedure")= "procedure"
increment.S( "importc")= "importc"
increment.S( "import")= "import"
increment.S( "macro")="macro"
increment.S( "interface")= "interface"
increment.S( "structureunion")= "structureunion"
increment.S("start3d")="start3d"
increment.S("compilerif")="compilerif"
increment.S("compilerselect")="compilerselect"
increment.S("module")="module"
increment.S("declaremodule")="declaremodule"
increment.S( "with")= "with"
increment.S( "select")= "select"
increment.S("openfile")="openfile"
increment.S("readfile")="readfile"
increment.S("createfile")="createfile"
increment.S( "foreach")="foreach"
increment.S( "for")="for"
increment.S( "repeat")="repeat"
increment.S( "while")="while"
increment.S( "if")="if"
increment.S("case")="case"
local.S=GetCurrentDirectory()
file.S=OpenFileRequester("ouvrir code",local.S,"*.pb",1)
Global NewList code.S()
if OpenFile(0,file.S)
While Eof(0) = 0 ; Boucle tant que la fin du fichier n'est pas atteinte. (Eof = 'End Of File')
line.S=ReadString(0)
a$=LTrim(line.S,Chr(9))
a$=LTrim(a$)
AddElement(code.S())
code.S()=a$
Wend
CloseFile(0)
Endif
Global Compteur_tab=0
ForEach code.S()
txt.S=code.S()
Debug tabulation(txt.S)
Next
Procedure.S tabulation(txt.S)
; procedure de tabulation by Dobro (plus court tu meurts !! )
extrait.S=LCase(StringField(txt.S,1,":")) ; recup avant multipligne
; extrait.S=StringField(extrait.S,1,";"); recupe avant commentaire : ceci ne tabule pas les comentaires ... c'est un choix
extrait.S=StringField(extrait.S,1," "); recupe premier mot
extrait.S=StringField(extrait.S,1,"("); pour les fonctions
extrait.S=StringField(extrait.S,1,"."); pour les fonctions (typée)
extrait.S=LCase(Trim(extrait.S))
; If extrait.S="" :Goto su :EndIf ; saute les lignes vides
tab.S=LSet(tab.S,Compteur_tab,Chr(9)) ; pour l'increment se fait dans un deuxieme appel
; -- Else
If extrait.S="else" Or extrait.S="elseif" Or extrait.S="case"
mem_Compteur_tab=Compteur_tab
Compteur_tab=Compteur_tab-1
tab.S=LSet(tab.S,Compteur_tab,Chr(9)) ; pour un decrement, c'est tout de suite
Compteur_tab=mem_Compteur_tab
Goto su
EndIf
;++++ increment
If extrait.S= increment.S(extrait.S) And extrait.S<>""
; regarde si un decrement se trouve sur la meme ligne qu'un increment , dans ce cas on tabule en arriere puis on remet la valeur de tabulation
extrait2.S=StringField(txt.S,1,";")
ForEach decrement.S()
pooo=FindString(extrait2.S,decrement.S(),Len(extrait.S),#PB_String_NoCase)
If pooo>0
If Asc(Mid( extrait2.S,pooo+Len(decrement.S()),1))<33
Goto su
Else
Break
EndIf
EndIf
Next
; regarde si un increment se trouve sur la meme ligne qu'un increment dans ce cas on ajoute une tabulation
extrait2.S=StringField(txt.S,1,"(") :extrait2.S=StringField(extrait2.S,1,"."):extrait2.S=StringField(extrait2.S,1,":")
ForEach increment.S()
;If FindString(extrait2.S,increment.S(),Len(extrait.S),#PB_String_NoCase)>0
pooo=FindString(extrait2.S,increment.S(),Len(extrait.S),#PB_String_NoCase) ; on a trouvé un semblant d'increment
If pooo>0
If Asc(Mid( extrait2.S,pooo+Len(increment.S()),1))<33 ; on regarde le caractere suivant, si inf a 33 c'est vrais c'est un increment
Compteur_tab=Compteur_tab+1
Break
EndIf
EndIf
Next
;
Compteur_tab=Compteur_tab+1
Goto su
EndIf
;----- decrement
If extrait.S= decrement.S(extrait.S) And extrait.S<>""
Compteur_tab=Compteur_tab-1
tab.S=LSet(tab.S,Compteur_tab,Chr(9)) ; pour un decrement, c'est tout de suite
Goto su
EndIf
su:
If Compteur_tab<=0:Compteur_tab=0
tab.S=""
EndIf
txt.S=tab.S+txt.S;+"("+str(compteur_tab)+")"
ProcedureReturn txt.S
EndProcedure