Skip to content

Instantly share code, notes, and snippets.

@LPGhatguy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LPGhatguy/6be56f1932b78513ae3d to your computer and use it in GitHub Desktop.
Save LPGhatguy/6be56f1932b78513ae3d to your computer and use it in GitHub Desktop.
Run this is a folder full of 'fdef' files to turn it into a folder of sublime-snippet files. See sample.fdef, sample.sublime-snippet, and sample-all.sublime-snippet.
--[[
]]
local lfs = require("lfs")
local CWD = "./"
function readfile(path)
local handle = io.open(path, "r")
if (handle) then
local data = handle:read("*a")
handle:close()
return data
end
end
function writefile(path, data)
local handle = io.open(path, "w")
if (handle) then
handle:write(data)
handle:close()
return true
end
end
function parse_fdef(source)
local parsed = {}
local desc = source:match("^(.-)\n")
parsed.desc = (#desc > 0) and desc or "no description available"
local name, arguments = source:match("([%w%d%.%-_]+)%(([^%)]+)%)")
parsed.name = name
parsed.arguments = {}
for argument in arguments:gmatch("[^%s,]+") do
table.insert(parsed.arguments, argument)
end
local defaults = {}
for name, value in source:gmatch("%w+ ([%w%d%-_]+) %(([^%)]+)%)") do
if (tonumber(value)) then --number!
defaults[name] = tonumber(value)
elseif (value:match("^\".-\"$")) then --string!
defaults[name] = value
elseif (defaults[value]) then --backreference!
defaults[name] = defaults[value]
else --not sure WHAT this is
end
end
parsed.defaults = defaults
return parsed
end
function build_snippet(parsed, all)
local name, arguments, defaults, desc = parsed.name, parsed.arguments, parsed.defaults, parsed.desc
local buf = {
"<!--Automatically generated by LPGhatguy's LOVE FDEF Parser-->\n<snippet>"
}
local arg_buf = {}
local replace_index = 1
for key, argument in ipairs(arguments) do
if (defaults[argument]) then
if (all) then
table.insert(arg_buf, "${" .. replace_index .. ":" .. argument .. "=}${" .. replace_index + 1 .. ":" .. defaults[argument] .. "}")
replace_index = replace_index + 2
end
else
table.insert(arg_buf, "${" .. replace_index .. ":" .. argument .. "}")
replace_index = replace_index + 1
end
end
table.insert(buf, "\t<content><![CDATA[" .. name .. "(" .. table.concat(arg_buf, ", ") .. ")]]></content>")
table.insert(buf, "\t<tabTrigger>" .. name .. "</tabTrigger>")
table.insert(buf, "\t<scope>source.lua</scope>")
table.insert(buf, ("\t<description><![CDATA[ %s%s]]></description>"):format(desc, all and " (all arguments)" or ""))
table.insert(buf, "</snippet>")
return table.concat(buf, "\n")
end
lfs.mkdir(CWD .. "output/")
for filename in lfs.dir(CWD) do
if (filename:match("fdef$")) then
local target = CWD .. "output/" .. filename:gsub("fdef$", "sublime-snippet")
local targetall = CWD .. "output/" .. filename:gsub("%.fdef$", "-all.sublime-snippet")
local parsed = parse_fdef(readfile(CWD .. filename))
writefile(target, build_snippet(parsed))
if (next(parsed.defaults)) then
writefile(targetall, build_snippet(parsed, true))
end
end
end
<!--Automatically generated by LPGhatguy's LOVE FDEF Parser-->
<snippet>
<content><![CDATA[love.graphics.printf(${1:text}, ${2:x}, ${3:y}, ${4:limit}, ${5:align=}${6:"left"}, ${7:r=}${8:0}, ${9:sx=}${10:1}, ${11:sy=}${12:1}, ${13:ox=}${14:0}, ${15:oy=}${16:0}, ${17:kx=}${18:0}, ${19:ky=}${20:0})]]></content>
<tabTrigger>love.graphics.printf</tabTrigger>
<scope>source.lua</scope>
<description><![CDATA[ Draws formatted text, with word wrap and alignment. (all arguments)]]></description>
</snippet>
Draws formatted text, with word wrap and alignment.
love.graphics.printf( text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )
Arguments
string text
A text string.
number x
The position on the x-axis.
number y
The position on the y-axis.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.
number r (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).
<!--Automatically generated by LPGhatguy's LOVE FDEF Parser-->
<snippet>
<content><![CDATA[love.graphics.printf(${1:text}, ${2:x}, ${3:y}, ${4:limit})]]></content>
<tabTrigger>love.graphics.printf</tabTrigger>
<scope>source.lua</scope>
<description><![CDATA[ Draws formatted text, with word wrap and alignment.]]></description>
</snippet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment