nvim_runtime_lua

vim.diagnostic

TODO(lewis6991): deprecate some top level functions in favour of the submodule version e.g. vim.diagnostic.get_namespace() -> vim.diagnostic.namespace.get()

Methods25

function M.config(opts: vim.diagnostic.Opts | nil, namespace: nil | integer) -> vim.diagnostic.Opts | nil

Configure diagnostic options globally or for a specific diagnostic namespace.

Configuration can be specified globally, per-namespace, or ephemerally (i.e. only for a single call to |vim.diagnostic.set()| or |vim.diagnostic.show()|). Ephemeral configuration has highest priority, followed by namespace configuration, and finally global configuration.

For example, if a user enables virtual text globally with

vim.diagnostic.config({ virtual_text = true })

and a diagnostic producer sets diagnostics with

vim.diagnostic.set(ns, 0, diagnostics, { virtual_text = false })

then virtual text will not be enabled for those diagnostics.

Parameters
optsvim.diagnostic.Opts | nil

When omitted or nil, retrieve the current configuration. Otherwise, a configuration table (see |vim.diagnostic.Opts|).

namespacenil | integer

Update the options for the given namespace. When omitted, update the global diagnostic options.

Returns
vim.diagnostic.Opts | nil

: Current diagnostic config if {opts} is omitted.

function M.set(namespace: integer, buf: integer, diagnostics: vim.Diagnostic.Set[], opts: vim.diagnostic.Opts | nil) -> nil

Set diagnostics for the given namespace and buffer.

Parameters
namespaceinteger

The diagnostic namespace

bufinteger

Buffer number

diagnosticsvim.Diagnostic.Set[]
optsvim.diagnostic.Opts | nil

Display options to pass to |vim.diagnostic.show()|

Returns
nil
function M.get_namespace(namespace: integer) -> vim.diagnostic.NS

Get namespace metadata.

Parameters
namespaceinteger

Diagnostic namespace

Returns
vim.diagnostic.NS

: Namespace metadata

function M.get_namespaces() -> {integer, vim.diagnostic.NS}

Get current diagnostic namespaces.

Returns
{integer, vim.diagnostic.NS}

: List of active diagnostic namespaces |vim.diagnostic|.

function M.get(buf: nil | integer, opts: vim.diagnostic.GetOpts | nil) -> vim.Diagnostic[]

Get current diagnostics.

Modifying diagnostics in the returned table has no effect. To set diagnostics in a buffer, use |vim.diagnostic.set()|.

Parameters
bufnil | integer

Buffer number to get diagnostics from. Use 0 for current buffer or nil for all buffers.

Returns
vim.Diagnostic[]

: Fields buf, end_lnum, end_col, and severity are guaranteed to be present.

function M.count(buf: nil | integer, opts: vim.diagnostic.GetOpts | nil) -> {integer, integer}

Get current diagnostics count.

Parameters
bufnil | integer

Buffer number to get diagnostics from. Use 0 for current buffer or nil for all buffers.

Returns
{integer, integer}

: Table with actually present severity values as keys (see |diagnostic-severity|) and integer counts as values.

function M.get_prev(opts: vim.diagnostic.JumpOpts | nil) -> vim.Diagnostic | nil

Get the previous diagnostic closest to the cursor position.

Parameters
Returns
vim.Diagnostic | nil

: Previous diagnostic

function M.get_prev_pos(opts: vim.diagnostic.JumpOpts | nil) -> table | false
Deprecated

Return the position of the previous diagnostic in the current buffer.

Parameters
Returns
table | false

: Previous diagnostic position as a (row, col) tuple or false if there is no prior diagnostic.

Deprecated
function M.goto_prev(opts: vim.diagnostic.JumpOpts | nil) -> nil
Deprecated

Move to the previous diagnostic in the current buffer.

Deprecated
function M.get_next(opts: vim.diagnostic.JumpOpts | nil) -> vim.Diagnostic | nil

Get the next diagnostic closest to the cursor position.

Parameters
Returns
vim.Diagnostic | nil

: Next diagnostic

function M.get_next_pos(opts: vim.diagnostic.JumpOpts | nil) -> table | false
Deprecated

Return the position of the next diagnostic in the current buffer.

Parameters
Returns
table | false

: Next diagnostic position as a (row, col) tuple or false if no next diagnostic.

Deprecated
function M.jump(opts: vim.diagnostic.JumpOpts) -> vim.Diagnostic | nil

Move to a diagnostic.

Parameters
Returns
vim.Diagnostic | nil

The diagnostic that was moved to.

function M.goto_next(opts: vim.diagnostic.JumpOpts | nil) -> nil
Deprecated

Move to the next diagnostic.

Deprecated
function M.hide(namespace: nil | integer, buf: nil | integer) -> nil

Hide currently displayed diagnostics.

This only clears the decorations displayed in the buffer. Diagnostics can be redisplayed with |vim.diagnostic.show()|. To completely remove diagnostics, use |vim.diagnostic.reset()|.

To hide diagnostics and prevent them from re-displaying, use |vim.diagnostic.enable()|.

Parameters
namespacenil | integer

Diagnostic namespace. When omitted, hide diagnostics from all namespaces.

bufnil | integer

Buffer number, or 0 for current buffer. When omitted, hide diagnostics in all buffers.

Returns
nil
function M.is_enabled(filter: vim.diagnostic.Filter | nil) -> boolean

Check whether diagnostics are enabled.

@since 12

function M.show(namespace: nil | integer, buf: nil | integer, diagnostics: vim.Diagnostic[] | nil, opts: vim.diagnostic.Opts | nil) -> nil

Display diagnostics for the given namespace and buffer.

Parameters
namespacenil | integer

Diagnostic namespace. When omitted, show diagnostics from all namespaces.

bufnil | integer

Buffer number, or 0 for current buffer. When omitted, show diagnostics in all buffers.

diagnosticsvim.Diagnostic[] | nil

The diagnostics to display. When omitted, use the saved diagnostics for the given namespace and buffer. This can be used to display a list of diagnostics without saving them or to display only a subset of diagnostics. May not be used when {namespace} or {buf} is nil.

optsvim.diagnostic.Opts | nil

Display options.

Returns
nil
function M.open_float(opts: vim.diagnostic.Opts.Float | nil, ...) -> (float_bufnr nil | integer, winid nil | integer)

Show diagnostics in a floating window.

function M.reset(namespace: nil | integer, buf: nil | integer) -> nil

Remove all diagnostics from the given namespace.

Unlike |vim.diagnostic.hide()|, this function removes all saved diagnostics. They cannot be redisplayed using |vim.diagnostic.show()|. To simply remove diagnostic decorations in a way that they can be re-displayed, use |vim.diagnostic.hide()|.

Parameters
namespacenil | integer

Diagnostic namespace. When omitted, remove diagnostics from all namespaces.

bufnil | integer

Remove diagnostics for the given buffer. When omitted, diagnostics are removed for all buffers.

Returns
nil
function M.setqflist(opts: vim.diagnostic.setqflist.Opts | nil) -> nil

Add all diagnostics to the quickfix list.

function M.setloclist(opts: vim.diagnostic.setloclist.Opts | nil) -> nil

Add buffer diagnostics to the location list.

function M.enable(enable: nil | boolean, filter: vim.diagnostic.Filter | nil) -> nil

Enables or disables diagnostics.

To "toggle", pass the inverse of is_enabled():

vim.diagnostic.enable(not vim.diagnostic.is_enabled())
Parameters
enablenil | boolean

true/nil to enable, false to disable

filtervim.diagnostic.Filter | nil
Returns
nil
function M.match(str: string, pat: string, groups: string[], severity_map: table, defaults: nil | table) -> vim.Diagnostic | nil

Parse a diagnostic from a string.

For example, consider a line of output from a linter:

WARNING filename:27:3: Variable 'foo' does not exist

This can be parsed into |vim.Diagnostic| structure with:

local s = "WARNING filename:27:3: Variable 'foo' does not exist"
local pattern = "^(%w+) %w+:(%d+):(%d+): (.+)$"
local groups = { "severity", "lnum", "col", "message" }
vim.diagnostic.match(s, pattern, groups, { WARNING = vim.diagnostic.WARN })
Parameters
strstring

String to parse diagnostics from.

patstring

Lua pattern with capture groups.

groupsstring[]

List of fields in a |vim.Diagnostic| structure to associate with captures from {pat}.

severity_maptable

A table mapping the severity field from {groups} with an item from |vim.diagnostic.severity|.

defaultsnil | table

Table of default values for any fields not listed in {groups}. When omitted, numeric values default to 0 and "severity" defaults to ERROR.

Returns
vim.Diagnostic | nil

: |vim.Diagnostic| structure or nil if {pat} fails to match {str}.

function M.toqflist(diagnostics: vim.Diagnostic[]) -> table[]

Convert a list of diagnostics to a list of quickfix items that can be passed to |setqflist()| or |setloclist()|.

Parameters
diagnosticsvim.Diagnostic[]
Returns
table[]

: Quickfix list items |setqflist-what|

function M.fromqflist(list: vim.quickfix.entry[], opts: vim.diagnostic.fromqflist.Opts | nil) -> vim.Diagnostic[]

Convert a list of quickfix items to a list of diagnostics.

Parameters
listvim.quickfix.entry[]

List of quickfix items from |getqflist()| or |getloclist()|.

Returns
function M.status(buf: nil | integer) -> string

Returns formatted string with diagnostics for the current buffer. The severities with 0 diagnostics are left out. Example E:2 W:3 I:4 H:5

To customise appearance, see |vim.diagnostic.Opts.Status|.

Parameters
bufnil | integer

Buffer number to get diagnostics from. Defaults to 0 for the current buffer

Returns
string

Fields9

M._config : vim.diagnostic._config
M._display : vim.diagnostic._display
M._float : vim.diagnostic._float
M._jump : vim.diagnostic._jump
M._severity : vim.diagnostic._severity
M._store : vim.diagnostic._store
M._handlers : vim.diagnostic._handlers

@nodoc

M.handlers : {string, vim.diagnostic.Handler}

@nodoc