You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

folke/todo-comments.nvim问题:TodoTelescope等命令全局搜索而非仅项目

解决todo-comments.nvim全局搜索而非当前项目的问题

使用folke/todo-comments.nvim时,高亮功能正常,但通过<leader>st调用:TodoTelescope、:TodoTrouble等命令时,会搜索系统所有目录下的文件,而非仅当前项目树内的文件。相关配置如下:

{
  "folke/todo-comments.nvim",
  cmd = { "TodoTrouble", "TodoTelescope" },
  event = "LazyFile",
  dependencies = { "nvim-lua/plenary.nvim" },
  config= function()
    local todo_comments = require("todo-comments")

    todo_comments.setup()
    end,
  keys = {
    { "]t", function() require("todo-comments").jump_next() end, desc = "Next Todo Comment" },
    { "[t", function() require("todo-comments").jump_prev() end, desc = "Previous Todo Comment" },
    { "<leader>xt", "<cmd>TodoTrouble<cr>", desc = "Todo (Trouble)" },
    { "<leader>xT", "<cmd>TodoTrouble keywords=TODO,FIX,FIXME<cr>", desc = "Todo/Fix/Fixme (Trouble)" },
    { "<leader>st", "<cmd>TodoTelescope<cr>", desc = "Todo" },
    { "<leader>sT", "<cmd>TodoTelescope keywords=TODO,FIX,FIXME<cr>", desc = "Todo/Fix/Fixme" },
  },
},

环境说明:LazyVim发行版,Ubuntu 22.04.4 LTS,已安装ripgrep、fzf及所有项目依赖。


解决方法

1. 确保项目根目录被识别

todo-comments默认会通过版本控制目录(如.git、.hg)、配置文件(如.nvimrc、.editorconfig)识别项目根目录。如果你的项目没有这类文件,可手动创建空的.gitignore或.nvimrc文件,插件会自动限制搜索范围到当前项目根。

2. 修改插件配置,手动限制搜索范围

在setup函数中添加search配置项,自定义ripgrep的参数或指定搜索路径:

config= function()
  local todo_comments = require("todo-comments")

  todo_comments.setup({
    search = {
      command = "rg",
      args = {
        "--color=never",
        "--no-heading",
        "--with-filename",
        "--line-number",
        "--column",
        "--hidden",
        "--glob=!.git/", -- 排除git目录
        "--glob=!node_modules/", -- 按需添加其他排除目录
      },
      -- 或者直接指定搜索路径为当前工作目录
      -- path = vim.fn.getcwd()
    }
  })
end,

3. 调用命令时手动指定工作目录

绑定快捷键时,给命令加上cwd参数,强制限定搜索范围为当前工作目录:

keys = {
  -- 其他快捷键保持不变
  { "]t", function() require("todo-comments").jump_next() end, desc = "Next Todo Comment" },
  { "[t", function() require("todo-comments").jump_prev() end, desc = "Previous Todo Comment" },
  { "<leader>xt", "<cmd>TodoTrouble cwd=" .. vim.fn.getcwd() .. "<cr>", desc = "Todo (Trouble)" },
  { "<leader>xT", "<cmd>TodoTrouble cwd=" .. vim.fn.getcwd() .. " keywords=TODO,FIX,FIXME<cr>", desc = "Todo/Fix/Fixme (Trouble)" },
  { "<leader>st", "<cmd>TodoTelescope cwd=" .. vim.fn.getcwd() .. "<cr>", desc = "Todo" },
  { "<leader>sT", "<cmd>TodoTelescope cwd=" .. vim.fn.getcwd() .. " keywords=TODO,FIX,FIXME<cr>", desc = "Todo/Fix/Fixme" },
},

内容的提问来源于stack exchange,提问作者Pitore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 02:22:47