Skip to content

Commit ea2798a

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

File tree

4 files changed

+67
-34
lines changed

4 files changed

+67
-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: 53 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,77 @@ 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, item, 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+
filename = item['file']
409+
fullpath = join(path, goto, filename)
410+
is_dir = item['is_dir']
411+
result = lambda: is_hidden if not get_is_dir else [is_hidden, is_dir]
412+
if dirs_only and not is_dir:
413+
return result()
414+
if not is_hidden and not show_excluded:
415+
if is_dir:
416+
tests = self.view.settings().get('folder_exclude_patterns', [])
417+
if any(fnmatch.fnmatch(filename, p) for p in tests):
418+
is_hidden = True
419+
else:
420+
tests = self.view.settings().get('file_exclude_patterns', [])
421+
if any(fnmatch.fnmatch(filename, p) for p in tests):
422+
is_hidden = True
423+
if not is_hidden and not show_hidden:
424+
tests = self.view.settings().get('dired_hidden_files_patterns', ['.*'])
425+
if isinstance(tests, str):
426+
tests = [tests]
427+
if any(fnmatch.fnmatch(filename, p) for p in tests):
428+
is_hidden = True
429+
if not is_hidden and NT:
430+
# check for attribute on windows:
431+
try:
432+
attrs = ctypes.windll.kernel32.GetFileAttributesW(fullpath)
433+
assert attrs != -1
434+
if bool(attrs & 2):
435+
is_hidden = True
436+
except:
437+
pass
438+
return result()
439+
440+
def listdir(self, path):
441+
return [{'file': file, 'is_dir': isdir(join(path, file))} for file in os.listdir(path)]
442+
443+
def listdir_only_dirs(self, path):
444+
return [item for item in self.listdir(path) if item["isdir"] == True]
445+
446+
def try_listing_directory(self, path, dirs_only=False):
423447
'''Return tuple of two element
424448
items sorted list of filenames in path, or empty list
425449
error exception message, or empty string
426450
'''
427-
items, error = [], ''
451+
items = []
452+
error = None
428453
try:
429-
if not self.show_hidden:
430-
items = [name for name in os.listdir(path) if not self.is_hidden(name, path)]
454+
if dirs_only:
455+
items = self.listdir_only_dirs(path)
431456
else:
432-
items = os.listdir(path)
457+
items = self.listdir(path)
433458
except OSError as e:
434459
error = str(e)
435460
if NT:
436461
error = error.split(':')[0].replace('[Error 5] ', 'Access denied').replace('[Error 3] ', 'Not exists, press r to refresh')
437462
if not ST3 and LIN:
438463
error = error.decode('utf8')
439-
else:
464+
if (error == None):
465+
items = [item for item in items if not self.is_hidden(item, path)]
440466
sort_nicely(items)
441-
finally:
442-
return items, error
467+
return items, error
443468

444469
def try_listing_only_dirs(self, path):
445470
'''Same as self.try_listing_directory, but items contains only directories.
446471
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)
472+
return self.try_listing_directory(path, dirs_only=True)
451473

452474
def restore_marks(self, marked=None):
453475
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['is_dir'] == 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)