Skip to content

fix(#2004): remove unintentional captures in path_to_matching_str #2005

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

Merged
merged 2 commits into from
Feb 19, 2023

Conversation

kting28
Copy link
Contributor

@kting28 kting28 commented Feb 17, 2023

fixes #2004

The captures creates a limit where input path cannot have more than 32 special charactors ( . , _ or -)

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the case for underscores and dashes, however there is still potential for other magic regex characters to cause an issue.

A more complete solution might be for path_relative to use string.find (plain) and string.sub to extract the relative path without needing to resort to regexes.

Are you keen to make that change?

@@ -12,7 +12,7 @@ M.is_wsl = vim.fn.has "wsl" == 1
M.is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1

function M.path_to_matching_str(path)
return path:gsub("(%-)", "(%%-)"):gsub("(%.)", "(%%.)"):gsub("(%_)", "(%%_)")
return path:gsub("(%-)", "%%-"):gsub("(%.)", "%%."):gsub("(%_)", "%%_")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That definitely fixes it. Those captures weren't necessary.

@kting28
Copy link
Contributor Author

kting28 commented Feb 18, 2023

to use string.find (plain)

Hmm.. looks like string.find expects a pattern so there's no avoiding of striping the escape characters. From the manual:

The basic use of string.find is to search for a pattern inside a given string,

Is there a version that's straigthforward string match? I tried path:find(... and it requires converting into to match string first...

@alex-courtis
Copy link
Member

Is there a version that's straigthforward string match? I tried path:find(... and it requires converting into to match string first...

You can use the third argument plain to skip regex parsing. See https://github.com/nvim-tree/nvim-tree.lua/blob/master/lua/nvim-tree/git/init.lua#L43

@kting28 kting28 force-pushed the kting28-patch-1 branch 2 times, most recently from 463f0e6 to 9222462 Compare February 19, 2023 04:44
@kting28
Copy link
Contributor Author

kting28 commented Feb 19, 2023

Updated PR. I kept the original path_to_matching_str (with captures removed) for reference

@alex-courtis
Copy link
Member

Updated PR. I kept the original path_to_matching_str (with captures removed) for reference

No need; please remove.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for going the extra mile on this one.

There are a lot of windows standard paths that contain magic characters and this fix will take care of all of them.

local p, _ = path:gsub("^" .. M.path_to_matching_str(M.path_add_trailing(relative_to)), "")
local _, r = path:find(M.path_add_trailing(relative_to), 1, true)
local p = path
if r and r < p:len() then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good... except when there is a trailing slash.

See test case attached.
2005-test.lua.gz

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test cases. Just pushed the fix. previously version if relative == path, I kept the path but to match the original gsub version, the code now just returns ""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, thanks for the excellent plugin!

…tring to avoid using regex.

- This removed the original gsub with unintentional captures in path_to_matching_str
- The original regex based code captures create a limit where input path cannot
  have more than 32 special charactors ( `.`  , `_` or `-`)
Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution

@alex-courtis alex-courtis merged commit 66c15af into nvim-tree:master Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error opening directory with deep directory stucture and many underscores in the path name
2 participants