Nvim :help 页面,生成自源代码使用tree-sitter-vimdoc 解析器。
{namespace} 作为其第一个参数,而供使用者使用的 API 通常不需要命名空间(尽管通常可以可选地提供)。一个好的经验法则是,如果一种方法旨在修改缓冲区的诊断(例如,vim.diagnostic.set()),那么它需要一个命名空间。vim.diagnostic.get(0, { severity = vim.diagnostic.severity.WARN })2. 具有“min”或“max”键(或两者)的表vim.diagnostic.get(0, { severity = { min = vim.diagnostic.severity.WARN } })vim.diagnostic.get(0, { severity = {
vim.diagnostic.severity.WARN,
vim.diagnostic.severity.INFO,
} })function(namespace, bufnr, diagnostics, opts)
function(namespace, bufnr)
vim.diagnostic.handlers 中创建一个新键来添加(参见 diagnostic-handlers-example)。{opts} 表是完整的一组配置选项(也就是说,它不限于处理程序本身的选项)。表中的值已解析(即,如果用户为配置选项指定了一个函数,那么该函数已经执行)。-- It's good practice to namespace custom handlers to avoid collisions
vim.diagnostic.handlers["my/notify"] = {
show = function(namespace, bufnr, diagnostics, opts)
-- In our example, the opts table has a "log_level" option
local level = opts["my/notify"].log_level
local name = vim.diagnostic.get_namespace(namespace).name
local msg = string.format("%d diagnostics in buffer %d from %s",
#diagnostics,
bufnr,
name)
vim.notify(msg, level)
end,
}
-- Users can configure the handler
vim.diagnostic.config({
["my/notify"] = {
log_level = vim.log.levels.INFO
}
})-- Create a custom namespace. This will aggregate signs from all other
-- namespaces and only show the one with the highest severity on a
-- given line
local ns = vim.api.nvim_create_namespace("my_namespace")
-- Get a reference to the original signs handler
local orig_signs_handler = vim.diagnostic.handlers.signs
-- Override the built-in signs handler
vim.diagnostic.handlers.signs = {
show = function(_, bufnr, _, opts)
-- Get all diagnostics from the whole buffer rather than just the
-- diagnostics passed to the handler
local diagnostics = vim.diagnostic.get(bufnr)
-- Find the "worst" diagnostic per line
local max_severity_per_line = {}
for _, d in pairs(diagnostics) do
local m = max_severity_per_line[d.lnum]
if not m or d.severity < m.severity then
max_severity_per_line[d.lnum] = d
end
end
-- Pass the filtered diagnostics (with our custom namespace) to
-- the original handler
local filtered_diagnostics = vim.tbl_values(max_severity_per_line)
orig_signs_handler.show(ns, bufnr, filtered_diagnostics, opts)
end,
hide = function(_, bufnr)
orig_signs_handler.hide(ns, bufnr)
end,
}Diagnostic 开头,后面跟着高亮类型(例如,Sign、Underline 等)和严重性(例如,Error、Warn 等)highlight DiagnosticError guifg="BrightRed"-- Highlight entire line for errors
-- Highlight the line number for warnings
vim.diagnostic.config({
signs = {
text = {
[vim.diagnostic.severity.ERROR] = '',
[vim.diagnostic.severity.WARN] = '',
},
linehl = {
[vim.diagnostic.severity.ERROR] = 'ErrorMsg',
},
numhl = {
[vim.diagnostic.severity.WARN] = 'WarningMsg',
},
},
})当“severity_sort”选项设置时(参见 vim.diagnostic.config()),每个标记的优先级取决于关联诊断的严重性。否则,所有标记都具有相同的优先级(vim.diagnostic.config() 中“signs”表的“priority”选项的值,或者在未设置的情况下为 10)。vim.api.nvim_create_autocmd('DiagnosticChanged', {
callback = function(args)
local diagnostics = args.data.diagnostics
vim.print(diagnostics)
end,
}){bufnr} (integer) 缓冲区编号{lnum} (integer) 诊断的起始行(基于 0){end_lnum} (integer) 诊断的结束行(基于 0){col} (integer) 诊断的起始列(基于 0){end_col} (integer) 诊断的结束列(基于 0){message} (string) 诊断文本{source} (string) 诊断的来源{code} (string|integer) 诊断代码{user_data} (any) 插件可以添加的任意数据{namespace} (integer){namespace} (integer[]|integer) 将诊断限制为一个或多个命名空间。{lnum} (integer) 将诊断限制为跨越指定行号的诊断。{count} (integer) 从 {pos} 开始移动的诊断数量。正整数向前移动 {count} 个诊断,而负整数向后移动 {count} 个诊断。与 {diagnostic} 互斥。{pos} ([integer,integer]) 光标位置,以 (row, col) 元组形式表示。参见 nvim_win_get_cursor()。用于在使用 {count} 时查找最近的诊断。仅在 {count} 不为空时使用。默认值为当前光标位置。{float} (boolean|vim.diagnostic.Opts.Float, 默认值: false) 如果为 true,则在移动后调用 vim.diagnostic.open_float()。 如果为表格,则将表格作为 {opts} 参数传递给 vim.diagnostic.open_float()。 除非被覆盖,否则浮动窗口将显示新光标位置的诊断信息(就像将 "cursor" 传递给 "scope" 选项一样)。{winid} (integer, 默认值: 0) 窗口 ID{name} (string){user_data} (table){disabled} (boolean)false: 禁用此功能true: 启用此功能,使用默认设置。table: 启用此功能并覆盖设置。 使用空表以使用默认值。function: 带有签名 (namespace, bufnr) 的函数,返回上述任何值。{underline} (boolean|vim.diagnostic.Opts.Underline|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.Underline, 默认值: true) 对诊断信息使用下划线。{virtual_text} (boolean|vim.diagnostic.Opts.VirtualText|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.VirtualText, 默认值: true) 对诊断信息使用虚拟文本。 如果为一个命名空间设置了多个诊断信息,则显示每个诊断信息的前缀 + 最后一个诊断信息消息。{signs} (boolean|vim.diagnostic.Opts.Signs|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.Signs, 默认值: true) 对诊断信息使用符号 diagnostic-signs。{float} (boolean|vim.diagnostic.Opts.Float|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.Float) 浮动窗口选项。 请参见 vim.diagnostic.Opts.Float。{severity_sort} (boolean|{reverse?:boolean}, 默认值: false) 按严重性对诊断信息进行排序。 这会影响符号、虚拟文本和高亮显示的显示顺序。 当为 true 时,较高的严重性会先显示(例如,ERROR 会先于 WARN 显示)。 选项{reverse} (boolean) 反转排序顺序{bufnr} (integer, 默认值: 当前缓冲区) 要显示诊断信息的缓冲区号。{namespace} (integer) 将诊断信息限制在给定的命名空间。{scope} ('line'|'buffer'|'cursor'|'c'|'l'|'b', 默认值: line) 显示整个缓冲区 ("buffer") 的诊断信息,当前光标行 (line) 的诊断信息,或当前光标位置 (cursor) 的诊断信息。 也接受简写版本 (c 代表 cursor,l 代表 line,b 代表 buffer)。{pos} (integer|[integer,integer]) 如果 {scope} 为 "line" 或 "cursor",则使用此位置而不是光标位置。 如果为数字,则解释为行号;否则,为 (行号, 列号) 元组。{severity_sort} (boolean|{reverse?:boolean}, 默认值: false) 按严重性对诊断信息进行排序。 覆盖来自 vim.diagnostic.config() 的设置。{severity} (vim.diagnostic.SeverityFilter) 请参见 diagnostic-severity。 覆盖来自 vim.diagnostic.config() 的设置。{header} (string|[string,any]) 用于浮动窗口标题的字符串。 如果为表格,则解释为 [text, hl_group] 元组。 覆盖来自 vim.diagnostic.config() 的设置。{source} (boolean|'if_many') 在消息中包含诊断信息来源。 使用 "if_many" 仅在缓冲区中存在多个诊断信息来源时显示来源。 否则,任何真值都意味着始终显示诊断信息来源。 覆盖来自 vim.diagnostic.config() 的设置。{format} (fun(diagnostic:vim.Diagnostic): string) 一个以诊断信息为输入并返回字符串的函数。 返回值是用于显示诊断信息的文本。 覆盖来自 vim.diagnostic.config() 的设置。{prefix} (string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)) 在浮动窗口中的每个诊断信息前添加前缀function,则 {i} 是正在评估的诊断信息的索引,{total} 是窗口中显示的诊断信息总数。 该函数应返回一个 string,该字符串将被添加到窗口中的每个诊断信息之前,以及一个(可选的)高亮组,该高亮组将用于突出显示前缀。string,则它将被添加到窗口中每个诊断信息之前,没有高亮显示。 覆盖来自 vim.diagnostic.config() 的设置。{suffix} (string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)) 与 {prefix} 相同,但将文本追加到诊断信息后面而不是添加到前面。 覆盖来自 vim.diagnostic.config() 的设置。{focus_id} (string){priority} (integer, 默认值: 10) 用于符号的基本优先级。 当使用 {severity_sort} 时,符号的优先级会根据其严重性进行调整。 否则,所有符号都使用相同的优先级。{text} (table<vim.diagnostic.Severity,string>) 一个将 diagnostic-severity 映射到符号栏中显示的符号文本的表。 默认情况下,对错误、警告、信息和提示分别使用 "E"、"W"、"I" 和 "H"。 例子vim.diagnostic.config({
signs = { text = { [vim.diagnostic.severity.ERROR] = 'E', ... } }
})
{source} (boolean|"if_many") 在虚拟文本中包含诊断信息来源。 使用 'if_many' 仅在缓冲区中存在多个诊断信息来源时显示来源。 否则,任何真值都意味着始终显示诊断信息来源。{spacing} (integer) 在虚拟文本开头插入的空空格数量。{prefix} (string|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string)) 在诊断信息消息前添加前缀。 如果为 function,则 {i} 是正在评估的诊断信息的索引,{total} 是该行的诊断信息总数。 这可以用于渲染诊断信息符号或错误代码。{suffix} (string|(fun(diagnostic:vim.Diagnostic): string)) 在诊断信息消息后添加后缀。 这可以用于渲染 LSP 诊断信息错误代码。{format} (fun(diagnostic:vim.Diagnostic): string) 返回值是用于显示诊断信息的文本。 例子function(diagnostic)
if diagnostic.severity == vim.diagnostic.severity.ERROR then
return string.format("E: %s", diagnostic.message)
end
return diagnostic.message
end
vim.diagnostic.config({ virtual_text = true })vim.diagnostic.set(ns, 0, diagnostics, { virtual_text = false }){namespace} (integer?) 更新给定命名空间的选项。 当省略时,更新全局诊断选项。{bufnr} (integer?) 要获取诊断信息的缓冲区号。 使用 0 表示当前缓冲区,或使用 nil 表示所有缓冲区。is_enabled() 的反值。vim.diagnostic.enable(not vim.diagnostic.is_enabled()){enable} (boolean?) true/nil 表示启用,false 表示禁用{ns_id} (integer) 诊断命名空间,或 nil 表示全部。{bufnr} (integer) 缓冲区号,或 0 表示当前缓冲区,或 nil 表示所有缓冲区。{bufnr} (integer?) 要获取诊断信息的缓冲区号。 使用 0 表示当前缓冲区,或使用 nil 表示所有缓冲区。{namespace} (integer) 诊断命名空间{namespace} (integer?) 诊断命名空间。省略时,隐藏所有命名空间的诊断。{bufnr} (integer?) 缓冲区编号,或当前缓冲区的 0。省略时,隐藏所有缓冲区的诊断。{ns_id} (integer) 诊断命名空间,或 nil 表示全部。{bufnr} (integer) 缓冲区号,或 0 表示当前缓冲区,或 nil 表示所有缓冲区。boolean)WARNING filename:27:3: Variable 'foo' does not exist
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 }){str} (string) 用于解析诊断的字符串。{pat} (string) 具有捕获组的 Lua 模式。{defaults} (table?) 未列在 {groups} 中的任何字段的默认值表。省略时,数字值默认为 0,"severity" 默认为 ERROR。integer?) float_bufnr (integer?) winid{namespace} (integer?) 诊断命名空间。省略时,从所有命名空间中删除诊断。{bufnr} (integer?) 删除给定缓冲区的诊断。省略时,将删除所有缓冲区的诊断。{namespace} (integer) 诊断命名空间{bufnr} (integer) 缓冲区编号{opts} (table?) 具有以下键的配置表{namespace} (integer) 仅添加来自给定命名空间的诊断。{winnr} (integer, 默认值:0) 设置位置列表的窗口编号。{open} (boolean, 默认值:true) 设置后打开位置列表。{title} (string) 位置列表的标题。默认为 "Diagnostics"。{opts} (table?) 具有以下键的配置表{namespace} (integer) 仅添加来自给定命名空间的诊断。{open} (boolean, 默认值:true) 设置后打开快速修复列表。{title} (string) 快速修复列表的标题。默认为 "Diagnostics"。{namespace} (integer?) 诊断命名空间。省略时,显示所有命名空间的诊断。{bufnr} (integer?) 缓冲区编号,或当前缓冲区的 0。省略时,显示所有缓冲区的诊断。{diagnostics} (vim.Diagnostic[]?) 要显示的诊断。省略时,使用给定命名空间和缓冲区的保存诊断。这可用于显示诊断列表而不保存它们,或仅显示诊断的子集。当 {namespace} 或 {bufnr} 为 nil 时,可能无法使用。参见 vim.Diagnostic.