-
-
Notifications
You must be signed in to change notification settings - Fork 288
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
improved pathlib #618
base: master
Are you sure you want to change the base?
improved pathlib #618
Conversation
simpler
If you are rewriting the pathlib module from scratch, you should make sure that you are leveraging the latest relevant Neovim API (in particular, Conversely, if the stdlib is missing any functionality (in its scope), consider contributing it there (first). |
Also, a (strong) API design recommendation:
(This implies thinking hard about which arguments should be mandatory and implementing them from the start, while optional arguments can -- and should -- be left for later.) Otherwise you're very quickly painting yourself into a corner and can't add new options without breaking the API. |
47494d8
to
87277a0
Compare
Thanks for the feedback. I don't think there's much I can use from There's probably some learnings I can upstream to
Good idea, I'll take this into consideration. |
Introduce new pathlib -
plenary.path2
, with the focus of greatly improving Windows support.This help close several Windows path issues in telescope nvim-telescope/telescope.nvim#3275
There's a number of breaking changes which is why I opted to create a new pathlib over just fixing
plenary.path
but most of the API remains consistent to help with migration.Breaking Changes
Path
objects are now "immutable" (as much as lua allows)Many of the path operations are done using "metadata" parsed from the args pass on instantiation rather than on the public
filename
field. This frees us from needlessly parsingfilename
on each operation (eg.Path:absolute
). I don't think users mutating path instances is a common thing anyways.Path.new
no longer supported -- felt redundant and didn't feel too valuablePath:new
dropssep
table param support (eg.Path:new { "foo", "bar", sep = "/" }
) -- this doesn't make sense for posix paths since there's only one valid separator anyways, for Windows see next bulletRespects
shellslash
option on Windows -- provides more consistent experience with the rest of vimDrops
__concat
metamethod -- it was untested, and had a todo comment, wasn't sure what the intent of it wasFix
Path:make_relative
With
plenary.path
:This is wrong.
With
plenary.path2
, it will throw an error if the object path (foo/bar_baz
) is not a subpath of the target path (foo/bar
). But to compensate for this, adds a newwalk_up
option (defaultfalse
) to walk up the target path until the two paths reach a common parent. eg.This is probably the most dangerous change since at least in telescope, there are lots of calls to
make_relative
(mostly vianormalize
) to make paths relative to incompatible paths (eg. makeREADME.md
relative to~/projects/telescope.nvim
-- if both were absolute paths,README.md
would be considered as a being a subpath of~/projects/telescope.nvim
, but as a relative path, it's not possible to declare that it is, hencemake_relative
will fail).I'm tempted to add an option to silence errors for these types of conditions.
Path:normalize
:make_relative
behavior so fornormalize
, I'mpcall
ing themake_relative
part and using the original path ifmake_relative
fails -- this largely preserves existing behavior while improving others like https://github.com/nvim-telescope/telescope.nvim/pull/3151/files#r1633252306eg. with
plenary.path
plenary.path2
~
-- this feels like the exact opposite of what "normalize" style functions do. And generally, paths will be relative and so shouldn't apply in most cases. Maybe add an option or a separate function?Path.filename
is semi pre-normalized (no extraneous.
or path separators) -- with how the path metadata is parsed, this comes for freePath:rename
new_name
is now a positional arg (eg.path:rename('foobar')
)Path
instance rather than mutating itselfPath:copy
destination
is now a positional arg (eg.path:copy('foobar', { exists_ok = true })
){success: boolean, err: string?}
rather than justboolean
Error handling is generally a lot more explicit/loud, most noticeably with libuv interactions, raising returned errors rather than silently swallowing them or doing generic assertions (eg,
local fd = assert(uv.fs_open(...))
)Renamed
Path:iter
toPath:iter_lines
for more clarityPath:find_upwards
returnsnil
if file not found rather than an empty string