Skip to content

Instantly share code, notes, and snippets.

@vivainio
Created May 8, 2011 06:43
Show Gist options
  • Save vivainio/961173 to your computer and use it in GitHub Desktop.
Save vivainio/961173 to your computer and use it in GitHub Desktop.
import re
import leo.core.leoGlobals as g
def attr_matches( text, namespace):
"""Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME....[NAME], and is
evaluatable in self.namespace or self.global_namespace, it will be
evaluated and its attributes (as revealed by dir()) are used as
possible completions. (For class instances, class members are are
also considered.)
WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.
"""
# Another option, seems to work great. Catches things like ''.<tab>
m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
if not m:
return []
expr, attr = m.group(1, 3)
try:
obj = eval(expr, namespace)
except:
return []
words = dir(obj)
# Build match list to return
n = len(attr)
res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
return res
def complete_live_objects(c,p, expr):
ns = { 'c' : c, 'p': p, 'g': g }
g.es(attr_matches(expr, ns))
def test(c,p):
pats = ['c.all',
'p.ins',
'c.B',
'g.com',
'c.frame.top.'
]
for pat in pats:
complete_live_objects(c,p, pat)
test(c,p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment