Formatter tool

Working on new editor enhancements?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

This one is difficult :cry:

Code: Select all

switchplayer:
If playerturn = 1 And playerturn > 0
  playerturn = 2
Else
  playerturn = 1
EndIf
Return
because...

Logo: IncludeBinary "logo.bmp"

Should not indent...
Must check for the 'Return' that could be miles away... :cry:
Must think about it....
Hey Fred, change the 'Label:' for Gosub to 'Sub Label:' , easy for me

Regards,

Berikco

http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

New 'Beta' version :)

new version below
Regards,

Berikco

http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

No reply's, guess everything works fine....and nothing to add i think....:)
Mmmm, must think about what tool to make next ...:)

Regards,

Berikco

http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

New version

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
;
; ------------------------------------------------------------
;
; 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 :)
; So dont use WriteStringN() for last line
;
; Note: This source is formated by itself :)
;

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
          
          UseFile(#Source)
          a$=ReadString()
          
          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(a$)              ; Write in new source
            
          Else
            UseFile(#Destination)
            WriteString(a$)
          EndIf
        Wend
        
        CloseFile(#source)
        CloseFile(#destination)
        DeleteFile(SourceFileName$)
        
      EndIf
    EndIf
  EndIf
EndIf
End
Regards,

Berikco

http://www.benny.zeb.be/purebasic.htm
Post Reply