Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
feat: improved vim.api and vim parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 11, 2022
1 parent 0e7a16b commit 60fce6d
Show file tree
Hide file tree
Showing 6 changed files with 653 additions and 106 deletions.
1 change: 0 additions & 1 deletion lua/lua-dev/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,5 @@ function M.parse_signature(line)
return { name = name, params = params, doc = doc }
end
end
dumpp(M.lua())

return M
68 changes: 43 additions & 25 deletions lua/lua-dev/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,34 +204,55 @@ function M.get_functions(mpack)
return ret
end

function M.parse(mpack, prefix, exclude)
print(prefix)
-- exclude signatures for functions that are existing lua sources
local skip = {}
for _, value in pairs(exclude or {}) do
for key, _ in pairs(require(value)) do
print("skipping " .. value .. ": " .. key)
skip[key] = true
---@return string?, any?
function M.get_fn(prefix, name, alt)
for _, p in ipairs(alt and { prefix, alt } or { prefix }) do
local real_fn = vim.tbl_get(_G, unpack(vim.split(p .. "." .. name, ".", { plain = true })))
if real_fn then
return p, real_fn
end
end
end

---@param opts? {alt: string, extra: table<string,ApiFunction>}
function M.parse(mpack, prefix, opts)
opts = opts or {}
print(prefix)
prefix = prefix or "vim"
local fname = vim.fn.fnamemodify(mpack, ":t:r")

local writer = M.writer("types/" .. fname)
local classes = {}
for _, f in pairs(M.get_functions(mpack)) do
local name, fun = unpack(f)
if not skip[name] then
local parts = vim.fn.split(name, ":")
if #parts > 1 and not classes[parts[1]] then
writer.write(([[
--- @class %s
%s = {}
]]):format(prefix .. "." .. parts[1], prefix .. "." .. parts[1]))
classes[parts[1]] = true
local name = f[1]
local fun = f[2]

-- we shouldnt get classes here
assert(not name:find(":"))

local prefix_fn, real_fn = M.get_fn(prefix, name, opts.alt)

if prefix_fn then
local skip = false

if real_fn then
if type(real_fn) == "function" then
if prefix_fn ~= "vim.fn" and prefix_fn ~= "vim.api" then
local info = debug.getinfo(real_fn, "S")
skip = info.what == "Lua"
end
end
end

if not skip then
local emmy = M.emmy(M.process(name, fun, prefix_fn))
writer.write(emmy)
end
local emmy = M.emmy(M.process(name, fun, prefix))
end
end

if opts.extra then
for _, fun in pairs(opts.extra) do
local emmy = M.emmy(fun)
writer.write(emmy)
end
end
Expand Down Expand Up @@ -332,11 +353,8 @@ function M.build()
M.clean()
M.functions()
M.options()
-- M.parse("lua.mpack", "vim", { "vim.uri", "vim.inspect" })
M.parse("api.mpack", "vim.api")
-- M.parse("diagnostic.mpack", "vim.diagnostic")
-- M.parse("lsp.mpack", "vim.lsp", { "vim.lsp" })
-- M.parse("treesitter.mpack", "vim.treesitter", { "vim.treesitter" })
M.parse("lua.mpack", "vim", { extra = require("lua-dev.docs").lua() })
M.parse("api.mpack", "vim.api", { alt = "vim.fn" })
end

M.build()
Expand Down
30 changes: 15 additions & 15 deletions types/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function vim.api.nvim_buf_delete(buffer, opts) end
--- @param buffer buffer # Buffer handle, or 0 for current buffer
--- @return any # False if detach failed (because the buffer isn't loaded); otherwise
-- True.
function vim.api.nvim_buf_detach(buffer) end
function vim.fn.nvim_buf_detach(buffer) end

-- Gets a changed tick of a buffer
--- @param buffer buffer # Buffer handle, or 0 for current buffer
Expand Down Expand Up @@ -651,7 +651,7 @@ function vim.api.nvim_buf_set_var(buffer, name, value) end
-- which resulted in an error, the error type and the error message. If
-- an error occurred, the values from all preceding calls will still be
-- returned.
function vim.api.nvim_call_atomic(calls) end
function vim.fn.nvim_call_atomic(calls) end

-- Calls a VimL |Dictionary-function| with the given arguments.
--
Expand Down Expand Up @@ -1019,7 +1019,7 @@ function vim.api.nvim_err_writeln(str) end

--- @param lvl integer
--- @param data string
function vim.api.nvim_error_event(lvl, data) end
function vim.fn.nvim_error_event(lvl, data) end

-- Evaluates a VimL |expression|. Dictionaries and Lists are recursively
-- expanded.
Expand Down Expand Up @@ -1095,7 +1095,7 @@ function vim.api.nvim_exec_autocmds(event, opts) end
--- @param code string # Lua code to execute
--- @param args array # Arguments to the code
--- @return any # Return value of Lua code if present or NIL.
function vim.api.nvim_exec_lua(code, args) end
function vim.fn.nvim_exec_lua(code, args) end

-- Sends input-keys to Nvim, subject to various quirks controlled by `mode`
-- flags. This is a blocking call, unlike |nvim_input()|.
Expand Down Expand Up @@ -1131,7 +1131,7 @@ function vim.api.nvim_get_all_options_info() end
-- Returns a 2-tuple (Array), where item 0 is the current channel id and item
-- 1 is the |api-metadata| map (Dictionary).
--- @return any # 2-tuple [{channel-id}, {api-metadata}]
function vim.api.nvim_get_api_info() end
function vim.fn.nvim_get_api_info() end

-- Get all autocommands that match the corresponding {opts}.
--
Expand Down Expand Up @@ -1876,7 +1876,7 @@ function vim.api.nvim_select_popupmenu_item(item, insert, finish, opts) end
-- "MIT", …)
-- • "logo": URI or path to image, preferably small logo or
-- icon. .png or .svg format is preferred.
function vim.api.nvim_set_client_info(name, version, type, methods, attributes) end
function vim.fn.nvim_set_client_info(name, version, type, methods, attributes) end

-- Sets the current buffer.
--- @param buffer buffer # Buffer handle
Expand Down Expand Up @@ -2065,7 +2065,7 @@ function vim.api.nvim_strwidth(text) end

-- Subscribes to event broadcasts.
--- @param event string # Event type string
function vim.api.nvim_subscribe(event) end
function vim.fn.nvim_subscribe(event) end

-- Removes a tab-scoped (t:) variable
--- @param tabpage tabpage # Tabpage handle, or 0 for current tabpage
Expand Down Expand Up @@ -2118,12 +2118,12 @@ function vim.api.nvim_tabpage_set_var(tabpage, name, value) end
--- @param width integer # Requested screen columns
--- @param height integer # Requested screen rows
--- @param options dictionary # |ui-option| map
function vim.api.nvim_ui_attach(width, height, options) end
function vim.fn.nvim_ui_attach(width, height, options) end

-- Deactivates UI events on the channel.
--
-- Removes the client from the list of UIs. |nvim_list_uis()|
function vim.api.nvim_ui_detach() end
function vim.fn.nvim_ui_detach() end

-- Tells Nvim the geometry of the popupmenu, to align floating windows with
-- an external popup menu.
Expand All @@ -2138,20 +2138,20 @@ function vim.api.nvim_ui_detach() end
--- @param height float # Popupmenu height.
--- @param row float # Popupmenu row.
--- @param col float # Popupmenu height.
function vim.api.nvim_ui_pum_set_bounds(width, height, row, col) end
function vim.fn.nvim_ui_pum_set_bounds(width, height, row, col) end

-- Tells Nvim the number of elements displaying in the popupmenu, to decide
-- <PageUp> and <PageDown> movement.
--- @param height integer # Popupmenu height, must be greater than zero.
function vim.api.nvim_ui_pum_set_height(height) end
function vim.fn.nvim_ui_pum_set_height(height) end

--- @param name string
--- @param value object
function vim.api.nvim_ui_set_option(name, value) end
function vim.fn.nvim_ui_set_option(name, value) end

--- @param width integer
--- @param height integer
function vim.api.nvim_ui_try_resize(width, height) end
function vim.fn.nvim_ui_try_resize(width, height) end

-- Tell Nvim to resize a grid. Triggers a grid_resize event with the
-- requested grid size or the maximum size if it exceeds size limits.
Expand All @@ -2160,11 +2160,11 @@ function vim.api.nvim_ui_try_resize(width, height) end
--- @param grid integer # The handle of the grid to be changed.
--- @param width integer # The new requested width.
--- @param height integer # The new requested height.
function vim.api.nvim_ui_try_resize_grid(grid, width, height) end
function vim.fn.nvim_ui_try_resize_grid(grid, width, height) end

-- Unsubscribes to event broadcasts.
--- @param event string # Event type string
function vim.api.nvim_unsubscribe(event) end
function vim.fn.nvim_unsubscribe(event) end

-- Calls a function with window as temporary current window.
--- @see |win_execute()|
Expand Down
Loading

0 comments on commit 60fce6d

Please sign in to comment.