Skip to content

Commit 3398369

Browse files
Thom1729FichteFoll
andauthored
Fix glob bug. (#157)
* Fix glob bug. * Update st3/sublime_lib/_util/glob.py Co-authored-by: FichteFoll <[email protected]> Co-authored-by: FichteFoll <[email protected]>
1 parent 7c8ff38 commit 3398369

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

st3/sublime_lib/_util/glob.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,45 @@
77

88

99
GLOB_RE = re.compile(r"""(?x)(
10-
(?:^|/) \*\* (?:$|/)
11-
| (?<!\*)\*(?!\*)
10+
\*
1211
| \?
1312
| \[ .*? \]
1413
)""")
1514

1615

1716
@lru_cache()
1817
def get_glob_matcher(pattern: str) -> Callable[[str], bool]:
19-
s = r'\A'
2018
if pattern.startswith('/'):
2119
pattern = pattern[1:]
2220
else:
2321
pattern = '**/' + pattern
2422

25-
for part in GLOB_RE.split(pattern):
26-
if part == '':
23+
expr_string = r'\A'
24+
for component in pattern.split('/'):
25+
if component == '':
2726
pass
28-
elif part.strip('/') == '**':
29-
s += r'(?:^|/)(?:(?:.*)(?:$|/))?'
30-
elif part == '*':
31-
s += r'(?:[^/]*)'
32-
elif part == '?':
33-
s += r'(?:[^/])'
34-
elif part[0] == '[':
35-
s += part
36-
elif '**' in part:
27+
elif component == '*':
28+
# Component must not be empty.
29+
expr_string += r'(?:[^/])+' + '/'
30+
elif component == '**':
31+
expr_string += r'(?:.*(?:\Z|/))?'
32+
elif '**' in component:
3733
raise ValueError("Invalid pattern: '**' can only be an entire path component")
3834
else:
39-
s += re.escape(part)
40-
41-
expr = re.compile(s + r'\Z')
42-
return lambda s: (expr.search(s) is not None)
35+
for part in GLOB_RE.split(component):
36+
if part == '':
37+
pass
38+
elif part == '*':
39+
expr_string += r'(?:[^/])*'
40+
elif part == '?':
41+
expr_string += r'(?:[^/])'
42+
elif part[0] == '[':
43+
expr_string += part
44+
else:
45+
expr_string += re.escape(part)
46+
expr_string += '/'
47+
48+
expr_string = expr_string.rstrip('/') + r'\Z'
49+
expr = re.compile(expr_string)
50+
51+
return lambda path: (expr.search(path) is not None)

tests/test_glob.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def test_recursive(self):
115115
self._test_matches(
116116
'Foo/**',
117117
[
118+
'Packages/Foo/',
118119
'Packages/Foo/bar',
119120
'Packages/Foo/bar/baz',
120121
],
@@ -154,6 +155,18 @@ def test_recursive(self):
154155
[]
155156
)
156157

158+
self._test_matches(
159+
'Foo/**/*',
160+
[
161+
'Foo/bar',
162+
'Foo/bar/baz',
163+
],
164+
[
165+
'Foo',
166+
'Foo/',
167+
]
168+
)
169+
157170
def test_placeholder(self):
158171
self._test_matches(
159172
'/Packages/Foo/ba?',

0 commit comments

Comments
 (0)