arigo / hack / pypy-hack /

Armin Rigo b2cf8de 

#! /usr/bin/env python

# Magic line for bash (blame obscure shells):
#    complete -o bashdefault -o default -C /path/to/ py.test

import sys

complete_with_test_prefix = True

linestarts = [("def ", "test_"), ("class ", "Test")]

command, wordstart, previousword = sys.argv[1:4]
if wordstart == '-k':
    wordstart = ''
    addspace = '-k '
    if previousword != '-k':
        sys.exit(0)    # use default completion
    addspace = ''

import os
line = os.environ['COMP_LINE']

filenames = [arg for arg in line.split()
             if (arg.endswith('.py') or arg.endswith('.pl')) and
place = wordstart.find('.')
if '.' in wordstart:
    addspace = wordstart[:place + 1]
    wordstart = wordstart[place + 1:]

def gen_lines(arg):
        f = open(arg, 'r')
        lines = f.readlines()
    except (OSError, IOError):
    for line in lines:
        yield line.lstrip()

def complete_python(arg, filename):
    for line in gen_lines(arg):
        for (keyword, prefix) in linestarts:
            if not line.startswith(keyword + prefix):
            testname = line[len(keyword):]
            i = testname.find('(')
            if i < 0:
                i = testname.find(':')
                if i < 0:
            if complete_with_test_prefix:
                testname = testname[:i]
                if testname.startswith(wordstart):
                    print addspace + testname
                testname = testname[len(prefix):i]
                if testname.startswith(wordstart):
                    print addspace + testname
            if line.startswith('# =='):
                # follow comments of the form:
                # ===>
                parts = line.split(None, 2)
                if len(parts) == 3:
                    fn = parts[2].strip()
                    fn = os.path.join(os.path.dirname(arg), fn)
                    fn = os.path.abspath(fn)
                    if fn not in filenames:

def complete_prolog(arg, filenames):
    for line in gen_lines(arg):
        if line.startswith("test_"):
            i = line.find(' :-')
            if i < 0:
            testname = line[:i]
            if testname.startswith(wordstart):
                print addspace + testname

for arg in filenames:
    if arg.endswith(".py"):
        complete_python(arg, filenames)
        assert arg.endswith(".pl")
        complete_prolog(arg, filenames)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.