Skip to content
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

Reverse range in character class #122

Closed
staticssleever668 opened this issue Apr 3, 2021 · 4 comments
Closed

Reverse range in character class #122

staticssleever668 opened this issue Apr 3, 2021 · 4 comments

Comments

@staticssleever668
Copy link

Description

Sometimes when I open files with some characters in a directory's name in file path, vim-rooter prints an error.
The problem seems to be related to these symbols: -, [, ] and character u.

Steps to reproduce

folder_name="[u-a]" && mkdir -p "$folder_name" && touch "$folder_name/dummy" && vim "$folder_name/dummy"
Other folder names that trigger this error: [Au-S], [Audio-Stuff].

Full error message:

Error detected while processing function <SNR>35_rooter[5]..<SNR>35_root[11]..<SNR>35_match[8]..<SNR>35_has:
line    1:
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
E944: Reverse range in character class
Press ENTER or type command to continue
Error detected while processing function <SNR>35_rooter[5]..<SNR>35_root[11]..<SNR>35_match[8]..<SNR>35_has:
line    1:
E944: Reverse range in character class
Press ENTER or type command to continue
Error detected while processing function <SNR>35_rooter[5]..<SNR>35_root[11]..<SNR>35_match[8]..<SNR>35_has:
line    1:
E944: Reverse range in character class
Press ENTER or type command to continue
Error detected while processing function <SNR>35_rooter[5]..<SNR>35_root[11]..<SNR>35_match[8]..<SNR>35_has:
line    1:
E944: Reverse range in character class
Press ENTER or type command to continue
Error detected while processing function <SNR>35_rooter[5]..<SNR>35_root[11]..<SNR>35_match[8]..<SNR>35_has:
line    1:
E944: Reverse range in character class
Press ENTER or type command to continue
Error detected while processing function <SNR>35_rooter[5]..<SNR>35_root[11]..<SNR>35_match[8]..<SNR>35_has:
line    1:
E944: Reverse range in character class
Press ENTER or type command to continue```
@airblade
Copy link
Owner

airblade commented Apr 7, 2021

Thank you for the clear bug report along with steps to reproduce :)

The "Reverse range in character class" error means that Vim is treating the folder name as a regexp; and [u-a] looks like a character class except backwards – i.e. if it's a regexp it should be [a-u].

:echo 's' =~ '[r-t]'     " => 1
:echo 's' =~ '[t-r]'     " => E944: Reverse range in character class

Of course the code should be treating the name as a literal, not a regexp. I'll fix it up.

@airblade
Copy link
Owner

airblade commented Apr 7, 2021

The underlying problem is that when Rooter searches for "has" patterns, which are the most common, it uses globpath(). This concatenates the directory it's currently testing and the rooter pattern (e.g. .git), then expands the result with glob(). glob() expands the wildcards ?, *, **, and [abc] (character classes) – see :help wildcards.

So the problem is that even if your rooter patterns don't contain any wildcards, if you use "has" patterns and any of your files' ancestor directories contain wildcards, you'll get this undesired behaviour.

I had hoped that only the rooter pattern would be treated as a glob, not the directory too.

@airblade
Copy link
Owner

airblade commented Apr 7, 2021

Does this patch solve the problem for you?

diff --git i/plugin/rooter.vim w/plugin/rooter.vim
index 4573cbd..55b6b18 100644
--- i/plugin/rooter.vim
+++ w/plugin/rooter.vim
@@ -166,7 +166,7 @@ endfunction
 " dir        - full path to a directory
 " identifier - a file name or a directory name; may be a glob
 function! s:has(dir, identifier)
-  return !empty(globpath(a:dir, a:identifier, 1))
+  return !empty(globpath(escape(a:dir, '?*[]'), a:identifier, 1))
 endfunction

@airblade
Copy link
Owner

airblade commented Apr 8, 2021

Thanks for confirming.

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

No branches or pull requests

2 participants