Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
kmoschcau opened this issue Jun 19, 2024 · 6 comments
Open
3 tasks done

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

kmoschcau opened this issue Jun 19, 2024 · 6 comments
Labels
bug Something isn't working stale

Comments

@kmoschcau
Copy link

Did you check docs and existing issues?

  • I have read all the trouble.nvim docs
  • I have searched the existing issues of trouble.nvim
  • I have searched the existing issues of plugins related to this issue

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)
@kmoschcau kmoschcau added the bug Something isn't working label Jun 19, 2024
@b0ae989c
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

My mistake. buf = 0 is a special case. See

buf = function(item, buf, ctx)
if buf == 0 then
return ctx.main and ctx.main.filename == item.filename or false
end
return item.buf == buf
end,

Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Jul 20, 2024
@kmoschcau
Copy link
Author

@b0ae989c can you please remove the stale label? I don't think I'm allowed to do that.

@github-actions github-actions bot removed the stale label Jul 26, 2024
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

2 participants