feat(docs): render class dot members as module functions

AI-assisted: Codex
This commit is contained in:
Yi Ming
2026-04-15 22:12:18 +08:00
parent 481e70550c
commit 37aa66c1a2
6 changed files with 200 additions and 113 deletions

View File

@@ -39,12 +39,12 @@ local validate = vim.validate
---@field private [1] integer underlying representation of row
---@field private [2] integer underlying representation of col
---@field private [3] integer underlying representation of buf
local Pos = {}
local M = {}
---@private
---@param pos vim.Pos
---@param key any
function Pos.__index(pos, key)
function M.__index(pos, key)
if key == 'row' then
return pos[1]
elseif key == 'col' then
@@ -53,14 +53,14 @@ function Pos.__index(pos, key)
return pos[3]
end
return Pos[key]
return M[key]
end
---@package
---@param buf integer
---@param row integer
---@param col integer
function Pos.new(buf, row, col)
function M.new(buf, row, col)
validate('buf', buf, 'number')
validate('row', row, 'number')
validate('col', col, 'number')
@@ -70,7 +70,7 @@ function Pos.new(buf, row, col)
row,
col,
buf,
}, Pos)
}, M)
return self
end
@@ -98,17 +98,17 @@ local function cmp_pos(p1, p2)
end
---@private
function Pos.__lt(...)
function M.__lt(...)
return cmp_pos(...) == -1
end
---@private
function Pos.__le(...)
function M.__le(...)
return cmp_pos(...) ~= 1
end
---@private
function Pos.__eq(...)
function M.__eq(...)
return cmp_pos(...) == 0
end
@@ -131,7 +131,7 @@ end
--- ```
---@param pos vim.Pos
---@param position_encoding lsp.PositionEncodingKind
function Pos.to_lsp(pos, position_encoding)
function M.to_lsp(pos, position_encoding)
validate('pos', pos, 'table')
validate('position_encoding', position_encoding, 'string')
@@ -161,7 +161,7 @@ end
---@param buf integer
---@param pos lsp.Position
---@param position_encoding lsp.PositionEncodingKind
function Pos.lsp(buf, pos, position_encoding)
function M.lsp(buf, pos, position_encoding)
validate('buf', buf, 'number')
validate('pos', pos, 'table')
validate('position_encoding', position_encoding, 'string')
@@ -175,27 +175,27 @@ function Pos.lsp(buf, pos, position_encoding)
col = vim.str_byteindex(get_line(buf, row), position_encoding, col, false)
end
return Pos.new(buf, row, col)
return M.new(buf, row, col)
end
--- Converts |vim.Pos| to cursor position (see |api-indexing|).
---@param pos vim.Pos
---@return integer, integer
function Pos.to_cursor(pos)
function M.to_cursor(pos)
return pos.row + 1, pos.col
end
--- Creates a new |vim.Pos| from cursor position (see |api-indexing|).
---@param buf integer
---@param pos [integer, integer]
function Pos.cursor(buf, pos)
return Pos.new(buf, pos[1] - 1, pos[2])
function M.cursor(buf, pos)
return M.new(buf, pos[1] - 1, pos[2])
end
--- Converts |vim.Pos| to extmark position (see |api-indexing|).
---@param pos vim.Pos
---@return integer, integer
function Pos.to_extmark(pos)
function M.to_extmark(pos)
local line_count = api.nvim_buf_line_count(pos.buf)
local row = pos.row
@@ -212,16 +212,16 @@ end
---@param buf integer
---@param row integer
---@param col integer
function Pos.extmark(buf, row, col)
return Pos.new(buf, row, col)
function M.extmark(buf, row, col)
return M.new(buf, row, col)
end
-- Overload `Range.new` to allow calling this module as a function.
setmetatable(Pos, {
setmetatable(M, {
__call = function(_, ...)
return Pos.new(...)
return M.new(...)
end,
})
---@cast Pos +fun(buf: integer, row: integer, col: integer): vim.Pos
---@cast M +fun(buf: integer, row: integer, col: integer): vim.Pos
return Pos
return M