Permalink
Browse files

completion.py: Replace Python fnmatch with libc fnmatch.

This is to reduce our dependency on the Python interpreter.  I just
wrote basic unit tests to prevent trivial regressions.  completion.py
still needs a lot of work.
  • Loading branch information...
Andy Chu
Andy Chu committed Feb 2, 2018
1 parent 3483cbc commit 706724fe01d38f298504a45cac642b0b92ae3a5f
Showing with 22 additions and 7 deletions.
  1. +9 −3 core/completion.py
  2. +13 −4 core/completion_test.py
View
@@ -30,7 +30,6 @@
"""
import atexit
import fnmatch
import readline
import os
import sys
@@ -45,6 +44,8 @@
from core import ui
from core import util
import libc
command_e = ast.command_e
value_e = runtime.value_e
@@ -128,7 +129,8 @@ def GetCompleterForName(self, argv0):
return chain
for glob_pat, chain in self.patterns:
if fnmatch.fnmatch(key, glob_pat):
#log('Matching %r %r', key, glob_pat)
if libc.fnmatch(glob_pat, key):
return chain
return self.lookup['__default__']
@@ -326,7 +328,7 @@ def __init__(self, glob_pat):
self.glob_pat = glob_pat
def __call__(self, match):
return fnmatch.fnmatch(match, self.glob_pat)
return libc.fnmatch(self.glob_pat, match)
class ChainedCompleter(object):
@@ -363,6 +365,10 @@ def Matches(self, words, index, prefix):
# Hm actually zsh does something smarter, and which is probably preferable.
# It completes the word that
def __str__(self):
return '<ChainedCompleter %s %s %r %r>' % (
self.actions, self.predicate, self.prefix, self.suffix)
class DummyParser(object):
def GetWords(self, buf):
View
@@ -8,6 +8,7 @@
"""
completion_test.py: Tests for completion.py
"""
from __future__ import print_function
import unittest
@@ -52,9 +53,15 @@ def testLookup(self):
print(c.GetCompleterForName('/usr/bin/grep'))
c.RegisterGlob('*.py', C1)
print(c.GetCompleterForName('/usr/bin/foo.py'))
comp = c.GetCompleterForName('/usr/bin/foo.py')
print('py', comp)
# NOTE: This is an implementation detail
self.assertEqual(1, len(comp.actions))
print(c.GetCompleterForName('foo.rb'))
comp_rb = c.GetCompleterForName('foo.rb')
print('rb', comp_rb)
# NOTE: This is an implementation detail
self.assertEqual(0, len(comp_rb.actions))
def testWordsAction(self):
print(list(A1.Matches(['f'], 0, 'f')))
@@ -95,11 +102,13 @@ def testShellFuncExecution(self):
self.assertEqual(['f1 ', 'f2 '], matches)
def testChainedCompleter(self):
print(list(C1.Matches(['f'], 0, 'f')))
matches = list(C1.Matches(['f'], 0, 'f'))
self.assertEqual(['foo.py ', 'foo '], matches)
p = completion.GlobPredicate('*.py')
c2 = completion.ChainedCompleter([A1], predicate=p)
print(list(c2.Matches(['f'], 0, 'f')))
matches = list(c2.Matches(['f'], 0, 'f'))
self.assertEqual([], matches)
def testRootCompleter(self):
comp_lookup = completion.CompletionLookup()

0 comments on commit 706724f

Please sign in to comment.