fix(diagnostic): correct type annotations; add Diagnostic type (#21120)

Some functions didn't include the `nil` case in the return type
annotation. This corrects those and also adds a Diagnostic class
definition for the diagnostic.get return type
This commit is contained in:
Mathias Fußenegger 2022-11-21 22:02:18 +01:00 committed by GitHub
parent 5f7560b8da
commit 904d0056d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 33 deletions

View File

@ -431,7 +431,7 @@ fromqflist({list}) *vim.diagnostic.fromqflist()*
|getloclist()|. |getloclist()|.
Return: ~ Return: ~
array of diagnostics |diagnostic-structure| Diagnostic [] array of |diagnostic-structure|
get({bufnr}, {opts}) *vim.diagnostic.get()* get({bufnr}, {opts}) *vim.diagnostic.get()*
Get current diagnostics. Get current diagnostics.
@ -446,7 +446,7 @@ get({bufnr}, {opts}) *vim.diagnostic.get()*
• severity: See |diagnostic-severity|. • severity: See |diagnostic-severity|.
Return: ~ Return: ~
(table) A list of diagnostic items |diagnostic-structure|. Diagnostic [] table A list of diagnostic items |diagnostic-structure|.
get_namespace({namespace}) *vim.diagnostic.get_namespace()* get_namespace({namespace}) *vim.diagnostic.get_namespace()*
Get namespace metadata. Get namespace metadata.
@ -467,37 +467,39 @@ get_next({opts}) *vim.diagnostic.get_next()*
Get the next diagnostic closest to the cursor position. Get the next diagnostic closest to the cursor position.
Parameters: ~ Parameters: ~
• {opts} (table) See |vim.diagnostic.goto_next()| • {opts} (table|nil) See |vim.diagnostic.goto_next()|
Return: ~ Return: ~
(table) Next diagnostic Diagnostic|nil Next diagnostic
get_next_pos({opts}) *vim.diagnostic.get_next_pos()* get_next_pos({opts}) *vim.diagnostic.get_next_pos()*
Return the position of the next diagnostic in the current buffer. Return the position of the next diagnostic in the current buffer.
Parameters: ~ Parameters: ~
• {opts} (table) See |vim.diagnostic.goto_next()| • {opts} (table|nil) See |vim.diagnostic.goto_next()|
Return: ~ Return: ~
(table) Next diagnostic position as a (row, col) tuple. table|false Next diagnostic position as a (row, col) tuple or false if
no next diagnostic.
get_prev({opts}) *vim.diagnostic.get_prev()* get_prev({opts}) *vim.diagnostic.get_prev()*
Get the previous diagnostic closest to the cursor position. Get the previous diagnostic closest to the cursor position.
Parameters: ~ Parameters: ~
• {opts} (table) See |vim.diagnostic.goto_next()| • {opts} nil|table See |vim.diagnostic.goto_next()|
Return: ~ Return: ~
(table) Previous diagnostic Diagnostic|nil Previous diagnostic
get_prev_pos({opts}) *vim.diagnostic.get_prev_pos()* get_prev_pos({opts}) *vim.diagnostic.get_prev_pos()*
Return the position of the previous diagnostic in the current buffer. Return the position of the previous diagnostic in the current buffer.
Parameters: ~ Parameters: ~
• {opts} (table) See |vim.diagnostic.goto_next()| • {opts} (table|nil) See |vim.diagnostic.goto_next()|
Return: ~ Return: ~
(table) Previous diagnostic position as a (row, col) tuple. table|false Previous diagnostic position as a (row, col) tuple or
false if there is no prior diagnostic
goto_next({opts}) *vim.diagnostic.goto_next()* goto_next({opts}) *vim.diagnostic.goto_next()*
Move to the next diagnostic. Move to the next diagnostic.
@ -537,8 +539,8 @@ hide({namespace}, {bufnr}) *vim.diagnostic.hide()*
|vim.diagnostic.disable()|. |vim.diagnostic.disable()|.
Parameters: ~ Parameters: ~
• {namespace} (number|nil) Diagnostic namespace. When omitted, hide • {namespace} (number|nil) Diagnostic namespace. When omitted, hide diagnostics from all
diagnostics from all namespaces. namespaces.
• {bufnr} (number|nil) Buffer number, or 0 for current buffer. When • {bufnr} (number|nil) Buffer number, or 0 for current buffer. When
omitted, hide diagnostics in all buffers. omitted, hide diagnostics in all buffers.
@ -571,8 +573,8 @@ match({str}, {pat}, {groups}, {severity_map}, {defaults})
default to 0 and "severity" defaults to ERROR. default to 0 and "severity" defaults to ERROR.
Return: ~ Return: ~
diagnostic |diagnostic-structure| or `nil` if {pat} fails to match Diagnostic|nil: |diagnostic-structure| or `nil` if {pat} fails to
{str}. match {str}.
open_float({opts}, {...}) *vim.diagnostic.open_float()* open_float({opts}, {...}) *vim.diagnostic.open_float()*
Show diagnostics in a floating window. Show diagnostics in a floating window.
@ -628,7 +630,7 @@ open_float({opts}, {...}) *vim.diagnostic.open_float()*
from |vim.diagnostic.config()|. from |vim.diagnostic.config()|.
Return: ~ Return: ~
tuple ({float_bufnr}, {win_id}) number|nil, number|nil: ({float_bufnr}, {win_id})
reset({namespace}, {bufnr}) *vim.diagnostic.reset()* reset({namespace}, {bufnr}) *vim.diagnostic.reset()*
Remove all diagnostics from the given namespace. Remove all diagnostics from the given namespace.
@ -639,8 +641,8 @@ reset({namespace}, {bufnr}) *vim.diagnostic.reset()*
re-displayed, use |vim.diagnostic.hide()|. re-displayed, use |vim.diagnostic.hide()|.
Parameters: ~ Parameters: ~
• {namespace} (number|nil) Diagnostic namespace. When omitted, remove • {namespace} (number|nil) Diagnostic namespace. When omitted, remove diagnostics from all
diagnostics from all namespaces. namespaces.
• {bufnr} (number|nil) Remove diagnostics for the given buffer. • {bufnr} (number|nil) Remove diagnostics for the given buffer.
When omitted, diagnostics are removed for all buffers. When omitted, diagnostics are removed for all buffers.
@ -688,8 +690,8 @@ show({namespace}, {bufnr}, {diagnostics}, {opts})
Display diagnostics for the given namespace and buffer. Display diagnostics for the given namespace and buffer.
Parameters: ~ Parameters: ~
• {namespace} (number|nil) Diagnostic namespace. When omitted, show • {namespace} (number|nil) Diagnostic namespace. When omitted, show diagnostics from all
diagnostics from all namespaces. namespaces.
• {bufnr} (number|nil) Buffer number, or 0 for current buffer. • {bufnr} (number|nil) Buffer number, or 0 for current buffer.
When omitted, show diagnostics in all buffers. When omitted, show diagnostics in all buffers.
• {diagnostics} (table|nil) The diagnostics to display. When omitted, • {diagnostics} (table|nil) The diagnostics to display. When omitted,
@ -709,6 +711,6 @@ toqflist({diagnostics}) *vim.diagnostic.toqflist()*
• {diagnostics} (table) List of diagnostics |diagnostic-structure|. • {diagnostics} (table) List of diagnostics |diagnostic-structure|.
Return: ~ Return: ~
array of quickfix list items |setqflist-what| table[] of quickfix list items |setqflist-what|
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl: vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:

View File

@ -2,6 +2,7 @@ local api, if_nil = vim.api, vim.F.if_nil
local M = {} local M = {}
---@enum DiagnosticSeverity
M.severity = { M.severity = {
ERROR = 1, ERROR = 1,
WARN = 2, WARN = 2,
@ -755,6 +756,18 @@ function M.get_namespaces()
return vim.deepcopy(all_namespaces) return vim.deepcopy(all_namespaces)
end end
---@class Diagnostic
---@field buffer number
---@field lnum number 0-indexed
---@field end_lnum nil|number 0-indexed
---@field col number 0-indexed
---@field end_col nil|number 0-indexed
---@field severity DiagnosticSeverity
---@field message string
---@field source nil|string
---@field code nil|string
---@field user_data nil|any arbitrary data plugins can add
--- Get current diagnostics. --- Get current diagnostics.
--- ---
---@param bufnr number|nil Buffer number to get diagnostics from. Use 0 for ---@param bufnr number|nil Buffer number to get diagnostics from. Use 0 for
@ -763,7 +776,7 @@ end
--- - namespace: (number) Limit diagnostics to the given namespace. --- - namespace: (number) Limit diagnostics to the given namespace.
--- - lnum: (number) Limit diagnostics to the given line number. --- - lnum: (number) Limit diagnostics to the given line number.
--- - severity: See |diagnostic-severity|. --- - severity: See |diagnostic-severity|.
---@return table A list of diagnostic items |diagnostic-structure|. ---@return Diagnostic[] table A list of diagnostic items |diagnostic-structure|.
function M.get(bufnr, opts) function M.get(bufnr, opts)
vim.validate({ vim.validate({
bufnr = { bufnr, 'n', true }, bufnr = { bufnr, 'n', true },
@ -775,8 +788,8 @@ end
--- Get the previous diagnostic closest to the cursor position. --- Get the previous diagnostic closest to the cursor position.
--- ---
---@param opts table See |vim.diagnostic.goto_next()| ---@param opts nil|table See |vim.diagnostic.goto_next()|
---@return table Previous diagnostic ---@return Diagnostic|nil Previous diagnostic
function M.get_prev(opts) function M.get_prev(opts)
opts = opts or {} opts = opts or {}
@ -789,8 +802,9 @@ end
--- Return the position of the previous diagnostic in the current buffer. --- Return the position of the previous diagnostic in the current buffer.
--- ---
---@param opts table See |vim.diagnostic.goto_next()| ---@param opts table|nil See |vim.diagnostic.goto_next()|
---@return table Previous diagnostic position as a (row, col) tuple. ---@return table|false Previous diagnostic position as a (row, col) tuple or false if there is no
--- prior diagnostic
function M.get_prev_pos(opts) function M.get_prev_pos(opts)
local prev = M.get_prev(opts) local prev = M.get_prev(opts)
if not prev then if not prev then
@ -808,8 +822,8 @@ end
--- Get the next diagnostic closest to the cursor position. --- Get the next diagnostic closest to the cursor position.
--- ---
---@param opts table See |vim.diagnostic.goto_next()| ---@param opts table|nil See |vim.diagnostic.goto_next()|
---@return table Next diagnostic ---@return Diagnostic|nil Next diagnostic
function M.get_next(opts) function M.get_next(opts)
opts = opts or {} opts = opts or {}
@ -822,8 +836,9 @@ end
--- Return the position of the next diagnostic in the current buffer. --- Return the position of the next diagnostic in the current buffer.
--- ---
---@param opts table See |vim.diagnostic.goto_next()| ---@param opts table|nil See |vim.diagnostic.goto_next()|
---@return table Next diagnostic position as a (row, col) tuple. ---@return table|false Next diagnostic position as a (row, col) tuple or false if no next
--- diagnostic.
function M.get_next_pos(opts) function M.get_next_pos(opts)
local next = M.get_next(opts) local next = M.get_next(opts)
if not next then if not next then
@ -1230,7 +1245,7 @@ end
--- Overrides the setting from |vim.diagnostic.config()|. --- Overrides the setting from |vim.diagnostic.config()|.
--- - suffix: Same as {prefix}, but appends the text to the diagnostic instead of --- - suffix: Same as {prefix}, but appends the text to the diagnostic instead of
--- prepending it. Overrides the setting from |vim.diagnostic.config()|. --- prepending it. Overrides the setting from |vim.diagnostic.config()|.
---@return tuple ({float_bufnr}, {win_id}) ---@return number|nil, number|nil: ({float_bufnr}, {win_id})
function M.open_float(opts, ...) function M.open_float(opts, ...)
-- Support old (bufnr, opts) signature -- Support old (bufnr, opts) signature
local bufnr local bufnr
@ -1578,7 +1593,7 @@ end
---@param defaults table|nil Table of default values for any fields not listed in {groups}. ---@param defaults table|nil Table of default values for any fields not listed in {groups}.
--- When omitted, numeric values default to 0 and "severity" defaults to --- When omitted, numeric values default to 0 and "severity" defaults to
--- ERROR. --- ERROR.
---@return diagnostic |diagnostic-structure| or `nil` if {pat} fails to match {str}. ---@return Diagnostic|nil: |diagnostic-structure| or `nil` if {pat} fails to match {str}.
function M.match(str, pat, groups, severity_map, defaults) function M.match(str, pat, groups, severity_map, defaults)
vim.validate({ vim.validate({
str = { str, 's' }, str = { str, 's' },
@ -1625,7 +1640,7 @@ local errlist_type_map = {
--- passed to |setqflist()| or |setloclist()|. --- passed to |setqflist()| or |setloclist()|.
--- ---
---@param diagnostics table List of diagnostics |diagnostic-structure|. ---@param diagnostics table List of diagnostics |diagnostic-structure|.
---@return array of quickfix list items |setqflist-what| ---@return table[] of quickfix list items |setqflist-what|
function M.toqflist(diagnostics) function M.toqflist(diagnostics)
vim.validate({ vim.validate({
diagnostics = { diagnostics = {
@ -1662,7 +1677,7 @@ end
--- ---
---@param list table A list of quickfix items from |getqflist()| or ---@param list table A list of quickfix items from |getqflist()| or
--- |getloclist()|. --- |getloclist()|.
---@return array of diagnostics |diagnostic-structure| ---@return Diagnostic[] array of |diagnostic-structure|
function M.fromqflist(list) function M.fromqflist(list)
vim.validate({ vim.validate({
list = { list = {