Home Explore Blog CI



neovim

3rd chunk of `runtime/doc/diagnostic.txt`
6623ab90b827490308415132db01c959dc493e6416c7d4050000000100000fa0
     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-toggle-virtual-lines-example*
Diagnostic handlers can also be toggled. For example, you might want to toggle
the `virtual_lines` handler with the following keymap: >lua

    vim.keymap.set('n', 'gK', function()
      local new_config = not vim.diagnostic.config().virtual_lines
      vim.diagnostic.config({ virtual_lines = new_config })
    end, { desc = 'Toggle diagnostic virtual_lines' })
<

                                            *diagnostic-on-jump-example*
You can use the `on_jump` option from |vim.diagnostic.jump()| to show the
diagnostic that was jumped to using a specific handler. For example, the
following uses the `virtual_lines` handler when jumping to a diagnostic: >lua

  local virt_lines_ns = vim.api.nvim_create_namespace 'on_diagnostic_jump'

  --- @param diagnostic? vim.Diagnostic
  --- @param bufnr integer
  local function on_jump(diagnostic, bufnr)
      if not diagnostic then return end

      vim.diagnostic.show(
          virt_lines_ns,
          bufnr,
          { diagnostic },
          { virtual_lines = { current_line = true }, virtual_text = false }
      )
  end

  vim.diagnostic.config({ jump = { on_jump = on_jump } })

                                          *diagnostic-loclist-example*
Whenever the |location-list| is opened, the following `show` handler will show
the most recent diagnostics: >lua

  vim.diagnostic.handlers.loclist = {
    show = function(_, _, _, opts)
      -- Generally don't want it to open on every update
      opts.loclist.open = opts.loclist.open or false
      local winid = vim.api.nvim_get_current_win()
      vim.diagnostic.setloclist(opts.loclist)
      vim.api.nvim_set_current_win(winid)
    end
  }
<

The handler accepts the same options as |vim.diagnostic.setloclist()| and can be
configured using |vim.diagnostic.config()|: >lua

  -- Open the location list on every diagnostic change (warnings/errors only).
  vim.diagnostic.config({
    loclist = {
      open = true,
      severity = { min = vim.diagnostic.severity.WARN },
    }
  })
<

==============================================================================
HIGHLIGHTS                                              *diagnostic-highlights*

All highlights defined for diagnostics begin with `Diagnostic` followed by
the type of highlight (e.g., `Sign`, `Underline`, etc.) and the severity (e.g.
`Error`, `Warn`, etc.)

By default, highlights for signs, floating windows, and virtual text are linked to the
corresponding default highlight. Underline highlights are not linked and use their
own default highlight groups.

For example, the default highlighting for |hl-DiagnosticSignError| is linked
to |hl-DiagnosticError|. To change the default (and therefore the linked
highlights), use the |:highlight| command: >vim

    highlight DiagnosticError guifg="BrightRed"
<
                                                        *hl-DiagnosticError*
DiagnosticError
    Used as the base highlight group.
    Other Diagnostic highlights link to this by default (except Underline)

                                                        *hl-DiagnosticWarn*
DiagnosticWarn
    Used as the base highlight group.
    Other Diagnostic highlights link to this by default (except Underline)

 

Title: Diagnostic Handlers: Toggling, Jumping, and Location Lists; Highlights
Summary
This section provides examples for toggling diagnostic handlers, specifically focusing on toggling `virtual_lines` with a keymap. It also demonstrates how to use the `on_jump` option with `vim.diagnostic.jump()` to show the diagnostic that was jumped to using a specific handler, such as `virtual_lines`. Furthermore, it shows how to configure a handler to update the location list with diagnostics whenever the location list is opened. Lastly, the section details the highlight groups used for diagnostics, starting with `Diagnostic` and including the type of highlight and severity, and explains how to customize these highlights using the `:highlight` command.