December 03, 2020, 07:54:24 PM

Author Topic: [bb] Auto-format by jfk EO-11110 [ 1+ years ago ]  (Read 513 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Auto-format by jfk EO-11110 [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Auto-format
Author : jfk EO-11110
Posted : 1+ years ago

Description : I still use the original Editor. So I made this Code-Formatter. 3th Party Editors probably already contain this Feature. However. It works ok, the only Problem is with
"If x=0 Print y" Commands, but I use "Then" anyway and that works.


Code :
Code: BlitzBasic
  1. ; This Code will Auto-format Blitz Source.
  2.  
  3. ; It will not work with One-Line If-Commands wich don't use "Then"
  4. ; "If KeyDown(1) Print a"
  5. ; will not be formatted correctly, but
  6. ; "If KeyDown(1) Then Print a"
  7. ; will work ok.
  8.  
  9. f$="auto_scr_format.bb" ; this File will be formated
  10. o$="  "; will be used for Offset in Formatting: use Space(s) or Tab
  11.  
  12. CopyFile f$,f$+".fbk" ; a Backup will be created
  13. Dim ln$(100000) ; max numbr of lines
  14.  
  15. fi=ReadFile(f$) ; Read File to Array
  16. z=0
  17. While Eof(fi)=0
  18.   ln$(z)=ReadLine(fi)
  19.   z=z+1
  20. Wend
  21. z=z-1
  22. CloseFile fi
  23.  
  24. Restore frm_plus ; Read Relevant Command Tags, Format-Increment and -Decrement ones
  25. co_pl=0
  26. Repeat
  27.   Read r$
  28.   co_pl=co_pl+1
  29. Until r$=""
  30. co_pl=co_pl-1
  31.  
  32. Restore frm_minus
  33. co_mi=0
  34. Repeat
  35.   Read r$
  36.   co_mi=co_mi+1
  37. Until r$=""
  38. co_mi=co_mi-1
  39.  
  40. Dim f_plus$(co_pl)
  41. Dim f_minus$(co_mi)
  42.  
  43. Restore frm_plus
  44. For i=0 To co_pl-1
  45.   Read f_plus$(i)
  46. Next
  47.  
  48. Restore frm_minus
  49. For i=0 To co_mi-1
  50.   Read f_minus$(i)
  51. Next
  52.  
  53.  
  54. .frm_plus
  55. Data "If","While","For ","Repeat","Function","Type","Else","Select","Case",""
  56.  
  57.  
  58. .frm_minus
  59. Data "EndIf","Wend","Until","Forever","End Function","End Type","Else","Next","End Select","Case",""
  60. ; You can add some more (note "Else" or "Case" is in both Arrays!)
  61.  
  62.  
  63.  
  64. For i=0 To z ; remove Spaces and Tabs at beginning and End of Lines
  65.   c=1
  66.   While Mid$(ln$(i),c,1)=" " Or Mid$(ln$(i),c,1)=Chr$(9)
  67.     c=c+1
  68.   Wend
  69.   c=c-1
  70.   If c>0
  71.     ln$(i)=Right$(  ln$(i), Len(  ln$(i)  )-c  )
  72.   EndIf
  73.   While Right$(ln(i),1)=" " Or Right$(ln(i),1)=Chr$(9)
  74.     ln$(i)=Left$(ln$(i),Len(ln$(i))-1)
  75.   Wend
  76. Next
  77.  
  78.  
  79. ;----------------------------------------------------------------------
  80. For i=0 To z
  81.   ez=ez+nez
  82.   nez=0
  83.   For j=0 To co_mi-1
  84.     If Instr(ln$(i),f_minus$(j),1)=1
  85.       ez=ez-1
  86.       Exit
  87.     EndIf
  88.   Next
  89.   For j=0 To co_pl-1
  90.     If Instr(ln$(i),f_plus$(j),1)=1
  91.       nez=1
  92.       ; check special "Then" cases
  93.       wo=Instr(ln$(i),"Then",1) ; is it a single-line if-then command?
  94.       wo2=Instr(ln$(i),";",1)
  95.       If wo<>0 And (wo < Len(ln$(i))-6) And (wo2=0 Or wo2>=(wo+4)); Line finishes after that "Then" without further Commands?
  96.         nez=0
  97.       EndIf
  98.       If wo<>0 And wo2>=(wo+4)
  99.         nez=1
  100.         For j2=wo+4 To wo2
  101.           what$=Mid$(ln$(i),j2,1)
  102.           If what$<>" " And what$<>Chr$(9) And what$<>";"  ; some Commands between "Then" and ";" ?
  103.             nez=0
  104.           EndIf
  105.         Next
  106.       EndIf
  107.       Exit
  108.     EndIf
  109.   Next
  110.   Print String$(o$,ez)+ln$(i)
  111.   ln$(i)=String$(o$,ez)+ln$(i)
  112. Next
  113. ;----------------------------------------------------------------------
  114.  
  115.  
  116. ;finally save the edited Version
  117.  
  118. wr=WriteFile(f$)
  119. For i=0 To z
  120.   WriteLine(wr,ln$(i))
  121. Next
  122. CloseFile wr
  123. Color 0,255,0
  124. Print "ok, formatted "+f$+" ("+z+" Lines of code.)"
  125. Print "Key"
  126. WaitKey()
  127. End


Comments :


eBusiness(Posted 1+ years ago)

 Thanks, this saved me a good deal of time after a minor accident.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal