Advertisement
savior67

format(Util)

Feb 23rd, 2013
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. ---------------------
  2. --Created by Savior67
  3. --Version: 0.4
  4. ---------------------
  5. local indent = 2 --number of spaces to indent by
  6.  
  7.  
  8. local tArgs = { ... }
  9. if #tArgs ~= 1 then
  10.   print("Usage: "..shell.getRunningProgram().." <file>")
  11.   return
  12. end
  13. local file = tArgs[1]
  14.  
  15. function main()
  16.   local formatted = {}
  17.   local currentIndent = 0 --how many indents are active
  18.   local exception = false --true if else or elseif after if
  19.   if not fs.exists(file) then
  20.     print("File not found.")
  21.     return
  22.   else
  23.     local h = fs.open(file,"r")
  24.     local line = h.readLine()
  25.     while line~=nil do
  26.       formatted,currentIndent = keyFinder(line,currentIndent,formatted)
  27.       line = h.readLine()
  28.     end
  29.     h.close()
  30.     overwrite(formatted,file)
  31.   end
  32.   print(file.." was successfully formatted.")
  33. end
  34.  
  35. function overwrite(lines,file)
  36.   local h = fs.open(file,"w")
  37.   for i,line in ipairs(lines) do
  38.     h.writeLine(line)
  39.   end
  40.   h.close()
  41. end
  42.  
  43. function formatLine(s,currIn)
  44.   local whitespace = ""
  45.   for i=1,currIn do
  46.     whitespace = whitespace.." "
  47.   end
  48.   return(whitespace..trim(s))
  49. end
  50.  
  51. function keyFinder(s,currentIndent,f)
  52.   local inWrds = {"if","do","repeat","function"}
  53.   local deWrds = {"end","until"}
  54.   local specialCase = {"elseif","else"}
  55.  
  56.   local ded = findCases(s,deWrds)
  57.   local ind = findCases(s,inWrds)
  58.   local spe = findCases(s,specialCase)
  59.  
  60.   if ind and ded then
  61.     table.insert(f,formatLine(s,currentIndent))
  62.   elseif spe then
  63.     table.insert(f,formatLine(s,currentIndent-indent))
  64.   elseif ded then
  65.     currentIndent = currentIndent-indent
  66.     table.insert(f,formatLine(s,currentIndent))
  67.   elseif ind then
  68.     table.insert(f,formatLine(s,currentIndent))
  69.     currentIndent = currentIndent+indent
  70.   else
  71.     table.insert(f,formatLine(s,currentIndent))
  72.   end
  73.   return f,currentIndent
  74. end
  75.  
  76. function findCases(s,t)
  77.   for k,v in ipairs(t) do
  78.     if string.find(s,v) then
  79.       if not inQuotes(s,v) and not isComment(s,v) then
  80.          return true
  81.       end
  82.     end
  83.   end
  84.   return false
  85. end
  86.  
  87. function inQuotes(s,word)
  88.   local p1 = string.find(s,"\"")
  89.   if p1 ~= nil then
  90.     local p2 = string.find(s,"\"",p1+1)
  91.     if not p2 then return false end
  92.     local start,finish = string.find(s,word)
  93.     if p1<start and p2>finish then return true end
  94.   end
  95.   return false
  96. end
  97.  
  98. function isComment(s,word)
  99.   local p1=0
  100.   while true do
  101.     p1 = string.find(s,"-",p1+1)
  102.     if p1~= nil then
  103.       local p2 = string.sub(s,p1+1,p1+1)
  104.       if p2=="-" then
  105.         local start,finish = string.find(s,word)
  106.         if p1<start then return true end
  107.       end
  108.     else
  109.       break
  110.     end
  111.   end
  112.   return false
  113. end
  114.  
  115. --removes leading and trailing whitespace
  116. function trim(s)
  117.   -- from PiL2 20.4
  118.   return (s:gsub("^%s*(.-)%s*$", "%1"))
  119. end
  120.  
  121. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement