Skip to content

Commit

Permalink
feat(docgen): parse config tables
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Feb 3, 2023
1 parent b77fbd5 commit 93c41e1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
62 changes: 62 additions & 0 deletions docgen/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ docgen.parse_top_comment = function(comment)
else
local option_name, value = line:match("^%s*(%w+):%s*(.+)")

if vim.tbl_contains({ "true", "false" }, value) then
value = (value == "true")
end

if option_name and can_have_options then
result[option_name:lower()] = value
else
Expand Down Expand Up @@ -127,6 +131,64 @@ docgen.check_top_comment_integrity = function(top_comment)
return top_comment
end

--- TODO
---@param buffer number #Buffer ID
---@param root userdata #The root node
---@return userdata? #Root node
docgen.get_module_config_node = function(buffer, root)
local query = vim.treesitter.parse_query("lua", [[
(assignment_statement
(variable_list) @_name
(#eq? @_name "module.config.public")) @declaration
]])

local _, declaration_node = query:iter_captures(root, buffer)()

return declaration_node and declaration_node:named_child(1):named_child(0) or nil
end

--- TODO
---@param start_node userdata #Node
---@param callback function
docgen.map_config = function(buffer, start_node, callback)
local comments = {}

local query = vim.treesitter.parse_query("lua", [[
((comment)+ @comment
.
(field) @field)
]])

for capture_id, node in query:iter_captures(start_node, buffer) do
local capture = query.captures[capture_id]

if capture == "comment" then
table.insert(comments, ts.get_node_text(node, buffer))
elseif capture == "field" and node:parent():id() == start_node:id() then
local name = ts.get_node_text(node:named_child(0), buffer)
local value = node:named_child(1)

if value:type() == "table_constructor" then
callback({
node = node,
name = name,
value = value,
}, comments)

docgen.map_config(buffer, node, callback)
else
callback({
node = node,
name = name,
value = value,
}, comments)
end

comments = {}
else
comments = {}
end
end
end

return docgen
40 changes: 38 additions & 2 deletions docgen/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
local docgen = require("docgen")
local modules = {}
local modules = {
--[[
[name] = {
docgen_data...
buffer = id,
parsed = `ret value from sourcing the file`,
}
--]]
}

for _, file in ipairs(docgen.aggregate_module_files()) do
local buffer = docgen.open_file(vim.fn.fnamemodify(file, ":p"))
local fullpath = vim.fn.fnamemodify(file, ":p")

local buffer = docgen.open_file(fullpath)

local top_comment = docgen.get_module_top_comment(buffer)

Expand All @@ -14,7 +24,33 @@ for _, file in ipairs(docgen.aggregate_module_files()) do

if type(docgen_data) == "string" then
log.error("Error when parsing module '" .. file .. "': " .. docgen_data)
goto continue
end

local ok, parsed_module = pcall(dofile, fullpath)

if not ok then
log.error("Error when sourcing module '" .. file .. ": " .. parsed_module)
return
end

modules[parsed_module.name] = {
data = docgen_data,
buffer = buffer,
parsed = parsed_module,
}

::continue::
end

for module_name, module in pairs(modules) do
local buffer = module.buffer

local root = vim.treesitter.get_parser(buffer, "lua"):parse()[1]:root()
local config_node = docgen.get_module_config_node(buffer, root)

if config_node then
docgen.map_config(buffer, config_node, function(child, comment)
end)
end
end

0 comments on commit 93c41e1

Please sign in to comment.