-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fs): add vim.fs module #18583
feat(fs): add vim.fs module #18583
Conversation
893377a
to
9cf27ea
Compare
related: #16800 |
c3cdd94
to
31e54d8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
I wonder if we should start indicating in the docs if a function can be used in a luv callback? Currently one would have to try or read the code.
05ffd2b
to
e1a0f7e
Compare
if we start with plain strings, that should keep the door open on a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this functions. Have a few comments/questions, hope is not too spammy :)
I also see no mention of supporting ~
for user home dir, which is fine but perhaps it's worth mentioning.
What about windows paths is the separator normalized (I imagine not), maybe the examples should have a disclaimer.
runtime/doc/lua.txt
Outdated
|
||
Return: ~ | ||
Iterator over files and directories in {path}. Each | ||
iteration yields two values: name and type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which are the types dir, directory, file
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"file"
and "directory"
, I added this to the docs
{file} (string) File or directory | ||
|
||
Return: ~ | ||
(string) Parent directory of {file} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relative or absolute path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either, it depends on what it is passed. vim.fs.dirname("foo/bar")
returns "foo"
, vim.fs.dirname("/foo/bar")
returns "/foo"
.
(string) Basename of {file} | ||
|
||
dir({path}) *vim.path.dir()* | ||
Return an iterator over the files and directories located in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The iterator provides the file or directory basename or full path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each file/directory name returned from the iterator is relative to the provided {path}
argument. I added a note about this to the docs.
I've added For now this just uses |
This is currently blocked by a memory leak in ASAN output
We are using |
8926661
to
8db917b
Compare
More detailed backtrace:
I can replicate this locally but only when running |
I was finally able to reproduce this fully and I think I've identified the root cause. I've opened an issue with luv. |
Memory leak is gone, but
|
runtime/lua/vim/fs.lua
Outdated
function M.normalize(path) | ||
vim.validate({ path = { path, 's' } }) | ||
return (path:gsub('\\', '/'):gsub('^~/', vim.env.HOME .. '/'):gsub('%$([%w_]+)', vim.env)) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will work on Windows, unless you use PowerShell, or at least not reliably.
The environment variable expansion will give backslashes that you'd also need to sanitize before concatenating
$HOME
C:\Users\USERNAME
vim.fs.parents() is a Lua iterator that returns the next parent directory of the given file or directory on each iteration.
This function is modeled after the path.dir() function from Penlight and the luafilesystem module.
This is a pure Lua implementation of the Vim findfile() and finddir() functions without the special syntax.
11d6a05
to
99b3985
Compare
@gpanders: you seemed to have forgotten to address comments made by me and @mfussenegger about documenting that some functions implemented here cannot be run in Fortunately, since #18306 has been merged, we should be able to mark |
This PR adds a minimal
vim.fs
module. For now, only these six functions are included:vim.fs.parents
: an iterator that iterates over all of the parent/ancestor directories of the given file or directoryUsage example:
vim.fs.dirname
: Return the directory name of the given file or directoryUsage example:
vim.fs.basename
: Return the base name of the given file or directoryUsage example:
vim.fs.dir
: Iterator over all files and directories within a given directory. Adapted from Penlight'spath.dir()
(which is itself adapted fromluafilesystem.dir()
).Usage example:
vim.fs.find
: Find files or directories upwards or downwards from a given pathUsage example 1:
Usage example 2:
vim.fs.normalize
: Normalize a path to a standard format, expanding tildes and environment variablesUsage example: