neovim/runtime/doc
Justin M. Keyes 2e8103475e
Merge #15585 refactor: move vim.lsp.diagnostic to vim.diagnostic
## Overview

- Move vim.lsp.diagnostic to vim.diagnostic
- Refactor client ids to diagnostic namespaces
- Update tests
- Write/update documentation and function signatures

Currently, non-LSP diagnostics in Neovim must hook into the LSP subsystem. This
is what e.g. null-ls and nvim-lint do. This is necessary because none of the
diagnostic API is exposed separately from the LSP subsystem.

This commit addresses this by generalizing the diagnostic subsystem beyond the
scope of LSP. The `vim.lsp.diagnostic` module is now simply a specific
diagnostic producer and primarily maintains the interface between LSP clients
and the broader diagnostic API.

The current diagnostic API uses "client ids" which only makes sense in the
context of LSP. We replace "client ids" with standard API namespaces generated
from `nvim_create_namespace`.

This PR is *mostly* backward compatible (so long as plugins are only using the
publicly documented API): LSP diagnostics will continue to work as usual, as
will pseudo-LSP clients like null-ls and nvim-lint. However, the latter can now
use the new interface, which looks something like this:

```lua
-- The namespace *must* be given a name. Anonymous namespaces will not work with diagnostics
local ns = vim.api.nvim_create_namespace("foo")

-- Generate diagnostics
local diagnostics = generate_diagnostics()

-- Set diagnostics for the current buffer
vim.diagnostic.set(ns, diagnostics, bufnr)
```

Some public facing API utility methods were removed and internalized directly in `vim.diagnostic`:

* `vim.lsp.util.diagnostics_to_items`

## API Design

`vim.diagnostic` contains most of the same API as `vim.lsp.diagnostic` with
`client_id` simply replaced with `namespace`, with some differences:

* Generally speaking, functions that modify or add diagnostics require a namespace as their first argument, e.g.

  ```lua
  vim.diagnostic.set({namespace}, {bufnr}, {diagnostics}[, {opts}])
  ```

   while functions that read or query diagnostics do not (although in many cases one may be supplied optionally):

   ```lua
   vim.diagnostic.get({bufnr}[, {namespace}])
   ```

* We use our own severity levels to decouple `vim.diagnostic` from LSP. These
  are designed similarly to `vim.log.levels` and currently include:

  ```lua
  vim.diagnostic.severity.ERROR
  vim.diagnostic.severity.WARN
  vim.diagnostic.severity.INFO
  vim.diagnostic.severity.HINT
  ```

  In practice, these match the LSP diagnostic severity levels exactly, but we
  should treat this as an interface and not assume that they are the same. The
  "translation" between the two severity types is handled transparently in
  `vim.lsp.diagnostic`.

* The actual "diagnostic" data structure is: (**EDIT:** Updated 2021-09-09):

  ```lua
  {
    lnum = <number>,
    col = <number>,
    end_lnum = <number>,
    end_col = <number>,
    severity = <vim.diagnostic.severity>,
    message = <string>
  }
  ```

This differs from the LSP definition of a diagnostic, so we transform them in
the handler functions in vim.lsp.diagnostic.

## Configuration

The `vim.lsp.with` paradigm still works for configuring how LSP diagnostics are
displayed, but this is a specific use-case for the `publishDiagnostics` handler.
Configuration with `vim.diagnostic` is instead done with the
`vim.diagnostic.config` function:

```lua
vim.diagnostic.config({
    virtual_text = true,
    signs = false,
    underline = true,
    update_in_insert = true,
    severity_sort = false,
}[, namespace])
```

(or alternatively passed directly to `set()` or `show()`.)

When the `namespace` argument is `nil`, settings are set globally (i.e. for
*all* diagnostic namespaces). This is what user's will typically use for their
local configuration. Diagnostic producers can also set configuration options for
their specific namespace, although this is generally discouraged in order to
respect the user's global settings. All of the values in the table passed to
`vim.diagnostic.config()` are resolved in the same way that they are in
`on_publish_diagnostics`; that is, the value can be a boolean, a table, or
a function:

```lua
vim.diagnostic.config({
    virtual_text = function(namespace, bufnr)
        -- Only enable virtual text in buffer 3
        return bufnr == 3
    end,
})
```

## Misc Notes

* `vim.diagnostic` currently depends on `vim.lsp.util` for floating window
  previews. I think this is okay for now, although ideally we'd want to decouple
  these completely.
2021-09-16 14:23:42 -07:00
..
api.txt docs: extmarks indexing #15311 2021-09-10 19:10:09 -07:00
arabic.txt vim-patch:4d8f476176ea (#15612) 2021-09-10 08:48:27 +02:00
autocmd.txt vim-patch:partial 53f7fccc9413 (#15631) 2021-09-11 16:47:45 +02:00
change.txt vim-patch:4d8f476176ea (#15612) 2021-09-10 08:48:27 +02:00
channel.txt doc: port prompt-buffer section (#14342) 2021-04-14 20:39:37 +02:00
cmdline.txt vim-patch:partial 6aa57295cfbe (#15633) 2021-09-12 11:02:33 +02:00
debug.txt
deprecated.txt refactor: move vim.lsp.diagnostic to vim.diagnostic 2021-09-15 14:09:47 -06:00
develop.txt docs: naming conventions 2021-09-09 06:28:11 -07:00
diagnostic.txt refactor: move vim.lsp.diagnostic to vim.diagnostic 2021-09-15 14:09:47 -06:00
diff.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
digraph.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
editing.txt vim-patch:130cbfc31235 2021-05-02 12:03:34 -04:00
eval.txt doc(eval): include latest relevant Blob changes 2021-09-16 00:14:48 +01:00
filetype.txt vim-patch:8.2.3399: Octave files are not recognized (#15622) 2021-09-10 14:01:13 +02:00
fold.txt vim-patch:partial 53f7fccc9413 (#15631) 2021-09-11 16:47:45 +02:00
ft_ada.txt
ft_ps1.txt vim-patch:4d8f476176ea (#15612) 2021-09-10 08:48:27 +02:00
ft_raku.txt vim-patch:4d8f476176ea (#15612) 2021-09-10 08:48:27 +02:00
ft_rust.txt
ft_sql.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
gui.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
hebrew.txt
help.txt vim-patch:partial 2346a6378483 (#15599) 2021-09-09 18:59:11 +02:00
helphelp.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
if_cscop.txt
if_lua.txt
if_perl.txt vim-patch:8.1.0735: cannot handle binary data 2021-09-15 21:19:22 +01:00
if_pyth.txt vim-patch:664f3cf3f21d 2021-04-27 09:21:31 -04:00
if_ruby.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
indent.txt vim-patch:942db23c9cb7 2021-05-01 23:47:11 -04:00
index.txt vim-patch:8.2.3389: cannot stop insert mode completion without side effects #15538 2021-09-10 17:14:32 -07:00
insert.txt vim-patch:partial 6aa57295cfbe (#15633) 2021-09-12 11:02:33 +02:00
intro.txt docs(terminal): clarify CTRL-\ behavior #15171 2021-09-10 06:24:08 -07:00
job_control.txt fix(doc): various fixes #15604 2021-09-09 00:37:59 -07:00
lsp-extension.txt lsp: vim.lsp.diagnostic (#12655) 2020-11-12 22:21:34 -05:00
lsp.txt refactor: move vim.lsp.diagnostic to vim.diagnostic 2021-09-15 14:09:47 -06:00
lua.txt feat(decode_string): decode binary string with NULs to Blob 2021-09-15 21:19:30 +01:00
Makefile
makehtml.awk vim-patch:388a5d4f20b4 2021-04-28 21:29:57 -04:00
maketags.awk
map.txt doc: Document -complete=lua option (#15102) 2021-08-09 21:21:14 +02:00
mbyte.txt vim-patch:130cbfc31235 2021-05-02 12:03:34 -04:00
message.txt vim-patch:7e6a515ed14e 2021-05-01 22:29:03 -04:00
mlang.txt Merge pull request #14403 from seandewar/vim-8.2.1933 2021-05-09 17:08:21 -04:00
motion.txt vim-patch:4d8f476176ea (#15612) 2021-09-10 08:48:27 +02:00
msgpack_rpc.txt
nvim_terminal_emulator.txt vim-patch:partial 6aa57295cfbe (#15633) 2021-09-12 11:02:33 +02:00
nvim.txt
options.txt vim-patch:6c391a74fe90 (#15654) 2021-09-13 16:33:41 +02:00
pattern.txt vim-patch:partial 2346a6378483 (#15599) 2021-09-09 18:59:11 +02:00
pi_gzip.txt
pi_health.txt
pi_msgpack.txt
pi_netrw.txt vim-patch:89a9c159f23f #15641 2021-09-13 06:05:27 -07:00
pi_paren.txt
pi_spec.txt
pi_tar.txt runtime/tar: 8024f936368336241406137a2fa78ed5ee9000a6 2021-02-17 23:54:40 -05:00
pi_tutor.txt
pi_zip.txt vim-patch:cb80aa2d53e5 2021-05-01 22:29:02 -04:00
print.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
provider.txt docs #15625 2021-09-10 06:59:17 -07:00
quickfix.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
quickref.txt vim-patch:8.1.2019: 'cursorline' always highlights the whole line (#15161) 2021-07-30 21:51:26 -04:00
recover.txt vim-patch:3d1cde8a2f28 2021-04-29 21:08:09 -04:00
remote_plugin.txt
repeat.txt docs: third-party licenses, TEST_COLORS, system() #15665 2021-09-14 10:20:33 -07:00
rileft.txt vim-patch:4d8f476176ea (#15612) 2021-09-10 08:48:27 +02:00
russian.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
scroll.txt
sign.txt vim-patch:partial 6aa57295cfbe (#15633) 2021-09-12 11:02:33 +02:00
spell.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
starting.txt feat(lua)!: register_keystroke_callback => on_key 2021-09-09 06:09:33 -07:00
syntax.txt vim-patch:89a9c159f23f #15641 2021-09-13 06:05:27 -07:00
tabpage.txt vim-patch:ebdf3c964a90 2021-04-27 09:21:35 -04:00
tagsrch.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
term.txt vim-patch:8024f9363683 2021-04-27 09:21:34 -04:00
testing.txt vim-patch:partial 6aa57295cfbe (#15633) 2021-09-12 11:02:33 +02:00
tips.txt fix(doc): don't use method call syntax in examples 2021-06-19 16:37:04 +01:00
treesitter.txt docs #15625 2021-09-10 06:59:17 -07:00
uganda.txt vim-patch:664f3cf3f21d 2021-04-27 09:21:31 -04:00
ui.txt [RDY] Add buffer information to tabline_update (#12481) 2021-06-27 15:30:09 -04:00
undo.txt vim-patch:1b884a005398 2021-05-01 22:29:03 -04:00
usr_01.txt vim-patch:0c0734d527a1 2021-04-27 09:21:30 -04:00
usr_02.txt vim-patch:4c295027a426 2021-05-02 13:00:38 -04:00
usr_03.txt vim-patch:82be4849eed0 2021-05-01 23:19:57 -04:00
usr_04.txt vim-patch:9faec4e3d439 2021-05-02 10:23:35 -04:00
usr_05.txt docs #15625 2021-09-10 06:59:17 -07:00
usr_06.txt
usr_07.txt vim-patch:191acfdecabf 2021-04-27 21:47:42 -04:00
usr_08.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
usr_09.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
usr_10.txt vim-patch:7ceefb35c811 2021-04-28 21:29:56 -04:00
usr_11.txt vim-patch:cb80aa2d53e5 2021-05-01 22:29:02 -04:00
usr_12.txt vim-patch:11e3c5ba8203 2021-05-02 12:53:49 -04:00
usr_20.txt vim-patch:e7b1ea0276cc 2021-04-29 20:42:16 -04:00
usr_21.txt mksession: always unix slashes "/" for filepaths 2020-01-26 17:30:47 -08:00
usr_22.txt vim-patch:d1caa941d876 2021-04-27 22:40:39 -04:00
usr_23.txt vim-patch:4072ba571bab 2021-05-01 22:29:03 -04:00
usr_24.txt vim-patch:5666fcd0bd79 2021-04-27 09:21:33 -04:00
usr_25.txt
usr_26.txt
usr_27.txt vim-patch:2547aa930b59 2021-04-29 09:27:19 -04:00
usr_28.txt
usr_29.txt
usr_30.txt vim-patch:e7b1ea0276cc 2021-04-29 20:42:16 -04:00
usr_31.txt vim-patch:e7b1ea0276cc 2021-04-29 20:42:16 -04:00
usr_32.txt
usr_40.txt vim-patch:e7b1ea0276cc 2021-04-29 20:42:16 -04:00
usr_41.txt vim-patch:8.2.0886: cannot use octal numbers in scriptversion 4 2021-09-11 15:36:03 +01:00
usr_42.txt docs: fix some remanining cases of gender pronoun for "the user" 2021-05-18 22:47:17 +02:00
usr_43.txt
usr_44.txt vim-patch:8024f9363683 2021-04-27 09:21:34 -04:00
usr_45.txt doc: remove mentions of compile-time flags #14935 2021-07-07 18:51:40 -07:00
usr_toc.txt vim-patch:e7b1ea0276cc 2021-04-29 20:42:16 -04:00
various.txt vim-patch:partial 2346a6378483 (#15599) 2021-09-09 18:59:11 +02:00
vi_diff.txt vim-patch:98a29d00a48e 2021-05-01 23:19:57 -04:00
vim_diff.txt docs: third-party licenses, TEST_COLORS, system() #15665 2021-09-14 10:20:33 -07:00
visual.txt vim-patch:d2ea7cf10a4d #15571 2021-09-08 07:24:12 -07:00
windows.txt vim-patch:6c391a74fe90 (#15654) 2021-09-13 16:33:41 +02:00