diagnostic.txt 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. *diagnostic.txt* Diagnostics
  2. NVIM REFERENCE MANUAL
  3. Diagnostic framework *vim.diagnostic*
  4. Nvim provides a framework for displaying errors or warnings from external
  5. tools, otherwise known as "diagnostics". These diagnostics can come from a
  6. variety of sources, such as linters or LSP servers. The diagnostic framework
  7. is an extension to existing error handling functionality such as the
  8. |quickfix| list.
  9. Type |gO| to see the table of contents.
  10. ==============================================================================
  11. QUICKSTART *diagnostic-quickstart*
  12. Anything that reports diagnostics is referred to below as a "diagnostic
  13. producer". Diagnostic producers need only follow a few simple steps to
  14. report diagnostics:
  15. 1. Create a namespace |nvim_create_namespace()|. Note that the namespace must
  16. have a name. Anonymous namespaces WILL NOT WORK.
  17. 2. (Optional) Configure options for the diagnostic namespace
  18. |vim.diagnostic.config()|.
  19. 3. Generate diagnostics.
  20. 4. Set the diagnostics for the buffer |vim.diagnostic.set()|.
  21. 5. Repeat from step 3.
  22. Generally speaking, the API is split between functions meant to be used by
  23. diagnostic producers and those meant for diagnostic consumers (i.e. end users
  24. who want to read and view the diagnostics for a buffer). The APIs for
  25. producers require a {namespace} as their first argument, while those for
  26. consumers generally do not require a namespace (though often one may be
  27. optionally supplied). A good rule of thumb is that if a method is meant to
  28. modify the diagnostics for a buffer (e.g. |vim.diagnostic.set()|) then it
  29. requires a namespace.
  30. *vim.diagnostic.severity* *diagnostic-severity*
  31. The "severity" key in a diagnostic is one of the values defined in
  32. `vim.diagnostic.severity`:
  33. vim.diagnostic.severity.ERROR
  34. vim.diagnostic.severity.WARN
  35. vim.diagnostic.severity.INFO
  36. vim.diagnostic.severity.HINT
  37. Functions that take a severity as an optional parameter (e.g.
  38. |vim.diagnostic.get()|) accept one of three forms:
  39. 1. A single |vim.diagnostic.severity| value: >lua
  40. vim.diagnostic.get(0, { severity = vim.diagnostic.severity.WARN })
  41. 2. A table with a "min" or "max" key (or both): >lua
  42. vim.diagnostic.get(0, { severity = { min = vim.diagnostic.severity.WARN } })
  43. <
  44. This form allows users to specify a range of severities.
  45. 3. A list-like table: >lua
  46. vim.diagnostic.get(0, { severity = {
  47. vim.diagnostic.severity.WARN,
  48. vim.diagnostic.severity.INFO,
  49. } })
  50. <
  51. This form allows users to filter for specific severities
  52. ==============================================================================
  53. HANDLERS *diagnostic-handlers*
  54. Diagnostics are shown to the user with |vim.diagnostic.show()|. The display of
  55. diagnostics is managed through handlers. A handler is a table with a "show"
  56. and (optionally) a "hide" function. The "show" function has the signature
  57. >
  58. function(namespace, bufnr, diagnostics, opts)
  59. <
  60. and is responsible for displaying or otherwise handling the given
  61. diagnostics. The "hide" function takes care of "cleaning up" any actions taken
  62. by the "show" function and has the signature
  63. >
  64. function(namespace, bufnr)
  65. <
  66. Handlers can be configured with |vim.diagnostic.config()| and added by
  67. creating a new key in `vim.diagnostic.handlers` (see
  68. |diagnostic-handlers-example|).
  69. The {opts} table passed to a handler is the full set of configuration options
  70. (that is, it is not limited to just the options for the handler itself). The
  71. values in the table are already resolved (i.e. if a user specifies a
  72. function for a config option, the function has already been evaluated).
  73. If a diagnostic handler is configured with a "severity" key then the list of
  74. diagnostics passed to that handler will be filtered using the value of that
  75. key (see example below).
  76. Nvim provides these handlers by default: "virtual_text", "virtual_lines",
  77. "signs", and "underline".
  78. *diagnostic-handlers-example*
  79. The example below creates a new handler that notifies the user of diagnostics
  80. with |vim.notify()|: >lua
  81. -- It's good practice to namespace custom handlers to avoid collisions
  82. vim.diagnostic.handlers["my/notify"] = {
  83. show = function(namespace, bufnr, diagnostics, opts)
  84. -- In our example, the opts table has a "log_level" option
  85. local level = opts["my/notify"].log_level
  86. local name = vim.diagnostic.get_namespace(namespace).name
  87. local msg = string.format("%d diagnostics in buffer %d from %s",
  88. #diagnostics,
  89. bufnr,
  90. name)
  91. vim.notify(msg, level)
  92. end,
  93. }
  94. -- Users can configure the handler
  95. vim.diagnostic.config({
  96. ["my/notify"] = {
  97. log_level = vim.log.levels.INFO
  98. -- This handler will only receive "error" diagnostics.
  99. severity = vim.diagnostic.severity.ERROR,
  100. }
  101. })
  102. <
  103. In this example, there is nothing to do when diagnostics are hidden, so we
  104. omit the "hide" function.
  105. Existing handlers can be overridden. For example, use the following to only
  106. show a sign for the highest severity diagnostic on a given line: >lua
  107. -- Create a custom namespace. This will aggregate signs from all other
  108. -- namespaces and only show the one with the highest severity on a
  109. -- given line
  110. local ns = vim.api.nvim_create_namespace("my_namespace")
  111. -- Get a reference to the original signs handler
  112. local orig_signs_handler = vim.diagnostic.handlers.signs
  113. -- Override the built-in signs handler
  114. vim.diagnostic.handlers.signs = {
  115. show = function(_, bufnr, _, opts)
  116. -- Get all diagnostics from the whole buffer rather than just the
  117. -- diagnostics passed to the handler
  118. local diagnostics = vim.diagnostic.get(bufnr)
  119. -- Find the "worst" diagnostic per line
  120. local max_severity_per_line = {}
  121. for _, d in pairs(diagnostics) do
  122. local m = max_severity_per_line[d.lnum]
  123. if not m or d.severity < m.severity then
  124. max_severity_per_line[d.lnum] = d
  125. end
  126. end
  127. -- Pass the filtered diagnostics (with our custom namespace) to
  128. -- the original handler
  129. local filtered_diagnostics = vim.tbl_values(max_severity_per_line)
  130. orig_signs_handler.show(ns, bufnr, filtered_diagnostics, opts)
  131. end,
  132. hide = function(_, bufnr)
  133. orig_signs_handler.hide(ns, bufnr)
  134. end,
  135. }
  136. <
  137. *diagnostic-toggle-virtual-lines-example*
  138. Diagnostic handlers can also be toggled. For example, you might want to toggle
  139. the `virtual_lines` handler with the following keymap: >lua
  140. vim.keymap.set('n', 'gK', function()
  141. local new_config = not vim.diagnostic.config().virtual_lines
  142. vim.diagnostic.config({ virtual_lines = new_config })
  143. end, { desc = 'Toggle diagnostic virtual_lines' })
  144. <
  145. *diagnostic-loclist-example*
  146. Whenever the |location-list| is opened, the following `show` handler will show
  147. the most recent diagnostics: >lua
  148. vim.diagnostic.handlers.loclist = {
  149. show = function(_, _, _, opts)
  150. -- Generally don't want it to open on every update
  151. opts.loclist.open = opts.loclist.open or false
  152. local winid = vim.api.nvim_get_current_win()
  153. vim.diagnostic.setloclist(opts.loclist)
  154. vim.api.nvim_set_current_win(winid)
  155. end
  156. }
  157. <
  158. The handler accepts the same options as |vim.diagnostic.setloclist()| and can be
  159. configured using |vim.diagnostic.config()|: >lua
  160. -- Open the location list on every diagnostic change (warnings/errors only).
  161. vim.diagnostic.config({
  162. loclist = {
  163. open = true,
  164. severity = { min = vim.diagnostic.severity.WARN },
  165. }
  166. })
  167. <
  168. ==============================================================================
  169. HIGHLIGHTS *diagnostic-highlights*
  170. All highlights defined for diagnostics begin with `Diagnostic` followed by
  171. the type of highlight (e.g., `Sign`, `Underline`, etc.) and the severity (e.g.
  172. `Error`, `Warn`, etc.)
  173. By default, highlights for signs, floating windows, and virtual text are linked to the
  174. corresponding default highlight. Underline highlights are not linked and use their
  175. own default highlight groups.
  176. For example, the default highlighting for |hl-DiagnosticSignError| is linked
  177. to |hl-DiagnosticError|. To change the default (and therefore the linked
  178. highlights), use the |:highlight| command: >vim
  179. highlight DiagnosticError guifg="BrightRed"
  180. <
  181. *hl-DiagnosticError*
  182. DiagnosticError
  183. Used as the base highlight group.
  184. Other Diagnostic highlights link to this by default (except Underline)
  185. *hl-DiagnosticWarn*
  186. DiagnosticWarn
  187. Used as the base highlight group.
  188. Other Diagnostic highlights link to this by default (except Underline)
  189. *hl-DiagnosticInfo*
  190. DiagnosticInfo
  191. Used as the base highlight group.
  192. Other Diagnostic highlights link to this by default (except Underline)
  193. *hl-DiagnosticHint*
  194. DiagnosticHint
  195. Used as the base highlight group.
  196. Other Diagnostic highlights link to this by default (except Underline)
  197. *hl-DiagnosticOk*
  198. DiagnosticOk
  199. Used as the base highlight group.
  200. Other Diagnostic highlights link to this by default (except Underline)
  201. *hl-DiagnosticVirtualTextError*
  202. DiagnosticVirtualTextError
  203. Used for "Error" diagnostic virtual text.
  204. *hl-DiagnosticVirtualTextWarn*
  205. DiagnosticVirtualTextWarn
  206. Used for "Warn" diagnostic virtual text.
  207. *hl-DiagnosticVirtualTextInfo*
  208. DiagnosticVirtualTextInfo
  209. Used for "Info" diagnostic virtual text.
  210. *hl-DiagnosticVirtualTextHint*
  211. DiagnosticVirtualTextHint
  212. Used for "Hint" diagnostic virtual text.
  213. *hl-DiagnosticVirtualTextOk*
  214. DiagnosticVirtualTextOk
  215. Used for "Ok" diagnostic virtual text.
  216. *hl-DiagnosticUnderlineError*
  217. DiagnosticUnderlineError
  218. Used to underline "Error" diagnostics.
  219. *hl-DiagnosticUnderlineWarn*
  220. DiagnosticUnderlineWarn
  221. Used to underline "Warn" diagnostics.
  222. *hl-DiagnosticUnderlineInfo*
  223. DiagnosticUnderlineInfo
  224. Used to underline "Info" diagnostics.
  225. *hl-DiagnosticUnderlineHint*
  226. DiagnosticUnderlineHint
  227. Used to underline "Hint" diagnostics.
  228. *hl-DiagnosticUnderlineOk*
  229. DiagnosticUnderlineOk
  230. Used to underline "Ok" diagnostics.
  231. *hl-DiagnosticFloatingError*
  232. DiagnosticFloatingError
  233. Used to color "Error" diagnostic messages in diagnostics float.
  234. See |vim.diagnostic.open_float()|
  235. *hl-DiagnosticFloatingWarn*
  236. DiagnosticFloatingWarn
  237. Used to color "Warn" diagnostic messages in diagnostics float.
  238. *hl-DiagnosticFloatingInfo*
  239. DiagnosticFloatingInfo
  240. Used to color "Info" diagnostic messages in diagnostics float.
  241. *hl-DiagnosticFloatingHint*
  242. DiagnosticFloatingHint
  243. Used to color "Hint" diagnostic messages in diagnostics float.
  244. *hl-DiagnosticFloatingOk*
  245. DiagnosticFloatingOk
  246. Used to color "Ok" diagnostic messages in diagnostics float.
  247. *hl-DiagnosticSignError*
  248. DiagnosticSignError
  249. Used for "Error" signs in sign column.
  250. *hl-DiagnosticSignWarn*
  251. DiagnosticSignWarn
  252. Used for "Warn" signs in sign column.
  253. *hl-DiagnosticSignInfo*
  254. DiagnosticSignInfo
  255. Used for "Info" signs in sign column.
  256. *hl-DiagnosticSignHint*
  257. DiagnosticSignHint
  258. Used for "Hint" signs in sign column.
  259. *hl-DiagnosticSignOk*
  260. DiagnosticSignOk
  261. Used for "Ok" signs in sign column.
  262. *hl-DiagnosticDeprecated*
  263. DiagnosticDeprecated
  264. Used for deprecated or obsolete code.
  265. *hl-DiagnosticUnnecessary*
  266. DiagnosticUnnecessary
  267. Used for unnecessary or unused code.
  268. ==============================================================================
  269. SIGNS *diagnostic-signs*
  270. Signs are defined for each diagnostic severity. The default text for each sign
  271. is the first letter of the severity name (for example, "E" for ERROR). Signs
  272. can be customized with |vim.diagnostic.config()|. Example: >lua
  273. -- Highlight entire line for errors
  274. -- Highlight the line number for warnings
  275. vim.diagnostic.config({
  276. signs = {
  277. text = {
  278. [vim.diagnostic.severity.ERROR] = '',
  279. [vim.diagnostic.severity.WARN] = '',
  280. },
  281. linehl = {
  282. [vim.diagnostic.severity.ERROR] = 'ErrorMsg',
  283. },
  284. numhl = {
  285. [vim.diagnostic.severity.WARN] = 'WarningMsg',
  286. },
  287. },
  288. })
  289. When the "severity_sort" option is set (see |vim.diagnostic.config()|) the
  290. priority of each sign depends on the severity of the associated diagnostic.
  291. Otherwise, all signs have the same priority (the value of the "priority"
  292. option in the "signs" table of |vim.diagnostic.config()| or 10 if unset).
  293. ==============================================================================
  294. EVENTS *diagnostic-events*
  295. *DiagnosticChanged*
  296. DiagnosticChanged After diagnostics have changed. When used from Lua,
  297. the new diagnostics are passed to the autocmd
  298. callback in the "data" table.
  299. Example: >lua
  300. vim.api.nvim_create_autocmd('DiagnosticChanged', {
  301. callback = function(args)
  302. local diagnostics = args.data.diagnostics
  303. vim.print(diagnostics)
  304. end,
  305. })
  306. <
  307. ==============================================================================
  308. Lua module: vim.diagnostic *diagnostic-api*
  309. *vim.Diagnostic*
  310. *diagnostic-structure*
  311. Diagnostics use the same indexing as the rest of the Nvim API (i.e.
  312. 0-based rows and columns). |api-indexing|
  313. Fields: ~
  314. • {bufnr}? (`integer`) Buffer number
  315. • {lnum} (`integer`) The starting line of the diagnostic
  316. (0-indexed)
  317. • {end_lnum}? (`integer`) The final line of the diagnostic (0-indexed)
  318. • {col} (`integer`) The starting column of the diagnostic
  319. (0-indexed)
  320. • {end_col}? (`integer`) The final column of the diagnostic
  321. (0-indexed)
  322. • {severity}? (`vim.diagnostic.Severity`) The severity of the
  323. diagnostic |vim.diagnostic.severity|
  324. • {message} (`string`) The diagnostic text
  325. • {source}? (`string`) The source of the diagnostic
  326. • {code}? (`string|integer`) The diagnostic code
  327. • {user_data}? (`any`) arbitrary data plugins can add
  328. • {namespace}? (`integer`)
  329. *vim.diagnostic.GetOpts*
  330. A table with the following keys:
  331. Fields: ~
  332. • {namespace}? (`integer[]|integer`) Limit diagnostics to one or more
  333. namespaces.
  334. • {lnum}? (`integer`) Limit diagnostics to those spanning the
  335. specified line number.
  336. • {severity}? (`vim.diagnostic.SeverityFilter`) See
  337. |diagnostic-severity|.
  338. *vim.diagnostic.JumpOpts*
  339. Extends: |vim.diagnostic.GetOpts|
  340. Configuration table with the keys listed below. Some parameters can have
  341. their default values changed with |vim.diagnostic.config()|.
  342. Fields: ~
  343. • {diagnostic}? (`vim.Diagnostic`) The diagnostic to jump to. Mutually
  344. exclusive with {count}, {namespace}, and {severity}.
  345. See |vim.Diagnostic|.
  346. • {count}? (`integer`) The number of diagnostics to move by,
  347. starting from {pos}. A positive integer moves forward
  348. by {count} diagnostics, while a negative integer moves
  349. backward by {count} diagnostics. Mutually exclusive
  350. with {diagnostic}.
  351. • {pos}? (`[integer,integer]`) Cursor position as a `(row, col)`
  352. tuple. See |nvim_win_get_cursor()|. Used to find the
  353. nearest diagnostic when {count} is used. Only used when
  354. {count} is non-nil. Default is the current cursor
  355. position.
  356. • {wrap}? (`boolean`, default: `true`) Whether to loop around
  357. file or not. Similar to 'wrapscan'.
  358. • {severity}? (`vim.diagnostic.SeverityFilter`) See
  359. |diagnostic-severity|.
  360. • {float}? (`boolean|vim.diagnostic.Opts.Float`, default: `false`)
  361. If `true`, call |vim.diagnostic.open_float()| after
  362. moving. If a table, pass the table as the {opts}
  363. parameter to |vim.diagnostic.open_float()|. Unless
  364. overridden, the float will show diagnostics at the new
  365. cursor position (as if "cursor" were passed to the
  366. "scope" option).
  367. • {winid}? (`integer`, default: `0`) Window ID
  368. *vim.diagnostic.NS*
  369. Fields: ~
  370. • {name} (`string`)
  371. • {opts} (`vim.diagnostic.Opts`) See |vim.diagnostic.Opts|.
  372. • {user_data} (`table`)
  373. • {disabled}? (`boolean`)
  374. *vim.diagnostic.Opts*
  375. Many of the configuration options below accept one of the following:
  376. • `false`: Disable this feature
  377. • `true`: Enable this feature, use default settings.
  378. • `table`: Enable this feature with overrides. Use an empty table to use
  379. default values.
  380. • `function`: Function with signature (namespace, bufnr) that returns any
  381. of the above.
  382. Fields: ~
  383. • {underline}? (`boolean|vim.diagnostic.Opts.Underline|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.Underline`, default: `true`)
  384. Use underline for diagnostics.
  385. • {virtual_text}? (`boolean|vim.diagnostic.Opts.VirtualText|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.VirtualText`, default: `false`)
  386. Use virtual text for diagnostics. If multiple
  387. diagnostics are set for a namespace, one prefix
  388. per diagnostic + the last diagnostic message are
  389. shown.
  390. • {virtual_lines}? (`boolean|vim.diagnostic.Opts.VirtualLines|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.VirtualLines`, default: `false`)
  391. Use virtual lines for diagnostics.
  392. • {signs}? (`boolean|vim.diagnostic.Opts.Signs|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.Signs`, default: `true`)
  393. Use signs for diagnostics |diagnostic-signs|.
  394. • {float}? (`boolean|vim.diagnostic.Opts.Float|fun(namespace: integer, bufnr:integer): vim.diagnostic.Opts.Float`)
  395. Options for floating windows. See
  396. |vim.diagnostic.Opts.Float|.
  397. • {update_in_insert}? (`boolean`, default: `false`) Update diagnostics
  398. in Insert mode (if `false`, diagnostics are
  399. updated on |InsertLeave|)
  400. • {severity_sort}? (`boolean|{reverse?:boolean}`, default: `false`)
  401. Sort diagnostics by severity. This affects the
  402. order in which signs, virtual text, and
  403. highlights are displayed. When true, higher
  404. severities are displayed before lower severities
  405. (e.g. ERROR is displayed before WARN). Options:
  406. • {reverse}? (boolean) Reverse sort order
  407. • {jump}? (`vim.diagnostic.Opts.Jump`) Default values for
  408. |vim.diagnostic.jump()|. See
  409. |vim.diagnostic.Opts.Jump|.
  410. *vim.diagnostic.Opts.Float*
  411. Fields: ~
  412. • {bufnr}? (`integer`, default: current buffer) Buffer number
  413. to show diagnostics from.
  414. • {namespace}? (`integer`) Limit diagnostics to the given namespace
  415. • {scope}? (`'line'|'buffer'|'cursor'|'c'|'l'|'b'`, default:
  416. `line`) Show diagnostics from the whole buffer
  417. (`buffer"`, the current cursor line (`line`), or the
  418. current cursor position (`cursor`). Shorthand
  419. versions are also accepted (`c` for `cursor`, `l`
  420. for `line`, `b` for `buffer`).
  421. • {pos}? (`integer|[integer,integer]`) If {scope} is "line"
  422. or "cursor", use this position rather than the
  423. cursor position. If a number, interpreted as a line
  424. number; otherwise, a (row, col) tuple.
  425. • {severity_sort}? (`boolean|{reverse?:boolean}`, default: `false`)
  426. Sort diagnostics by severity. Overrides the setting
  427. from |vim.diagnostic.config()|.
  428. • {severity}? (`vim.diagnostic.SeverityFilter`) See
  429. |diagnostic-severity|. Overrides the setting from
  430. |vim.diagnostic.config()|.
  431. • {header}? (`string|[string,any]`) String to use as the header
  432. for the floating window. If a table, it is
  433. interpreted as a `[text, hl_group]` tuple. Overrides
  434. the setting from |vim.diagnostic.config()|.
  435. • {source}? (`boolean|'if_many'`) Include the diagnostic source
  436. in the message. Use "if_many" to only show sources
  437. if there is more than one source of diagnostics in
  438. the buffer. Otherwise, any truthy value means to
  439. always show the diagnostic source. Overrides the
  440. setting from |vim.diagnostic.config()|.
  441. • {format}? (`fun(diagnostic:vim.Diagnostic): string`) A
  442. function that takes a diagnostic as input and
  443. returns a string. The return value is the text used
  444. to display the diagnostic. Overrides the setting
  445. from |vim.diagnostic.config()|.
  446. • {prefix}? (`string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)`)
  447. Prefix each diagnostic in the floating window:
  448. • If a `function`, {i} is the index of the
  449. diagnostic being evaluated and {total} is the
  450. total number of diagnostics displayed in the
  451. window. The function should return a `string`
  452. which is prepended to each diagnostic in the
  453. window as well as an (optional) highlight group
  454. which will be used to highlight the prefix.
  455. • If a `table`, it is interpreted as a
  456. `[text, hl_group]` tuple as in |nvim_echo()|
  457. • If a `string`, it is prepended to each diagnostic
  458. in the window with no highlight. Overrides the
  459. setting from |vim.diagnostic.config()|.
  460. • {suffix}? (`string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)`)
  461. Same as {prefix}, but appends the text to the
  462. diagnostic instead of prepending it. Overrides the
  463. setting from |vim.diagnostic.config()|.
  464. • {focus_id}? (`string`)
  465. • {border}? (`string`) see |nvim_open_win()|.
  466. *vim.diagnostic.Opts.Jump*
  467. Fields: ~
  468. • {float}? (`boolean|vim.diagnostic.Opts.Float`, default: false)
  469. Default value of the {float} parameter of
  470. |vim.diagnostic.jump()|.
  471. • {wrap}? (`boolean`, default: true) Default value of the {wrap}
  472. parameter of |vim.diagnostic.jump()|.
  473. • {severity}? (`vim.diagnostic.SeverityFilter`) Default value of the
  474. {severity} parameter of |vim.diagnostic.jump()|.
  475. *vim.diagnostic.Opts.Signs*
  476. Fields: ~
  477. • {severity}? (`vim.diagnostic.SeverityFilter`) Only show virtual text
  478. for diagnostics matching the given severity
  479. |diagnostic-severity|
  480. • {priority}? (`integer`, default: `10`) Base priority to use for
  481. signs. When {severity_sort} is used, the priority of a
  482. sign is adjusted based on its severity. Otherwise, all
  483. signs use the same priority.
  484. • {text}? (`table<vim.diagnostic.Severity,string>`) A table mapping
  485. |diagnostic-severity| to the sign text to display in the
  486. sign column. The default is to use `"E"`, `"W"`, `"I"`,
  487. and `"H"` for errors, warnings, information, and hints,
  488. respectively. Example: >lua
  489. vim.diagnostic.config({
  490. signs = { text = { [vim.diagnostic.severity.ERROR] = 'E', ... } }
  491. })
  492. <
  493. • {numhl}? (`table<vim.diagnostic.Severity,string>`) A table mapping
  494. |diagnostic-severity| to the highlight group used for the
  495. line number where the sign is placed.
  496. • {linehl}? (`table<vim.diagnostic.Severity,string>`) A table mapping
  497. |diagnostic-severity| to the highlight group used for the
  498. whole line the sign is placed in.
  499. *vim.diagnostic.Opts.Underline*
  500. Fields: ~
  501. • {severity}? (`vim.diagnostic.SeverityFilter`) Only underline
  502. diagnostics matching the given severity
  503. |diagnostic-severity|.
  504. *vim.diagnostic.Opts.VirtualLines*
  505. Fields: ~
  506. • {current_line}? (`boolean`, default: `false`) Only show diagnostics
  507. for the current line.
  508. • {format}? (`fun(diagnostic:vim.Diagnostic): string`) A function
  509. that takes a diagnostic as input and returns a
  510. string. The return value is the text used to display
  511. the diagnostic.
  512. *vim.diagnostic.Opts.VirtualText*
  513. Fields: ~
  514. • {severity}? (`vim.diagnostic.SeverityFilter`) Only show
  515. virtual text for diagnostics matching the given
  516. severity |diagnostic-severity|
  517. • {source}? (`boolean|"if_many"`) Include the diagnostic
  518. source in virtual text. Use `'if_many'` to only
  519. show sources if there is more than one
  520. diagnostic source in the buffer. Otherwise, any
  521. truthy value means to always show the diagnostic
  522. source.
  523. • {spacing}? (`integer`) Amount of empty spaces inserted at
  524. the beginning of the virtual text.
  525. • {prefix}? (`string|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string)`)
  526. Prepend diagnostic message with prefix. If a
  527. `function`, {i} is the index of the diagnostic
  528. being evaluated, and {total} is the total number
  529. of diagnostics for the line. This can be used to
  530. render diagnostic symbols or error codes.
  531. • {suffix}? (`string|(fun(diagnostic:vim.Diagnostic): string)`)
  532. Append diagnostic message with suffix. This can
  533. be used to render an LSP diagnostic error code.
  534. • {format}? (`fun(diagnostic:vim.Diagnostic): string`) The
  535. return value is the text used to display the
  536. diagnostic. Example: >lua
  537. function(diagnostic)
  538. if diagnostic.severity == vim.diagnostic.severity.ERROR then
  539. return string.format("E: %s", diagnostic.message)
  540. end
  541. return diagnostic.message
  542. end
  543. <
  544. • {hl_mode}? (`'replace'|'combine'|'blend'`) See
  545. |nvim_buf_set_extmark()|.
  546. • {virt_text}? (`[string,any][]`) See |nvim_buf_set_extmark()|.
  547. • {virt_text_pos}? (`'eol'|'eol_right_align'|'inline'|'overlay'|'right_align'`)
  548. See |nvim_buf_set_extmark()|.
  549. • {virt_text_win_col}? (`integer`) See |nvim_buf_set_extmark()|.
  550. • {virt_text_hide}? (`boolean`) See |nvim_buf_set_extmark()|.
  551. config({opts}, {namespace}) *vim.diagnostic.config()*
  552. Configure diagnostic options globally or for a specific diagnostic
  553. namespace.
  554. Configuration can be specified globally, per-namespace, or ephemerally
  555. (i.e. only for a single call to |vim.diagnostic.set()| or
  556. |vim.diagnostic.show()|). Ephemeral configuration has highest priority,
  557. followed by namespace configuration, and finally global configuration.
  558. For example, if a user enables virtual text globally with >lua
  559. vim.diagnostic.config({ virtual_text = true })
  560. <
  561. and a diagnostic producer sets diagnostics with >lua
  562. vim.diagnostic.set(ns, 0, diagnostics, { virtual_text = false })
  563. <
  564. then virtual text will not be enabled for those diagnostics.
  565. Parameters: ~
  566. • {opts} (`vim.diagnostic.Opts?`) When omitted or `nil`, retrieve
  567. the current configuration. Otherwise, a configuration
  568. table (see |vim.diagnostic.Opts|).
  569. • {namespace} (`integer?`) Update the options for the given namespace.
  570. When omitted, update the global diagnostic options.
  571. Return: ~
  572. (`vim.diagnostic.Opts?`) Current diagnostic config if {opts} is
  573. omitted. See |vim.diagnostic.Opts|.
  574. count({bufnr}, {opts}) *vim.diagnostic.count()*
  575. Get current diagnostics count.
  576. Parameters: ~
  577. • {bufnr} (`integer?`) Buffer number to get diagnostics from. Use 0 for
  578. current buffer or nil for all buffers.
  579. • {opts} (`vim.diagnostic.GetOpts?`) See |vim.diagnostic.GetOpts|.
  580. Return: ~
  581. (`table`) Table with actually present severity values as keys (see
  582. |diagnostic-severity|) and integer counts as values.
  583. enable({enable}, {filter}) *vim.diagnostic.enable()*
  584. Enables or disables diagnostics.
  585. To "toggle", pass the inverse of `is_enabled()`: >lua
  586. vim.diagnostic.enable(not vim.diagnostic.is_enabled())
  587. <
  588. Parameters: ~
  589. • {enable} (`boolean?`) true/nil to enable, false to disable
  590. • {filter} (`table?`) Optional filters |kwargs|, or `nil` for all.
  591. • {ns_id}? (`integer`) Diagnostic namespace, or `nil` for
  592. all.
  593. • {bufnr}? (`integer`) Buffer number, or 0 for current
  594. buffer, or `nil` for all buffers.
  595. fromqflist({list}) *vim.diagnostic.fromqflist()*
  596. Convert a list of quickfix items to a list of diagnostics.
  597. Parameters: ~
  598. • {list} (`table[]`) List of quickfix items from |getqflist()| or
  599. |getloclist()|.
  600. Return: ~
  601. (`vim.Diagnostic[]`) See |vim.Diagnostic|.
  602. get({bufnr}, {opts}) *vim.diagnostic.get()*
  603. Get current diagnostics.
  604. Modifying diagnostics in the returned table has no effect. To set
  605. diagnostics in a buffer, use |vim.diagnostic.set()|.
  606. Parameters: ~
  607. • {bufnr} (`integer?`) Buffer number to get diagnostics from. Use 0 for
  608. current buffer or nil for all buffers.
  609. • {opts} (`vim.diagnostic.GetOpts?`) See |vim.diagnostic.GetOpts|.
  610. Return: ~
  611. (`vim.Diagnostic[]`) Fields `bufnr`, `end_lnum`, `end_col`, and
  612. `severity` are guaranteed to be present. See |vim.Diagnostic|.
  613. get_namespace({namespace}) *vim.diagnostic.get_namespace()*
  614. Get namespace metadata.
  615. Parameters: ~
  616. • {namespace} (`integer`) Diagnostic namespace
  617. Return: ~
  618. (`vim.diagnostic.NS`) Namespace metadata. See |vim.diagnostic.NS|.
  619. get_namespaces() *vim.diagnostic.get_namespaces()*
  620. Get current diagnostic namespaces.
  621. Return: ~
  622. (`table<integer,vim.diagnostic.NS>`) List of active diagnostic
  623. namespaces |vim.diagnostic|.
  624. get_next({opts}) *vim.diagnostic.get_next()*
  625. Get the next diagnostic closest to the cursor position.
  626. Parameters: ~
  627. • {opts} (`vim.diagnostic.JumpOpts?`) See |vim.diagnostic.JumpOpts|.
  628. Return: ~
  629. (`vim.Diagnostic?`) Next diagnostic. See |vim.Diagnostic|.
  630. get_prev({opts}) *vim.diagnostic.get_prev()*
  631. Get the previous diagnostic closest to the cursor position.
  632. Parameters: ~
  633. • {opts} (`vim.diagnostic.JumpOpts?`) See |vim.diagnostic.JumpOpts|.
  634. Return: ~
  635. (`vim.Diagnostic?`) Previous diagnostic. See |vim.Diagnostic|.
  636. hide({namespace}, {bufnr}) *vim.diagnostic.hide()*
  637. Hide currently displayed diagnostics.
  638. This only clears the decorations displayed in the buffer. Diagnostics can
  639. be redisplayed with |vim.diagnostic.show()|. To completely remove
  640. diagnostics, use |vim.diagnostic.reset()|.
  641. To hide diagnostics and prevent them from re-displaying, use
  642. |vim.diagnostic.enable()|.
  643. Parameters: ~
  644. • {namespace} (`integer?`) Diagnostic namespace. When omitted, hide
  645. diagnostics from all namespaces.
  646. • {bufnr} (`integer?`) Buffer number, or 0 for current buffer. When
  647. omitted, hide diagnostics in all buffers.
  648. is_enabled({filter}) *vim.diagnostic.is_enabled()*
  649. Check whether diagnostics are enabled.
  650. Attributes: ~
  651. Since: 0.10.0
  652. Parameters: ~
  653. • {filter} (`table?`) Optional filters |kwargs|, or `nil` for all.
  654. • {ns_id}? (`integer`) Diagnostic namespace, or `nil` for
  655. all.
  656. • {bufnr}? (`integer`) Buffer number, or 0 for current
  657. buffer, or `nil` for all buffers.
  658. Return: ~
  659. (`boolean`)
  660. jump({opts}) *vim.diagnostic.jump()*
  661. Move to a diagnostic.
  662. Parameters: ~
  663. • {opts} (`vim.diagnostic.JumpOpts`) See |vim.diagnostic.JumpOpts|.
  664. Return: ~
  665. (`vim.Diagnostic?`) The diagnostic that was moved to. See
  666. |vim.Diagnostic|.
  667. *vim.diagnostic.match()*
  668. match({str}, {pat}, {groups}, {severity_map}, {defaults})
  669. Parse a diagnostic from a string.
  670. For example, consider a line of output from a linter: >
  671. WARNING filename:27:3: Variable 'foo' does not exist
  672. <
  673. This can be parsed into |vim.Diagnostic| structure with: >lua
  674. local s = "WARNING filename:27:3: Variable 'foo' does not exist"
  675. local pattern = "^(%w+) %w+:(%d+):(%d+): (.+)$"
  676. local groups = { "severity", "lnum", "col", "message" }
  677. vim.diagnostic.match(s, pattern, groups, { WARNING = vim.diagnostic.WARN })
  678. <
  679. Parameters: ~
  680. • {str} (`string`) String to parse diagnostics from.
  681. • {pat} (`string`) Lua pattern with capture groups.
  682. • {groups} (`string[]`) List of fields in a |vim.Diagnostic|
  683. structure to associate with captures from {pat}.
  684. • {severity_map} (`table`) A table mapping the severity field from
  685. {groups} with an item from |vim.diagnostic.severity|.
  686. • {defaults} (`table?`) Table of default values for any fields not
  687. listed in {groups}. When omitted, numeric values
  688. default to 0 and "severity" defaults to ERROR.
  689. Return: ~
  690. (`vim.Diagnostic?`) |vim.Diagnostic| structure or `nil` if {pat} fails
  691. to match {str}.
  692. open_float({opts}) *vim.diagnostic.open_float()*
  693. Show diagnostics in a floating window.
  694. Parameters: ~
  695. • {opts} (`vim.diagnostic.Opts.Float?`) See
  696. |vim.diagnostic.Opts.Float|.
  697. Return (multiple): ~
  698. (`integer?`) float_bufnr
  699. (`integer?`) winid
  700. reset({namespace}, {bufnr}) *vim.diagnostic.reset()*
  701. Remove all diagnostics from the given namespace.
  702. Unlike |vim.diagnostic.hide()|, this function removes all saved
  703. diagnostics. They cannot be redisplayed using |vim.diagnostic.show()|. To
  704. simply remove diagnostic decorations in a way that they can be
  705. re-displayed, use |vim.diagnostic.hide()|.
  706. Parameters: ~
  707. • {namespace} (`integer?`) Diagnostic namespace. When omitted, remove
  708. diagnostics from all namespaces.
  709. • {bufnr} (`integer?`) Remove diagnostics for the given buffer.
  710. When omitted, diagnostics are removed for all buffers.
  711. set({namespace}, {bufnr}, {diagnostics}, {opts}) *vim.diagnostic.set()*
  712. Set diagnostics for the given namespace and buffer.
  713. Parameters: ~
  714. • {namespace} (`integer`) The diagnostic namespace
  715. • {bufnr} (`integer`) Buffer number
  716. • {diagnostics} (`vim.Diagnostic[]`) See |vim.Diagnostic|.
  717. • {opts} (`vim.diagnostic.Opts?`) Display options to pass to
  718. |vim.diagnostic.show()|. See |vim.diagnostic.Opts|.
  719. setloclist({opts}) *vim.diagnostic.setloclist()*
  720. Add buffer diagnostics to the location list.
  721. Parameters: ~
  722. • {opts} (`table?`) Configuration table with the following keys:
  723. • {namespace}? (`integer`) Only add diagnostics from the given
  724. namespace.
  725. • {winnr}? (`integer`, default: `0`) Window number to set
  726. location list for.
  727. • {open}? (`boolean`, default: `true`) Open the location list
  728. after setting.
  729. • {title}? (`string`) Title of the location list. Defaults to
  730. "Diagnostics".
  731. • {severity}? (`vim.diagnostic.SeverityFilter`) See
  732. |diagnostic-severity|.
  733. setqflist({opts}) *vim.diagnostic.setqflist()*
  734. Add all diagnostics to the quickfix list.
  735. Parameters: ~
  736. • {opts} (`table?`) Configuration table with the following keys:
  737. • {namespace}? (`integer`) Only add diagnostics from the given
  738. namespace.
  739. • {open}? (`boolean`, default: `true`) Open quickfix list
  740. after setting.
  741. • {title}? (`string`) Title of quickfix list. Defaults to
  742. "Diagnostics". If there's already a quickfix list with this
  743. title, it's updated. If not, a new quickfix list is created.
  744. • {severity}? (`vim.diagnostic.SeverityFilter`) See
  745. |diagnostic-severity|.
  746. *vim.diagnostic.show()*
  747. show({namespace}, {bufnr}, {diagnostics}, {opts})
  748. Display diagnostics for the given namespace and buffer.
  749. Parameters: ~
  750. • {namespace} (`integer?`) Diagnostic namespace. When omitted, show
  751. diagnostics from all namespaces.
  752. • {bufnr} (`integer?`) Buffer number, or 0 for current buffer.
  753. When omitted, show diagnostics in all buffers.
  754. • {diagnostics} (`vim.Diagnostic[]?`) The diagnostics to display. When
  755. omitted, use the saved diagnostics for the given
  756. namespace and buffer. This can be used to display a
  757. list of diagnostics without saving them or to display
  758. only a subset of diagnostics. May not be used when
  759. {namespace} or {bufnr} is nil. See |vim.Diagnostic|.
  760. • {opts} (`vim.diagnostic.Opts?`) Display options. See
  761. |vim.diagnostic.Opts|.
  762. toqflist({diagnostics}) *vim.diagnostic.toqflist()*
  763. Convert a list of diagnostics to a list of quickfix items that can be
  764. passed to |setqflist()| or |setloclist()|.
  765. Parameters: ~
  766. • {diagnostics} (`vim.Diagnostic[]`) See |vim.Diagnostic|.
  767. Return: ~
  768. (`table[]`) Quickfix list items |setqflist-what|
  769. vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl: