bug v3: Order of entries changes, when jumping to next entry in a trouble telescope or quickfix list
Did you check docs and existing issues?
- [X] I have read all the trouble.nvim docs
- [X] I have searched the existing issues of trouble.nvim
- [X] I have searched the existing issues of plugins related to this issue
Neovim version (nvim -v)
NVIM v0.9.5 Build type: Release LuaJIT 2.1.1703358377
Operating system/version
MacOs 14.2.1
Describe the bug
Lets say I have the 4 files a b c, d opened in a trouble window (e.g. from Telescope from a Quickfix list, it does not matter).
When I execute :lua require'trouble'.next({jump=true}) a few times, the order of the trouble list changes.
Steps To Reproduce
mkdir test && cd testtouch a b c dnvim -u path/to/repro.lua:Telescope find_files- Press
<C-q>to open all files in a trouble window :lua require'trouble'.next({jump=true}):lua require'trouble'.next({jump=true})--> The order is already messed up
this also happens with quickfix lists.
BTW: what is the difference between :Trouble qflist and :Trouble quickfix ?? i can open two trouble windows, showing the same content?
Expected Behavior
Order should not change.
Repro
-- 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",
branch = "dev",
lazy = false,
opts = {
follow = false,
restore = true,
auto_preview = false,
auto_refresh = false,
},
},
{
'nvim-telescope/telescope.nvim',
dependencies = { 'nvim-lua/plenary.nvim' },
config = function()
local open_with_trouble = require("trouble.sources.telescope").open
require('telescope').setup {
defaults = {
mappings = {
i = {
['<C-q>'] = open_with_trouble,
}
}
}
}
end,
}
-- add any other plugins here
}
require("lazy").setup(plugins, {
root = root .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")
-- add anything else here
Started to migrate to v3, experiencing the same issue
That's actually because of the default sort for some sources.
This is the default for diagnostics for example:
sort = { { buf = 0 }, "severity", "filename", "pos", "message" },
The first item of the sorter will sort items from your current buffer to the top.
You can change it with:
{
modes = {
diagnostics = {
sort = { "severity", "filename", "pos", "message" },
}
}
}
I'll keep this issue open for now, since the default doesn't work well indeed for going to next/previous items
And quickfix is the same as qflist.
I don't fully rememeber, but I probably kept that for backward compatibility.
would it make sense then to make the following defaults?
modes = {
telescope = {
sort = { "pos", "filename", "severity", "message" },
},
quickfix = {
sort = { "pos", "filename", "severity", "message" },
},
loclist = {
sort = { "pos", "filename", "severity", "message" },
},
todo = {
sort = { "pos", "filename", "severity", "message"}
},
}
I guess it makes sense for diagnostics that they sort every time you move or jump inside the list, since fixing lsp errors is a continuous task. But for quickfix, loclist, todo comments (which have imho no severity?) and telescope results its quite confusing when the order changes every time you move
Changed all the views to remove the buf = 0, so should no longer happen
What was the default sort behavior in the old version of trouble? My workflow is the following:
- I open telescope with
grep_string - I search my query
- I press a keymap to send all results in a trouble window (
require("trouble.sources.telescope").open) - I have a keymap for
require("trouble").next({skip_groups = true, jump = true})to jump to next entries in the list - Sometimes, I want more screen space, close trouble with
:Trouble telescope toggle - When i bring up the trouble list again with the same toggle keymap, the order is not preserved.
in the old version of trouble, the order was preserverd. it behaved just like the native vim qflist. how must i configure the sorter, so the order is not changed, even after closing and opening trouble again?
@tummetott seems I forgot to remove that buf = 0 filter for telescope. Fixed now.
Or maybe in other words. How can i preserve the order that telescope shows me after typing a query? When sending items of a telescope search to a native qflist, the order is the one from telescope. in trouble, i can sort by filetype (which is the behavior of the old trouble) or by pos. but pos does not keep the order of telescope
You can try opts.modes.telescope.sort = {}. Not 100%, but this probably does use the order of items as you saw it in Telescope.
You'll probaly also have to set opts.modes.telescope.groups = {}.