Skip to content

Commit

Permalink
move url-building to buildstep; Type funcs now with dot operator inst…
Browse files Browse the repository at this point in the history
…ead of colon operator
  • Loading branch information
kevinfiol committed Apr 23, 2021
1 parent 49281b8 commit 53f1161
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 66 deletions.
79 changes: 72 additions & 7 deletions api/main.lua
Expand Up @@ -2,6 +2,29 @@ local api = require 'love_api'
local json = require 'json'
local inspect = require 'inspect'

function string:split(delimiter)
local result = {}
local from = 1
local delim_from, delim_to = string.find(self, delimiter, from, true)
while delim_from do
if (delim_from ~= 1) then
table.insert(result, string.sub(self, from, delim_from-1))
end
from = delim_to + 1
delim_from, delim_to = string.find(self, delimiter, from, true)
end
if (from <= #self) then table.insert(result, string.sub(self, from)) end
return result
end

function makeWikiLink(endpoint)
return 'https://love2d.org/wiki/' .. endpoint
end

function makeApiLink(endpoint)
return 'https://love2d-community.github.io/love-api/#' .. endpoint
end

function exists(x)
return x ~= nil
end
Expand All @@ -10,17 +33,47 @@ function ternary(cond, T, F)
if cond then return T else return F end
end

function parseFunctions(t, parent, namespace, opt)
if opt == nil then
opt = '.'
end
function parseFunctions(t, parent, namespace, isTypeFuncs)
if isTypeFuncs == nil then isTypeFuncs = false end
for _, fn in ipairs(t) do
local name = namespace .. opt .. fn.name
local name = namespace .. '.' .. fn.name

-- string manip stuff for links
-- possible namespaces
-- love.module.Type
-- love.module
-- love.Type

local tokens = namespace:split('.')
local wikiLink = nil
local apiLink = nil

if isTypeFuncs then
local typeName = nil
if #tokens == 3 then typeName = tokens[3] end
if #tokens == 2 then typeName = tokens[2] end
wikiLink = makeWikiLink(typeName .. ':' .. fn.name)
apiLink = makeApiLink(typeName .. '_' .. fn.name)
else
if #tokens == 2 then
local moduleName = tokens[2]
wikiLink = makeWikiLink('love.' .. moduleName .. '.' .. fn.name)
apiLink = makeApiLink(moduleName .. '_' .. fn.name)
else
wikiLink = makeWikiLink('love.' .. fn.name)
apiLink = makeApiLink(fn.name)
end
end

parent[name] = {
meta = {
prop_type = 'function',
is_type_func = isTypeFuncs,
name = fn.name,
namespace = namespace,
description = fn.description,
api_link = apiLink,
wiki_link = wikiLink,
returns = ternary(exists(fn.variants[1].returns), fn.variants[1].returns, nil),
arguments = ternary(exists(fn.variants[1].arguments), fn.variants[1].arguments, nil)
}
Expand All @@ -32,11 +85,17 @@ function parseTypes(t, parent, namespace)
for _, ty in ipairs(t) do
if exists(ty.name) then
local name = namespace .. '.' .. ty.name
local wikiLink = makeWikiLink(ty.name)
local apiLink = makeApiLink('type_' .. ty.name)

parent[name] = {
meta = {
prop_type = 'type',
name = ty.name,
namespace = namespace,
description = ty.description,
api_link = apiLink,
wiki_link = wikiLink,
constructors = ternary(exists(ty.constructors), ty.constructors, nil),
supertypes = ternary(ty.supertypes, ty.supertypes, nil)
}
Expand All @@ -48,7 +107,7 @@ function parseTypes(t, parent, namespace)
-- nil
-- )
if ty.functions ~= nil then
parseFunctions(ty.functions, parent, name, ':')
parseFunctions(ty.functions, parent, name, true)
end
end
end
Expand All @@ -58,11 +117,17 @@ function parseModules(t, parent, namespace)
for _, mod in ipairs(t) do
if exists(mod.name) then
local name = namespace .. '.' .. mod.name
local wikiLink = makeWikiLink('love.' .. mod.name)
local apiLink = makeApiLink(mod.name)

parent[name] = {
meta = {
prop_type = 'module',
name = mod.name,
description = mod.description
namespace = namespace,
description = mod.description,
api_link = apiLink,
wiki_link = wikiLink
}
}

Expand Down
15 changes: 7 additions & 8 deletions api/reorder_json.js
@@ -1,15 +1,9 @@
const json = require('./output.json');

const typeFuncs = {}
const firstLevel = {};
const secondLevel = {};
const thirdLevel = {}
for (let key in json) {
if (key.split(':').length == 2) {
typeFuncs[key] = json[key];
delete json[key];
continue;
}

if (key.split('.').length == 2) {
firstLevel[key] = json[key];
delete json[key];
Expand All @@ -19,12 +13,17 @@ for (let key in json) {
secondLevel[key] = json[key];
delete json[key];
}

if (key.split('.').length == 4) {
thirdLevel[key] = json[key];
delete json[key];
}
}

const reordered = {
...firstLevel,
...secondLevel,
...typeFuncs
...thirdLevel
};

require('fs').writeFileSync('./output.json', JSON.stringify(reordered));
2 changes: 1 addition & 1 deletion love_api.json

Large diffs are not rendered by default.

51 changes: 1 addition & 50 deletions lovely2d.py
Expand Up @@ -22,7 +22,6 @@ def run(self, edit, key, point, hide_on_mouse_move):
content = self.contentCache[key]
else:
meta = self.api[key]['meta']
links = self.get_links(key, meta)

signature = None
if meta['prop_type'] == 'function':
Expand Down Expand Up @@ -83,7 +82,7 @@ def run(self, edit, key, point, hide_on_mouse_move):
'<div id="sublime_love__description" style="padding: 0.2rem 0;"><span>{}</span></div>'
.format(meta['description']) +
'<div id="sublime_love__links"><a href="{}">Wiki</a> | <a href="{}">API</a></div>'
.format(links['wiki_link'], links['api_link']) +
.format(meta['wiki_link'], meta['api_link']) +
'</div>'
)

Expand All @@ -97,54 +96,6 @@ def run(self, edit, key, point, hide_on_mouse_move):
location=point
)

def get_links(self, key, meta):
wiki_link = 'https://love2d.org/wiki/'
api_link = 'https://love2d-community.github.io/love-api/#'

fn = None
module = None
type_name = None

if meta['prop_type'] == 'function':
temp = key.split(':')
fn = meta['name']
if len(temp) == 2:
# it's a type func
temp = temp[0]
temp = temp.split('.')
if len(temp) == 3:
module = temp[1]
type_name = temp[2]
elif len(temp) == 2:
type_name = temp[1]
wiki_link += '{}:{}'.format(type_name, fn)
api_link += '{}_{}'.format(type_name, fn)
elif len(temp) == 1:
# it's a module func or top-level callback
temp = temp[0]
temp = temp.split('.')
if len(temp) == 3:
module = temp[1]

if module:
wiki_link += 'love.{}.{}'.format(module, fn)
api_link += '{}_{}'.format(module, fn)
else:
wiki_link += 'love.{}'.format(fn)
api_link += fn
elif meta['prop_type'] == 'module':
module = meta['name']
wiki_link += 'love.{}'.format(module)
api_link += module
elif meta['prop_type'] == 'type':
temp = key.split('.')
module = temp[1]
type_name = meta['name']
wiki_link += type_name
api_link += 'type_{}'.format(type_name)

return { "wiki_link": wiki_link, "api_link": api_link }

class LoveListener(sublime_plugin.EventListener):
api = None

Expand Down

0 comments on commit 53f1161

Please sign in to comment.