t8rt0t

docsAPI.lua

Mar 28th, 2024 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.22 KB | None | 0 0
  1. --Adds the default variable types
  2. --@OptionalArg table tab: The table to add the default types to
  3. --@Returns table tab: The table with the default types added
  4. local function getDefaultTypes(tab)
  5.     local types = {
  6.         ["any"] = {colors.white,"Any type of value, typically for tables"},
  7.         ["string"]= {colors.red,"Lua string"},
  8.         ["number"]= {colors.yellow,"64 bit floating point"},
  9.         ["table"]= {colors.lightBlue,"Lua table"},
  10.         ["boolean"]= {colors.lime,"true/false value"},
  11.         ["function"]= {colors.purple,"A function which you can pass arguments into"},
  12.         ["Color"]={colors.pink,"One of the 16 paint colors from the colors api"},
  13.         ["File"]={colors.blue,"A file handle that can be read from and written onto"}
  14.     }
  15.     if type(tab) ~= "table" then tab = {} end
  16.     for k,v in pairs(types) do
  17.         if not tab[k] then tab[k] = v end
  18.     end
  19.     return tab
  20. end
  21. --Reads the color and information about a specific variable type
  22. --@OptionalArg string varType: the variable type to be read. If not provided, will read all the variable types
  23. --@Returns boolean false,string errorMsg: If the varType was not found
  24. --@Returns table information: {Color color, string comment}, the information for the varType specified
  25. --@Returns table varTypeInfo: A table of varType info for all registered variable types
  26. function readVariableType(varType)
  27.     if not fs.exists("variable_types.json") then
  28.         local file = fs.open("variable_types.json","w")
  29.         local tab = getDefaultTypes()
  30.         local data = textutils.serialiseJSON(tab)
  31.         file.write(data)
  32.         file.close()
  33.         if type(varType) ~= "string" then return tab
  34.         elseif tab[varType] then return tab[varType]
  35.         else return false,"Could not find varType "..varType end
  36.     end
  37.     local file = fs.open("variable_types.json","r")
  38.     local data = textutils.unserialiseJSON(file.readAll())
  39.     if not data then return false, "Invalid JSON in variable_types.json" end
  40.     if type(varType) ~= "string" then return data
  41.     elseif data[varType] then return data[varType]
  42.     else return false,"Could not find varType "..varType end
  43. end
  44.  
  45. --Registers a new variable type (or edits an old one) for documentation purposes
  46. --@Arg string varType: The variable type to be registered
  47. --@OptionalArg Color color: The color that the variable type will be displayed as
  48. --@OptionalArg string comment: Information about the variable type
  49. function registerType(varType,color,comment)
  50.     local types,errorMsg = readVariableType()
  51.     if type(types) ~= "table" then types = {} end
  52.     if types[varType] then
  53.         types[varType] = {color or varType[1],comment or varType[2]}
  54.     else
  55.         types[varType] = {color or colors.gray,comment or ""}
  56.     end
  57.     local data = textutils.serialiseJSON(types)
  58.     local file = fs.open("variable_types.json","w")
  59.     file.write(data)
  60.     file.close()
  61. end
  62. --Seperates a string into a table by a seperator character
  63. --@Arg string input: The input string to be split
  64. --@OptionalArg string sep: The character, or character class to seperate the string by
  65. --@Returns table splitString: A list of strings that got seperated
  66. local function split(input,sep)
  67.     sep = sep or "%s"
  68.     local t = {}
  69.     for str in string.gmatch(input,"([^"..sep.."]+)") do
  70.         table.insert(t,str)
  71.     end
  72.     return t
  73. end
  74. --Gets the arguments from a comment line
  75. --@Arg string comment: the comment before a function
  76. --@OptionalArg table details: the details table where the data gets stored
  77. --@Returns table details: The details table with the comment added
  78. local function addCommentDetail(comment,details)
  79.     if type(details) ~= "table" then details = {} end
  80.     local commentType = string.match(comment,"^@%w+") or "Comments"
  81.     commentType = string.gsub(commentType,"@","")
  82.     if commentType == "Arg" then
  83.         --Too lazy to fill this out
  84.     end
  85.     if type(details[commentType]) ~= "table" then details[commentType] = {} end
  86.     local str = string.gsub(comment,"^@%w+ ","")
  87.     table.insert(details[commentType],str)
  88.     return details
  89. end
  90. --Simple printing function to write arguments with fancy colors
  91. --@Arg string argType: the argument type (string,boolean,table,function,etc)
  92. --@Arg string argName: the argument name
  93. local function writeArg(argType,argName)
  94.     argType = string.gsub(argType," ","")
  95.     local typeTable = readVariableType(argType)
  96.     if typeTable then term.setTextColor(typeTable[1]) else term.setTextColor(colors.lightGray) end
  97.     if argType and argType ~= "None" then term.write(argType.." ")
  98.     else term.setTextColor(colors.red) end
  99.     term.write(argName)
  100. end
  101. --Gets all the methods in a lua file, returning the functions and their documentation
  102. --@Arg string api: absolute path of the file to read documentation
  103. --@OptionalArg number test: Does nothing, just to show OptionalArg
  104. --@Returns boolean False, string errorMsg: If the api could not be found
  105. --@Returns table methods: A list of the valid functions, and their documentation
  106. function getAllMethods(api,test)
  107.     if not fs.exists(api) then return false,"could not find api" end
  108.     local file = fs.open(api,"r")
  109.     local comment = {}
  110.     local methods = {}
  111.     local functionName = ""
  112.     local line = file.readLine()
  113.     while line do
  114.         line = string.gsub(line,"$[%s\t]*","") --Removing whitespace
  115.         if string.sub(line,1,2) == "--" then
  116.             table.insert(comment,string.sub(line,3))
  117.         elseif string.match(line,"^function") or string.match(line,"^local function") then
  118.             local method = {}
  119.             if string.sub(line,1,5) == "local" then
  120.                 method.type = "private"
  121.             else
  122.                 method.type = "public"
  123.             end
  124.             for k,v in pairs(comment) do
  125.                 addCommentDetail(v,method)
  126.             end
  127.             method.header = string.gsub(line,"%w* ?function ","")
  128.             method.args = string.match(method.header,"%b()")
  129.             local args = string.gsub(method.args,"%.","%%.")
  130.             functionName = string.gsub(method.header,args,"")
  131.             functionName = string.gsub(functionName,"[%(%)]","")
  132.             method.name = functionName
  133.             table.insert(methods,method)
  134.             comment = {}
  135.         else
  136.             comment = {}
  137.         end
  138.         line = file.readLine()
  139.     end
  140.     file.close()
  141.     return methods
  142. end
  143. --Prints all of the methods in an api
  144. --@Arg string api: The absolute path of the file to read documentation
  145. --@OptionalArg string method: The specific method to show. If not specified, prints all methods
  146. --@OptionalArg boolean showLocals: Default false, whether local methods should also be shown
  147. --@OptionalArg boolean showReturnComments: Default true, whether comments on the return values should be shown
  148. --@OptionalArg boolean showComments: Default false, whether the comments on the method should be shown
  149. function printMethodSummary(api,method,showLocals,showReturnComments,showAllComments)
  150.     if type(showReturnComments) ~= "boolean" then showReturnComments = true end
  151.     local methods,err = getAllMethods(api)
  152.     if not methods then
  153.         textutils.setTextColor(colors.red)
  154.         print(err)
  155.         textutils.setTextColor(colors.white)
  156.         return
  157.     end
  158.     if type(method) == "string" then
  159.         for k,v in pairs(methods) do
  160.             if v.name == method then
  161.                 methods = {v}
  162.                 showLocals = true
  163.             end
  164.         end
  165.     end
  166.     for k,v in pairs(methods) do
  167.         if v.type == "public" or (showLocals and type(showLocals) == "boolean") then
  168.             local color = colors.gray
  169.             if v.type == "public" then color = colors.yellow end
  170.             term.setTextColor(color)
  171.             term.clearLine()
  172.             term.write(v.name)
  173.             if type(v.Arg) == "table" or type(v.OptionalArg) == "table" or type(v.Args) == "table" then
  174.                 term.write("(")
  175.                 if type(v.Arg) == "table" and #v.Arg >= 1 then
  176.                     for j,w in pairs(v.Arg) do
  177.                         local argType = string.match(w,"^%w+")
  178.                         local argName = string.match(w," %w+:") or " Errored_Arg"
  179.                         argName = string.gsub(argName,"[ :]","")
  180.                         writeArg(argType,argName)
  181.                         term.setTextColor(color)
  182.                         term.write(", ")
  183.                     end
  184.                     --local x,y = term.getCursorPos()
  185.                     --term.setCursorPos(x-2,y) --Clearing the extra comma
  186.                     --term.write("  ")
  187.                     --term.setCursorPos(x-2,y)
  188.                 end
  189.                 if type(v.OptionalArg) == "table" and #v.OptionalArg >= 1 then
  190.                     term.write("[")
  191.                     for j,w in pairs(v.OptionalArg) do
  192.                         local argType = string.match(w,"^%w+")
  193.                         local argName = string.match(w," %w+:") or " Errored_Arg"
  194.                         argName = string.gsub(argName,"[ :]","")
  195.                         writeArg(argType,argName)
  196.                         term.setTextColor(color)
  197.                         term.write(", ")
  198.                     end
  199.                     local x,y = term.getCursorPos()
  200.                     term.setCursorPos(x-2,y)
  201.                     term.write("  ")
  202.                     term.setCursorPos(x-2,y)
  203.                     term.setTextColor(color)
  204.                     term.write("], ")
  205.                 end
  206.                 if type(v.Args) == "table" and #v.Args >= 1 then
  207.                     for j,w in pairs(v.Args) do
  208.                         local argType = string.match(w,"^%w+")
  209.                         local argName = string.match(w," %w+:") or " Errored_Arg"
  210.                         argName = string.gsub(argName,"[ :]","")
  211.                         writeArg(argType,argName)
  212.                         term.setTextColor(color)
  213.                         term.write("...")
  214.                     end
  215.                 else
  216.                     local x,y = term.getCursorPos()
  217.                     term.setCursorPos(x-2,y) --Clearing the extra comma
  218.                     term.write("  ")
  219.                     term.setCursorPos(x-2,y)
  220.                 end
  221.                 term.setTextColor(color)
  222.                 term.write(")")
  223.             else
  224.                 term.write(v.args)
  225.             end
  226.             if type(v.Returns) == "table" then
  227.                 for j,w in pairs(v.Returns) do
  228.                     print()
  229.                     term.clearLine()
  230.                     term.setTextColor(colors.red)
  231.                     term.write("  returns:")
  232.                     local splitString = split(w,":")
  233.                     local returnArgs = split(splitString[1],",")
  234.                     for l,u in pairs(returnArgs) do
  235.                         local argType = string.match(u,"^.?%w+")
  236.                         argType = string.gsub(argType," ","")
  237.                         local argName = string.match(u,"%w+$")
  238.                         writeArg(argType,argName)
  239.                         term.setTextColor(color)
  240.                         term.write(", ")
  241.                     end
  242.                     local x,y = term.getCursorPos()
  243.                     term.setCursorPos(x-2,y)
  244.                     term.write("  ")
  245.                     term.setCursorPos(x-2,y)
  246.                     if (showReturnComments or showComments) and type(splitString[2]) == "string" and #splitString[2] >= 1 then
  247.                         term.setTextColor(colors.green)
  248.                         term.write(" --"..splitString[2])
  249.                     end
  250.                 end
  251.             end
  252.             if showComments then
  253.                 term.setTextColor(colors.green)
  254.                 for j,w in pairs(v) do
  255.                     if j ~= "Returns" and type(w) == "table" and #w > 0 then
  256.                         print()
  257.                         term.clearLine()
  258.                         term.write("  "..j)
  259.                         for l,u in pairs(w) do
  260.                             print()
  261.                             term.clearLine()
  262.                             term.write("    "..u)
  263.                         end
  264.                     end
  265.                 end
  266.             end
  267.             print()
  268.         end
  269.     end
  270.     term.setTextColor(colors.white)
  271. end
Advertisement
Add Comment
Please, Sign In to add comment