Neovim Lua isdirectory vim 函数

Neovim Lua isdirectory vim function

我希望为我在 git 目录中和不在目录中时设置键盘映射。也许现在我正在使用 Lua,也许我不能这样做。任何帮助将不胜感激。

if vim.fn.isdirectory('.git') then
  map('n', '<leader>t', '<cmd>lua require(\'telescope.builtin\').git_files({hidden = true})<CR>', options)
else
  map('n', '<leader>e', '<cmd>lua require(\'telescope.builtin\').find_files({hidden = true})<CR>', options)
end

似乎 if 总是命中而 else 永远不会。

:h是目录

isdirectory({directory})

isdirectory() The result is a Number, which is non-zero when a directory with the name {directory} exists. If {directory} doesn't exist, or isn't a directory, the result is FALSE. {directory} is any expression, which is used as a String.

:h 假

For boolean operators Numbers are used. Zero is FALSE, non-zero is TRUE. You can also use |v:false| and |v:true|. When TRUE is returned from a function it is the Number one, FALSE is the number zero.

确保 FALSE 实际上是 false。它可能是 0,这将是 Lua 中的真实值。 在 Lua 中,除 false 或 nil 之外的任何值都是 true。

所以理想情况下检查 if vim.fn.isdirectory('.git') ~= 0 then