Clean up file and abstract root love module parsing

This commit is contained in:
Emma 2024-08-10 16:55:36 -04:00
parent 0cb16a6456
commit 3816bd81cc

View File

@ -7,13 +7,9 @@
(local stringify-table fennel.view)
(fn download-love-api-tooling! []
"Clones the LÖVE-API git repository that contains tooling to scrape and
convert the LÖVE Wiki into a Lua table."
(when (not (io.open :build/love-api))
(git-clone love-api-build-directory
"https://github.com/love2d-community/love-api")))
;
; UTILS
; -----
(fn merge [...]
(let [arg-count (select "#" ...)
args [...]]
@ -25,6 +21,23 @@
(values k (merge v (. result k)))
(values k v)))))))
(fn download-love-api-tooling! []
"Clones the LÖVE-API git repository that contains tooling to scrape and
convert the LÖVE Wiki into a Lua table."
(when (not (io.open :build/love-api))
(git-clone love-api-build-directory
"https://github.com/love2d-community/love-api")))
(fn build-lsp-value [name ?args ?docstring]
"Takes ... and returns a table to be used with the LSP."
(let [lsp-value {:binding name :metadata {}}]
(when ?args (set lsp-value.metadata.fnl/arglist ?args))
(when ?docstring (set lsp-value.metadata.fnl/docstring ?docstring))
lsp-value))
;
; PARSERS
; -------
(fn fn-arguments->names [arguments]
"Given an array of arguments, return all names as an array."
(icollect [_i {:description _ : name :type _} (ipairs arguments)]
@ -44,45 +57,33 @@
:returns (values :returns v-value)
:arguments (values :args (fn-arguments->names v-value)))))
; (fn build-arg-name-list [arguments-tbl]
; "Returns a list of all argument names"
; (print (.. "CHECK OUT THIS TABLE —" (fennel.view arguments-tbl)))
; (icollect [_ v (ipairs arguments-tbl)]
; v.name))
(fn parse-doc-module-tbl [docs-tbl]
"Takes a LÖVE-API documentation table and generates a list of each
key with values suitable for the Fennel LSP."
{:fields (collect [_i value (ipairs docs-tbl)]
(let [{: name : description} value
?variants (?. value :variants)
first-variant (if ?variants
(parse-fn-variant (. ?variants 1))
nil)
?args (?. first-variant :args)
?returns (?. first-variant :returns)
docstring (.. description
(if ?returns (fn-return->string ?returns) ""))]
(values name (build-lsp-value name ?args docstring))))})
(fn build-lsp-value [name ?args ?docstring]
"Takes ... and returns a table to be used with the LSP."
(let [lsp-value {:binding name :metadata {}}]
(when ?args (set lsp-value.metadata.fnl/arglist ?args))
(when ?docstring (set lsp-value.metadata.fnl/docstring ?docstring))
lsp-value))
; TODO - This function takes the functions object directly, then it can be used
; for any high-level object.
(fn get-love-functions [docs-tbl]
"Takes the LÖVE-API documentation table and generates a list of the
library-level functions suitable for the Fennel LSP."
(collect [_i value (ipairs docs-tbl)]
(let [{: name : description} value
?variants (?. value :variants)
first-variant (if ?variants
(parse-fn-variant (. ?variants 1))
nil)
?args (?. first-variant :args)
?returns (?. first-variant :returns)
docstring (.. description (if ?returns (fn-return->string ?returns)
""))]
(values name (build-lsp-value name ?args docstring)))))
(fn love-api->root-lsp-tbl [love-api]
(let [love-doc-string (.. "LÖVE is a framework for making "
"2D games in the Lua programming language.")
love-docs (build-lsp-value :love nil love-doc-string)
love-functions (parse-doc-module-tbl love-api.functions)
love-callbacks (parse-doc-module-tbl love-api.callbacks)]
{:love (merge love-docs love-functions love-callbacks)}))
(fn convert []
(download-love-api-tooling!)
(let [love-docs-tbl (require-love-api)
love-doc-string (.. "LÖVE is a framework for making "
"2D games in the Lua programming language.")
love-docs (build-lsp-value :love nil love-doc-string)
love-functions {:fields (get-love-functions love-docs-tbl.functions)}
love-callbacks {:fields (get-love-functions love-docs-tbl.callbacks)}]
(stringify-table {:love (merge love-docs love-functions love-callbacks)})))
(let [love-api (require-love-api)
root-lsp-tbl (love-api->root-lsp-tbl love-api)]
(stringify-table root-lsp-tbl)))
{: convert}