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.15k stars 173 forks source link

bug v3: Order of entries changes, when jumping to next entry in a trouble telescope or quickfix list #399

Closed tummetott closed 1 month ago

tummetott commented 3 months ago

Did you check docs and existing issues?

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

  1. mkdir test && cd test
  2. touch a b c d
  3. nvim -u path/to/repro.lua
  4. :Telescope find_files
  5. Press <C-q> to open all files in a trouble window
  6. :lua require'trouble'.next({jump=true})
  7. :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
bellini666 commented 3 months ago

Started to migrate to v3, experiencing the same issue

folke commented 3 months ago

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

folke commented 3 months ago

And quickfix is the same as qflist. I don't fully rememeber, but I probably kept that for backward compatibility.

tummetott commented 3 months ago

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

folke commented 1 month ago

Changed all the views to remove the buf = 0, so should no longer happen

tummetott commented 1 month ago

What was the default sort behavior in the old version of trouble? My workflow is the following:

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?

folke commented 1 month ago

@tummetott seems I forgot to remove that buf = 0 filter for telescope. Fixed now.

tummetott commented 1 month ago

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

folke commented 1 month ago

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 = {}.