Ooops
January 26, 2021, 05:36:49 AM

Author Topic: [bb] Auto code indenter by Rob Farley [ 1+ years ago ]  (Read 417 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Auto code indenter by Rob Farley [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Auto code indenter
Author : Rob Farley
Posted : 1+ years ago

Description : Somebody needed a program to do this... so I thought I'd have a crack at writing it.

The code below is not indented on purpose!

Give it an input file and an output file, run it and the code comes out all indented.

There are 2 lists, addindent$ and decindent$, these hold the commands for adding and removing indents (with the exception of 'if' as this is a bit special). I've probably missed a couple so let me know which ones and I'll update the code as necessary. You will notice that some of the commands have a space after them, that's because you might have a variable called functionname so it needs the space to create seperation.

Times it will NOT work:
If you've got variable names like EndIfName it will go pear shaped.
If you've got if statements without 'then' formed like 'If a=true a=false'

Happy indenting!


Code :
Code: BlitzBasic
  1. ; Code indenter written by Rob Farley (Dec 2004)
  2. ;
  3. ; Additional If-Then specials by Damien Sturdy
  4.  
  5. Function Entry$(number,List$,delimeter$=",")
  6. n=1
  7. count = 1
  8. found = False
  9. start = 1
  10.  
  11. If number > 1
  12. Repeat
  13. If Mid(List,n,1)=delimeter
  14. count = count + 1
  15. If count = number
  16. found=True
  17. start = n + 1
  18. Exit
  19. EndIf
  20. EndIf
  21. n=n+1
  22. Until n >= Len(List)
  23. If found = False Then RuntimeError("List Element out of Range")
  24. EndIf
  25. Endof = Instr(List,delimeter,start)
  26. If endof = 0 Then endof = Len(List)+1
  27. Return Mid(List,start,endof-start)
  28. End Function
  29.  
  30. Function countentries(List$,delimeter$=",")
  31. t$ = Replace(List$,delimeter,"")
  32. Return (Len(List)-Len(t))+1
  33. End Function
  34.  
  35.  
  36. Function removeindents$(l$)
  37. ret$=""
  38. For n=1 To Len(l$)
  39. If Asc(Mid(l,n,1))>31 Then ret=ret+Mid(l,n,1)
  40. Next
  41. Return ret
  42. End Function
  43.  
  44.  
  45. ; add addindent or decindent commands if I've missed any
  46. addindent$="repeat,while,function,type,for"
  47. addindentfuncs = countentries(addindent)
  48. DebugLog addindentfuncs
  49. decindent$="until,wend,end function,end type,next,endif"
  50. decindentfuncs = countentries(decindent)
  51.  
  52. indent = 0
  53.  
  54. inputfilename$="Autoindent.bb"
  55. outputfilename$="Autoindent.txt"
  56.  
  57.  
  58. filein = ReadFile(inputfilename)
  59. fileout = WriteFile(outputfilename)
  60.  
  61. addone=False
  62.  
  63. Repeat
  64.  
  65. l$ = removeindents(ReadLine(filein))
  66.  
  67. For n=1 To addindentfuncs
  68. funky$ = Lower(entry(n,addindent))
  69. If Left(Lower(l),Len(funky))=funky Then addone = True
  70.  
  71. ; special if statement, checks if there's a 'then' in the line
  72. If Left(Lower(l),2)="if" And Instr(Lower(l),"then")=0 Then addone = True
  73. If Left(Lower(l),2)="if" And Instr(Lower(l),"then:")=1 Then addone = True
  74. If Left(Lower(l),2)="if" And Instr(Lower(l),"then :")=1 Then addone = True
  75. If Left(Lower(l),2)="if" And Right$(Lower(l),4)="then" Then addone = True
  76. If Left(Lower(l),2)="if" And Instr(l,";")>1 Then
  77. nn=Instr(l,";")
  78. Repeat:nn=nn-1:Until Mid$(l,nn,1)<>";":nn=nn+1
  79. Repeat:nn=nn-1:Until Mid$(l,nn,1)>" " Or nn<2
  80. If nn>6 Then
  81. ;Print Lower(Mid$(l,nn-4,4))
  82. If Lower(Mid$(l,nn-3,4))="then" Then addone=True
  83. If Lower(Mid$(l,nn-4,5))="then:" Then addone=True
  84. If Lower(Mid$(l,nn-5,6))="then :" Then addone=True
  85. EndIf
  86. EndIf
  87.  
  88. Next
  89.  
  90. For n=1 To decindentfuncs
  91. funky$ = Lower(entry(n,decindent))
  92. If Left(Lower(l),Len(funky))=funky Then indent = indent - 1
  93. Next
  94.  
  95. tab$ = Chr(9)
  96.  
  97. indenter$ = ""
  98. If indent > 0
  99. For n=1 To indent
  100. indenter = indenter + tab
  101. Next
  102. EndIf
  103.  
  104. If addone = True
  105. indent = indent + 1
  106. addone = False
  107. EndIf
  108.  
  109. l = indenter + l
  110.  
  111. WriteLine fileout,l
  112.  
  113. Until Eof(filein)
  114.  
  115. CloseFile filein
  116. CloseFile fileout
  117. ExecFile outputfilename$


Comments :


Damien Sturdy(Posted 1+ years ago)

 Nice one.. great start. I like the code you did, and i like my modifications ^.^this realy helped me out with a super-long BB file thatd have taken HOURS to have fixed!!! cheers :)


_PJ_(Posted 1+ years ago)

 Another extremely useful snippet, Rob!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal