英文:
How to get all files and directories within the current working directory of Neovim using Lua?
问题
Here's the translation of the provided text:
给定在 Neovim 中打开的编辑文件,如何返回当前工作目录 (CWD) 中的所有文件和目录?
预期用例是设置一个键映射,该键映射具有回调到一个函数的功能,该函数检查当前文件或目录是否存在于CWD中。我正试图编写这样一个函数。在下面的伪代码中,我称之为 existsInCWD
。
情景
例如,mappings.lua
文件中的键映射
vim.keymap.set({'n'}, '<A-f>', '',
{
desc = "Check if 'figures' directory exists",
callback = function()
local figuresDirExists = existsInCWD("figures")
if figuresDirExists then
print("Figure directory exists.")
else
print("Figure directory does not exist.")
end
end
}
)
负责调用 existsInCWD
函数。然后,该函数获取当前工作目录中的所有文件和目录,并检查传入的名称是否与任何文件匹配。
existsInCWD = function(nameToCheck)
-- 获取当前工作目录
local cwDir = vim.fn.getcwd()
-- 获取CWD中的所有文件和目录(伪代码)
local cwdContent = get_all_files_and_dirs_in_cwd()
-- 检查指定的文件或目录是否存在
local fullNameToCheck = cwDir .. "/" .. nameToCheck
print("Checking for: \"" .. fullNameToCheck .. "\"")
for _, cwdItem in pairs(cwdContent) do
if cwdItem == fullNameToCheck then
return true
end
end
return false
end
在上面的伪代码中,我正在寻找一个与 Neovim 内置功能兼容的 Lua/Neovim 解决方案,用于 get_all_files_and_dirs_in_cwd()
伪函数,该函数使用了 Neovim 的内置功能。
英文:
Given a file that is open for editing within Neovim, how can one return all files and directories within the current working directory (CWD) of that open file?
The intended use case is to have a key-mapping set that has a callback to a function that checks whether a current file or directory exists within the CWD. I am trying to write such a function. In the pseudo-code shown further below, I have called it existsInCWD
.
Scenario
For example, the key-map in the mappings.lua
file
vim.keymap.set({'n'}, '<A-f>', '',
{
desc = "Check if 'figures' directory exists",
callback = function()
local figuresDirExists = existsInCWD("figures")
if figuresDirExists then
print("Figure directory exists.")
else
print("Figure directory does not exist.")
end
end
}
)
is responsible for calling the existsInCWD
function. This function then gets all the files and directories in the current working directory and checks whether the passed in name matches any of the files.
existsInCWD = function(nameToCheck)
-- Get current working directory
local cwDir = vim.fn.getcwd()
-- Get all files and directories in CWD (pseudo-code)
local cwdContent = get_all_files_and_dirs_in_cwd()
-- Check if specified file or directory exists
local fullNameToCheck = cwDir .. "/" .. nameToCheck
print("Checking for: \"" .. fullNameToCheck .. "\"")
for _, cwdItem in pairs(cwdContent) do
if cwdItem == fullNameToCheck then
return true
end
end
return false
end
In the above pseudo-code, I am looking for a Lua/Neovim-compatible solution to the get_all_files_and_dirs_in_cwd()
pseudo-function that makes use of the built-in functionality of Neovim.
答案1
得分: 3
以下是您要翻译的内容:
要存储当前工作目录(CWD)中的所有文件和目录,可以使用以下代码:
local cwDir = vim.fn.getcwd()
local cwdContent = vim.split(vim.fn.glob(cwDir .. "/*"), '\n', {trimempty=true})
-- 注意1:如果cwDir以尾部的"/"结尾,因为CWD是通过其他方式(例如Lua字符串匹配/替换)获取的,
-- 那么在上面的代码行中删除在"*"之前的"/",以避免出现"//"的问题
-- 注意2:使用{trimempty=true}确保vim.split()会删除在分隔符之前的空白空间,而不是将其视为保留的实体。
然后,变量cwdContent
可以迭代并根据需要使用。以下是完整的示例函数:
existsInCWD = function(nameToCheck)
local cwDir = vim.fn.getcwd()
-- 获取CWD中的所有文件和目录
local cwdContent = vim.split(vim.fn.glob(cwDir .. "/*"), '\n', {trimempty=true})
-- 检查指定的文件或目录是否存在
local fullNameToCheck = cwDir .. "/" .. nameToCheck
print("Checking for: \"" .. fullNameToCheck .. "\"")
for _, cwdItem in pairs(cwdContent) do
if cwdItem == fullNameToCheck then
return true
end
end
return false
end
英文:
To store all files and directories in the CWD, one can use:
local cwDir = vim.fn.getcwd()
local cwdContent = vim.split(vim.fn.glob(cwDir .. "/*"), '\n', {trimempty=true})
-- Note 1: if cwDir contains a trailing "/" because the CWD was acquired
-- by means other than getcwd() (e.g. Lua string matching/substitution),
-- then remove the "/" preceding the "*" in the above line to avoid "//" issues
-- Note 2: use {trimempty=true} to ensure that vim.split() drops
-- empty space preceding a separator instead of treating it as en entity to keep.
The variable cwdContent
can then be iterated over and used as desired. As a complete example function:
existsInCWD = function(nameToCheck)
local cwDir = vim.fn.getcwd()
-- Get all files and directories in CWD
local cwdContent = vim.split(vim.fn.glob(cwDir .. "/*"), '\n', {trimempty=true})
-- Check if specified file or directory exists
local fullNameToCheck = cwDir .. "/" .. nameToCheck
print("Checking for: \"" .. fullNameToCheck .. "\"")
for _, cwdItem in pairs(cwdContent) do
if cwdItem == fullNameToCheck then
return true
end
end
return false
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论