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",則使用此位置而非游標位置。如果為數字,則會被解讀為行號;否則,則為 (行, 列) 的元組。{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
會被加在視窗中的每個診斷訊息前面,以及一個 (可選的) 高亮顯示群組,該群組將用於高亮顯示前綴。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。{diagnostics}
) vim.diagnostic.toqflist()