folke / trouble.nvim

🚦 A pretty diagnostics, references, telescope results, quickfix and location list to help you solve all the trouble your code is causing.
Apache License 2.0
5.11k stars 172 forks source link

bug: Trouble shows no diagnostics results for scratch buffers #514

Open kmoschcau opened 1 week ago

kmoschcau commented 1 week ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.11.0-dev

Operating system/version

Ubuntu 22.04, WSL2, Windows 11

Describe the bug

When I toggle trouble for a scratch buffer, I get the following notification:

No results for **diagnostics**
Buffer: <name of the buffer in the previously focused window>

Steps To Reproduce

  1. create a scratch buffer
  2. add diagnostics to the buffer
  3. open buffer in new window and focus it
  4. toggle trouble with filter of buf = 0

Expected Behavior

Trouble should list the diagnostics for the scratch buffer, just like Telescope is able to.

Repro

-- Start nvim once with `nvim -u repro.lua`, to install plugins.
-- Then start nvim with `nvim -u repro.lua repro.lua`, then press `<Space>t` and
-- notice the notification.

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",
    "folke/trouble.nvim",
    -- add any other plugins here
}
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "Foo", "bar" })
vim.diagnostic.set(vim.api.nvim_create_namespace("Namespace"), bufnr, {
    {
        lnum = 0,
        col = 0,
        end_col = 4,
        severity = vim.diagnostic.severity.E,
        message = "Message",
        source = "Source",
        code = "Code",
    },
})

vim.api.nvim_open_win(bufnr, true, { vertical = true })

vim.keymap.set("n", "<Space>t", function()
    require("trouble").toggle({ mode = "diagnostics", filter = { buf = 0 } })
end)
b0ae989c commented 1 week ago

This is not a bug. bufnr is not 0 for the scratch buffer. Should close.

If you need to show diagnostic for the buffer where the cursor is, try

require("trouble").toggle({ mode = "diagnostics", filter = { buf = vim.fn.bufnr() } })
kmoschcau commented 1 week ago

How is it not 0, if everywhere in nvim's API, a buffer ID of 0 means the current buffer? It works that way with telescope (require("telescope.builtin").diagnostics { bufnr = 0 }). Trouble's documentation also mentions that buf = 0 filters for the current buffer. So either this is a bug or the documentation needs to be updated.

b0ae989c commented 1 week ago

My mistake. buf = 0 is a special case. See https://github.com/folke/trouble.nvim/blob/2d8610a07033f2b367e71230ab128f8b89c2639d/lua/trouble/filter.lua#L34-L39