Skip to content

Commit 08ccf88

Browse files
committed
Exclude file/folder_exclude_patterns
1 parent 0c233cf commit 08ccf88

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ such patterns:
301301
{ "dired_hidden_files_patterns": [".*", "__pycache__", "*.pyc"] }
302302
```
303303

304+
It also shows all files and directories otherwise excluded by your `file_exclude_patterns` and `folder_exclude_patterns`.
305+
306+
To hide excluded files and folders, add the following to your settings:
307+
``` json
308+
{ "dired_show_excluded_files": false }
309+
```
310+
304311
### VCS integration
305312
In case `git status`(or `hg status`) returns a colorable output in current directory, the modified
306313
and untracked files will be designated by orange and green icons respectively.

common.py

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ def sort_nicely(names):
3636
Source: http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
3737
"""
3838
convert = lambda text: int(text) if text.isdigit() else text.lower()
39-
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
39+
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key['file'] if key['file'] else key)]
4040
names.sort(key=alphanum_key)
4141

42-
4342
def print(*args, **kwargs):
4443
""" Redefine print() function; the reason is the inconsistent treatment of
4544
unicode literals among Python versions used in ST2.
@@ -400,54 +399,76 @@ def prepare_filelist(self, names, path, goto, indent):
400399
items += files
401400
return items
402401

403-
def is_hidden(self, filename, path, goto=''):
402+
def is_hidden(self, filename, path, goto='', get_is_dir=False, dirs_only=False):
404403
if not (path or goto): # special case for ThisPC
405404
return False
406-
tests = self.view.settings().get('dired_hidden_files_patterns', ['.*'])
407-
if isinstance(tests, str):
408-
tests = [tests]
409-
if any(fnmatch.fnmatch(filename, pattern) for pattern in tests):
410-
return True
411-
if sublime.platform() != 'windows':
412-
return False
413-
# check for attribute on windows:
414-
try:
415-
attrs = ctypes.windll.kernel32.GetFileAttributesW(join(path, goto, filename))
416-
assert attrs != -1
417-
result = bool(attrs & 2)
418-
except (AttributeError, AssertionError):
419-
result = False
420-
return result
421-
422-
def try_listing_directory(self, path):
405+
show_hidden = self.show_hidden
406+
show_excluded = self.view.settings().get('dired_show_excluded_files', True)
407+
is_hidden = False
408+
fullpath = join(path, goto, filename)
409+
is_dir = isdir(fullpath)
410+
result = lambda: is_hidden if not get_is_dir else [is_hidden, is_dir]
411+
if dirs_only and not is_dir:
412+
return result()
413+
if not is_hidden and not show_excluded:
414+
if is_dir:
415+
tests = self.view.settings().get('folder_exclude_patterns', [])
416+
if any(fnmatch.fnmatch(filename, p) for p in tests):
417+
is_hidden = True
418+
else:
419+
tests = self.view.settings().get('file_exclude_patterns', [])
420+
if any(fnmatch.fnmatch(filename, p) for p in tests):
421+
is_hidden = True
422+
if not is_hidden and not show_hidden:
423+
tests = self.view.settings().get('dired_hidden_files_patterns', ['.*'])
424+
if isinstance(tests, str):
425+
tests = [tests]
426+
if any(fnmatch.fnmatch(filename, p) for p in tests):
427+
is_hidden = True
428+
if not is_hidden and NT:
429+
# check for attribute on windows:
430+
try:
431+
attrs = ctypes.windll.kernel32.GetFileAttributesW(fullpath)
432+
assert attrs != -1
433+
if bool(attrs & 2):
434+
is_hidden = True
435+
except:
436+
pass
437+
return result()
438+
439+
def listdir(self, path):
440+
return [{"file": file, "isdir": isdir(join(path, file))} for file in os.listdir(path)]
441+
442+
def listdir_only_dirs(self, path):
443+
return [item for item in self.listdir(path) if item["isdir"] == True]
444+
445+
def try_listing_directory(self, path, dirs_only=False):
423446
'''Return tuple of two element
424447
items sorted list of filenames in path, or empty list
425448
error exception message, or empty string
426449
'''
427-
items, error = [], ''
450+
items = []
451+
error = None
428452
try:
429-
if not self.show_hidden:
430-
items = [name for name in os.listdir(path) if not self.is_hidden(name, path)]
453+
if dirs_only:
454+
items = self.listdir_only_dirs(path)
431455
else:
432-
items = os.listdir(path)
456+
items = self.listdir(path)
433457
except OSError as e:
434458
error = str(e)
435459
if NT:
436460
error = error.split(':')[0].replace('[Error 5] ', 'Access denied').replace('[Error 3] ', 'Not exists, press r to refresh')
437461
if not ST3 and LIN:
438462
error = error.decode('utf8')
439-
else:
463+
if (error == None):
464+
items = [item for item in items if not self.is_hidden(item['file'], path)]
440465
sort_nicely(items)
441-
finally:
442-
return items, error
466+
return items, error
443467

444468
def try_listing_only_dirs(self, path):
445469
'''Same as self.try_listing_directory, but items contains only directories.
446470
Used for prompt completion'''
447-
items, error = self.try_listing_directory(path)
448-
if items:
449-
items = [n for n in items if isdir(join(path, n))]
450-
return (items, error)
471+
return self.try_listing_directory(path, dirs_only=True)
451472

452473
def restore_marks(self, marked=None):
453474
if marked:

dired.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def populate_view(self, edit, path, names):
226226
u'\t<%s>' % error)
227227
self.view.set_read_only(True)
228228
else:
229+
items = [file['file'] for file in items]
229230
self.continue_populate(edit, path, items)
230231

231232
def continue_populate(self, edit, path, names):
@@ -260,10 +261,11 @@ def traverse_tree(self, root, path, indent, tree, expanded):
260261

261262
files = []
262263
index_files = []
263-
for f in items:
264+
for item in items:
265+
f = item['file']
264266
new_path = join(path, f)
265267
dir_path = u'%s%s' % (new_path.rstrip(os.sep), os.sep)
266-
check = isdir(new_path)
268+
check = item['isdir'] == True
267269
if check and dir_path in expanded:
268270
self.traverse_tree(root, dir_path, indent + '\t', tree, expanded)
269271
elif check:
@@ -497,6 +499,7 @@ def expand_single_directory(self, edit, filename, toggle):
497499
if error:
498500
replacement = [u'%s\t<%s>' % (root, error)]
499501
elif items:
502+
items = [file['file'] for file in items]
500503
replacement = [root] + self.prepare_filelist(items, '', filename, '\t')
501504
dired_count = self.view.settings().get('dired_count', 0)
502505
self.view.settings().set('dired_count', dired_count + len(items))

prompt.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ def get_completions(self, path, prefix):
125125
'''return tuple (completion(list, may be empty), error(boolean))'''
126126
# self.view is prompt, so get settings of active view in active window
127127
self.show_hidden = sublime.active_window().active_view().settings().get('dired_show_hidden_files', True)
128-
dirs, error = self.try_listing_only_dirs(path)
128+
items, error = self.try_listing_only_dirs(path)
129129
if error:
130130
sublime.error_message(u'FileBrowser:\n\n Content is unavailable\n\n\t%s\n\n\t%s' % (path, error))
131131
return ([], True)
132+
dirs = [file['file'] for file in items]
132133
completions = [n for n in dirs if n.upper().startswith(prefix.upper())]
133134
return (completions, False)
134135

0 commit comments

Comments
 (0)