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.12k stars 172 forks source link

bug v3: `follow=false` not working as expected #430

Closed zhongjis closed 1 month ago

zhongjis commented 1 month ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.10.0 commit 7acf39ddab8ebdb63ebf78ec980149d20783fd4b

Operating system/version

MacOS 14.4.1

Describe the bug

for some reason, follow=false config is not working for me.

with this setup I was expecting when I run Trouble lsp_references toggle, the result trouble list will not be refreshed when I move my cursor. I was hoping the list do not change until I manually refresh/re-toggle the trouble list. This happens with other lsp related lists.

My idea of using follow=false is from #391

I also tried manually set the follow using Trouble lsp_references toggle follow=false, but the behavior persist

Steps To Reproduce

  1. toggle trouble using Trouble lsp_references toggle or my keymap gr, I used map function on line 46
  2. move cursor to any other place
  3. the trouble list will be refreshed based on the current item cursor is on (PS, when I run with repro.lua, it take a bit time)

Expected Behavior

when cursor moved, the trouble list should not be refreshed.

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", -- IMPORTANT!
  },
  { "folke/neodev.nvim", opts = {} },
  "williamboman/mason.nvim",
  "williamboman/mason-lspconfig.nvim",
  "neovim/nvim-lspconfig",
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
-- lsp
vim.api.nvim_create_autocmd("LspAttach", {
  group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
  callback = function(event)
    -- stylua: ignore start
    local map = function(keys, func, desc)
      vim.keymap.set("n", keys, func, { buffer = bufnr, desc = "LSP: " .. desc })
    end

    -- NOTE: goto actions
    map("gd", "<cmd>Trouble lsp_definitions toggle follow=false<cr>", "[G]oto [D]efinition")
    map("gD", "<cmd>Trouble lsp_declarations toggle<cr>", "[G]oto [D]eclaration")
    map("gr", "<cmd>Trouble lsp_references toggle<cr>", "[G]oto [R]eferences")
    map("gi", "<cmd>Trouble lsp_implementations toggle<cr>", "[G]oto [I]mplementation")
    map("gtd", "<cmd>Trouble lsp_type_definitions toggle<cr>", "[G]oto [T]ype [D]efinition")

    -- NOTE: document actions
    map("<leader>ds", "<cmd>Trouble lsp_document_symbols toggle<cr>", "[D]ocument [S]ymbols")
    map("<leader>dd","<cmd>Trouble diagnostics toggle<cr>","[D]ocument [D]iagnostics List")

    map("[d", "<cmd>Trouble diagnostics prev<cr>", "Go to previous [D]iagnostic message")
    map("]d", "<cmd>Trouble diagnostics next<cr>", "Go to next [D]iagnostic message")

    map("<leader>rn", vim.lsp.buf.rename, "[R]e[n]ame")
    map("<leader>ca", vim.lsp.buf.code_action, "[C]ode [A]ction")
    map("K", vim.lsp.buf.hover, "Hover Documentation")
    -- stylua: ignore end

    local client = vim.lsp.get_client_by_id(event.data.client_id)
    if client and client.server_capabilities.documentHighlightProvider then
      local highlight_augroup =
        vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
      vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
        buffer = event.buf,
        group = highlight_augroup,
        callback = vim.lsp.buf.document_highlight,
      })

      vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
        buffer = event.buf,
        group = highlight_augroup,
        callback = vim.lsp.buf.clear_references,
      })
    end
  end,
})

vim.api.nvim_create_autocmd("LspDetach", {
  group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
  callback = function(event)
    vim.lsp.buf.clear_references()
    vim.api.nvim_clear_autocmds({
      group = "kickstart-lsp-highlight",
      buffer = event.buf,
    })
  end,
})

require("neodev").setup()

require("mason").setup()
require("mason-lspconfig").setup({
  ensure_installed = {
    "lua_ls",
  },

  handlers = {
    function(server_name)
      require("lspconfig")[server_name].setup({})
    end,

    lua_ls = function()
      require("lspconfig").lua_ls.setup({
        settings = {
          Lua = {
            completion = {
              callSnippet = "Replace",
            },
            runtime = {
              version = "LuaJIT",
            },
            diagnostics = {
              globals = {
                "vim",
              },
            },
            telemetry = {
              enable = false,
            },
          },
        },
      })
    end,
  },
})

-- trouble
require("trouble").setup({
  follow = false,
  modes = {
    telescope = {
      sort = { "pos", "filename", "severity", "message" },
    },
    quickfix = {
      sort = { "pos", "filename", "severity", "message" },
    },
    qflist = {
      sort = { "pos", "filename", "severity", "message" },
    },
    loclist = {
      sort = { "pos", "filename", "severity", "message" },
    },
    todo = {
      sort = { "pos", "filename", "severity", "message" },
    },
  },
})

-- disable trouble icon
require("trouble.format").formatters.file_icon = function()
  return ""
end
folke commented 1 month ago

The follow option means that the cursor in the trouble window will follow where you are in the buffer.

The option you want is auto_refresh=false, so :Trouble toggle lsp_references auto_refresh=false.

You can toggle auto_refresh in a trouble buffer with R. And force a refresh with r