调用Telescope自定义Lua搜索函数时触发E5108错误
问题解决方法
核心错误:函数嵌套定义导致外部无法访问
你的lua/config/telescope-custom.lua代码中,M.mandenkan和M.find_notes两个函数被错误嵌套在了M.dotfiles函数内部,这会导致当你require该模块后,返回的M对象中仅包含dotfiles方法,无法直接访问另外两个函数。调用不存在的方法时就会触发"attempt to index a boolean value"错误。
修正后的代码
将所有函数定义在M对象的顶层,不要嵌套:
local M = {} function M.dotfiles() require("telescope.builtin").find_files({ prompt_title = "< config >", prompt_prefix = "🔍", cwd = vim.fn.expand("$HOME/.config/"), -- 用vim.fn.expand解析路径变量 }) end function M.mandenkan() require("telescope.builtin").live_grep({ cwd = vim.fn.expand("$HOME/Documents/Jula/search"), prompt_title = "< mandenkan >", prompt_prefix = "🔍", layout_strategy = "horizontal", }) end function M.find_notes() require("telescope.builtin").find_files({ prompt_title = " Find Notes", path_display = { "smart" }, cwd = vim.fn.expand("~/notes/"), layout_strategy = "horizontal", layout_config = { preview_width = 0.65, width = 0.75 }, }) end return M
检查require路径是否正确
如果你的文件路径是lua/config/telescope-custom.lua,正确的调用命令应该是:
:lua require('config.telescope-custom').find_notes()
你之前使用的require'c._telescope-custom'路径不正确,这也可能导致模块加载失败,触发错误。
额外优化:路径变量解析
Telescope的cwd参数需要传入绝对路径,直接写$HOME或~可能无法被正确解析,所以用vim.fn.expand()来展开路径变量,确保目录路径有效。
内容的提问来源于stack exchange,提问作者Boyd
相关产品推荐
相关产品推荐

