diff --git a/core/completion.py b/core/completion.py index e0da053924..cd7289fd74 100755 --- a/core/completion.py +++ b/core/completion.py @@ -322,17 +322,23 @@ 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) @@ -340,7 +346,8 @@ def Matches(self, comp): 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 diff --git a/core/completion_test.py b/core/completion_test.py index 5a338fa09e..5672c15124 100755 --- a/core/completion_test.py +++ b/core/completion_test.py @@ -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/', []), @@ -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)