Skip to content

Commit

Permalink
[completion] Simplify and improve FileSystemAction.
Browse files Browse the repository at this point in the history
It still has the issue where .. doesn't get completed with a trailing
slash.  bash does this but zsh doesn't.

Addresses issue #240.
  • Loading branch information
Andy Chu committed Feb 28, 2019
1 parent 36843bd commit b7f8182
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
29 changes: 18 additions & 11 deletions core/completion.py
Expand Up @@ -322,25 +322,32 @@ def __init__(self, dirs_only=False, exec_only=False, add_slash=False):

def Matches(self, comp):
to_complete = comp.to_complete
i = to_complete.rfind('/')
if i == -1: # it looks like 'foo'

# Problem: .. and ../.. don't complete /.
# TODO: Set display_pos before fixing this.

#import os
#to_complete = os.path.normpath(to_complete)

dirname, basename = os_path.split(to_complete)
if dirname == '': # We're completing in this directory
to_list = '.'
base = ''
elif i == 0: # it's an absolute path to_complete like / or /b
to_list = '/'
base = '/'
else:
to_list = to_complete[:i]
base = to_list
#log('to_list %r', to_list)
else: # We're completing in some other directory
to_list = dirname

if 0:
log('basename %r', basename)
log('to_list %r', to_list)
log('dirname %r', dirname)

try:
names = posix.listdir(to_list)
except OSError as e:
return # nothing

for name in names:
path = os_path.join(base, name)
path = os_path.join(dirname, name)

if path.startswith(to_complete):
if self.dirs_only: # add_slash not used here
# NOTE: There is a duplicate isdir() check later to add a trailing
Expand Down
18 changes: 15 additions & 3 deletions core/completion_test.py
Expand Up @@ -127,14 +127,25 @@ def testExternalCommandAction(self):
# executable files are accessed!

def testFileSystemAction(self):
a = completion.FileSystemAction(add_slash=True)
CASES = [
# Dirs and files
('c', ['core', 'configure']),
('opy/doc', ['opy/doc']),
]

a = completion.FileSystemAction()
for prefix, expected in CASES:
log('')
log('-- PREFIX %r', prefix)
comp = self._CompApi([], 0, prefix)
self.assertEqual(expected, list(a.Matches(comp)))

os.system('mkdir -p /tmp/oil_comp_test')
os.system('bash -c "touch /tmp/oil_comp_test/{one,two,three}"')

# This test depends on actual file system content. But we choose things
# that shouldn't go away.
CASES = [
ADD_SLASH_CASES = [
# Dirs and files
('c', ['core/', 'configure']),
('nonexistent/', []),
Expand All @@ -153,7 +164,8 @@ def testFileSystemAction(self):
('./b', ['./bin/', './benchmarks/', './build/']),
]

for prefix, expected in CASES:
a = completion.FileSystemAction(add_slash=True)
for prefix, expected in ADD_SLASH_CASES:
log('')
log('-- PREFIX %s', prefix)
comp = self._CompApi([], 0, prefix)
Expand Down

0 comments on commit b7f8182

Please sign in to comment.