# Path: supplement/assistant.py
# def assist(project, source, position, filename):
#     logging.getLogger(__name__).info('assist %s %s', project.root, filename)
#     ctx_type, lineno, ctx, match, fctx = get_context(source, position)
#     if ctx_type == 'expr':
#         source = sanitize_encoding(source)
#         ast_nodes, fixed_source = fix(source)
# 
#         scope = get_scope_at(project, fixed_source, lineno, filename, ast_nodes)
#         project.calldb.collect_calls(scope.get_toplevel(), True)
#         if not ctx:
#             names = get_scope_names(scope, lineno)
#             if fctx:
#                 funcobj = infer(fctx, scope, lineno)
#                 args = funcobj.get_signature()
#                 if args:
#                     names = [(r + '=' for r in args[1])] + list(names)
#         else:
#             obj = infer(ctx, scope, lineno)
#             names = [obj.get_names()]
#     elif ctx_type == 'import':
#         names = (project.get_possible_imports(ctx, filename),)
#     elif ctx_type == 'from-import':
#         names = (
#             project.get_module(ctx, filename).get_names(),
#             project.get_possible_imports(ctx, filename))
#     elif ctx_type is None:
#         return match, []
# 
#     return match, collect_names(match, names)
# 
# def get_location(project, source, position, filename):
#     position = get_id_ending(source, position)
#     ctx_type, lineno, ctx, match, fctx = get_context(source, position)
# 
#     if ctx_type == 'expr':
#         source = sanitize_encoding(source)
#         ast_nodes, fixed_source = fix(source)
#         scope = get_scope_at(project, fixed_source, lineno, filename, ast_nodes)
#         if not ctx:
#             obj = scope.find_name(match, lineno)
#         else:
#             obj = infer(ctx, scope, lineno)[match]
# 
#         return obj.get_location()
# 
#     elif ctx_type in ('import', 'from-import'):
#         if ctx:
#             if ctx[-1] == '.':
#                 ctx = ctx[:-1]
# 
#             module_name = ctx + '.' + match
#         else:
#             module_name = match
# 
#         try:
#             module = project.get_module(module_name, filename)
#         except ImportError:
#             module_name, _, _ = module_name.rpartition('.')
#             if module_name:
#                 module = project.get_module(module_name, filename)
#                 return module[match].get_location()
#         else:
#             return 1, module.filename
# 
#     return None, None
# 
# def get_context(source, position):
#     lines, lineno = get_block(source, position)
# 
#     tokens = TokenGenerator(lines)
#     ctype, ctx, match, fctx = 'expr', '', '', ''
#     while True:
#         tid, value = tokens.next()
#         if not tid: break
# 
#         if tid == NAME and value == 'import':
#             ctype, fctx = 'import', None
#             ctx, match = parse_import(tokens)
# 
#         elif tid == NAME and value == 'from':
#             fctx = None
#             ctype, ctx, match = parse_from(tokens)
# 
#         elif tid == NAME or value in BRACKETS.keys():
#             ctype = 'expr'
#             tokens.hold(tid, value)
#             ctx, match, fctx = parse_expr(tokens)
#             ctx = untokenize(prep_tokens(ctx)).strip().rstrip('.')
#             fctx = untokenize(prep_tokens(fctx)).strip().rstrip('.')
# 
#         else:
#             ctype, ctx, match, fctx = 'expr', '', '', ''
# 
#     return ctype, lineno, ctx, match, fctx
# 
# def get_docstring(project, source, position, filename):
#     position = get_id_ending(source, position)
#     ctx_type, lineno, ctx, match, fctx = get_context(source, position)
#     if ctx_type == 'expr' and fctx:
#         source = sanitize_encoding(source)
#         ast_nodes, fixed_source = fix(source)
#         scope = get_scope_at(project, fixed_source, lineno, filename, ast_nodes)
#         obj = infer(fctx, scope, lineno)
# 
#         sig = obj.get_signature()
#         if sig:
#             sig = '%s%s' % (sig[0], formatargspec(*sig[1:]))
# 
#         return sig, obj.get_docstring()
# 
#     return None
# 
# Path: tests/helpers.py
# def pytest_funcarg__project(request):
#     for crap_module in ('toimport', 'package'):
#         if crap_module in sys.modules:
#             del sys.modules[crap_module]
# 
#     p = Project('.')
#     p.create_module = create_module.__get__(p, Project)
#     p.create_scope = create_scope.__get__(p, Project)
#     p.set_root = set_project_root.__get__(p, Project)
#     return p
# 
# def get_source_and_pos(source):
#     source = cleantabs(source)
#     pos = source.find('|')
#     if pos < 0:
#         pos = len(source)
# 
#     return source.replace('|', ''), pos
# 
# def do_assist(project, source, filename=None):
#     filename = filename or 'test.py'
#     source, pos = get_source_and_pos(source)
#     return assist(project, source, pos, filename)[1]


import pytest
import time
    import os.path
from supplement.assistant import assist, get_location, get_context, get_docstring
from .helpers import pytest_funcarg__project, get_source_and_pos, do_assist



def test_assist_for_module_names(project):
    result = do_assist(project, '''
        test = 1

# Path: supplement/assistant.py
# def assist(project, source, position, filename):
#     logging.getLogger(__name__).info('assist %s %s', project.root, filename)
#     ctx_type, lineno, ctx, match, fctx = get_context(source, position)
#     if ctx_type == 'expr':
#         source = sanitize_encoding(source)
#         ast_nodes, fixed_source = fix(source)
# 
#         scope = get_scope_at(project, fixed_source, lineno, filename, ast_nodes)
#         project.calldb.collect_calls(scope.get_toplevel(), True)
#         if not ctx:
#             names = get_scope_names(scope, lineno)
#             if fctx:
#                 funcobj = infer(fctx, scope, lineno)
#                 args = funcobj.get_signature()
#                 if args:
#                     names = [(r + '=' for r in args[1])] + list(names)
#         else:
#             obj = infer(ctx, scope, lineno)
#             names = [obj.get_names()]
#     elif ctx_type == 'import':
#         names = (project.get_possible_imports(ctx, filename),)
#     elif ctx_type == 'from-import':
#         names = (
#             project.get_module(ctx, filename).get_names(),
#             project.get_possible_imports(ctx, filename))
#     elif ctx_type is None:
#         return match, []
# 
#     return match, collect_names(match, names)
# 
# def get_location(project, source, position, filename):
#     position = get_id_ending(source, position)
#     ctx_type, lineno, ctx, match, fctx = get_context(source, position)
# 
#     if ctx_type == 'expr':
#         source = sanitize_encoding(source)
#         ast_nodes, fixed_source = fix(source)
#         scope = get_scope_at(project, fixed_source, lineno, filename, ast_nodes)
#         if not ctx:
#             obj = scope.find_name(match, lineno)
#         else:
#             obj = infer(ctx, scope, lineno)[match]
# 
#         return obj.get_location()
# 
#     elif ctx_type in ('import', 'from-import'):
#         if ctx:
#             if ctx[-1] == '.':
#                 ctx = ctx[:-1]
# 
#             module_name = ctx + '.' + match
#         else:
#             module_name = match
# 
#         try:
#             module = project.get_module(module_name, filename)
#         except ImportError:
#             module_name, _, _ = module_name.rpartition('.')
#             if module_name:
#                 module = project.get_module(module_name, filename)
#                 return module[match].get_location()
#         else:
#             return 1, module.filename
# 
#     return None, None
# 
# def get_context(source, position):
#     lines, lineno = get_block(source, position)
# 
#     tokens = TokenGenerator(lines)
#     ctype, ctx, match, fctx = 'expr', '', '', ''
#     while True:
#         tid, value = tokens.next()
#         if not tid: break
# 
#         if tid == NAME and value == 'import':
#             ctype, fctx = 'import', None
#             ctx, match = parse_import(tokens)
# 
#         elif tid == NAME and value == 'from':
#             fctx = None
#             ctype, ctx, match = parse_from(tokens)
# 
#         elif tid == NAME or value in BRACKETS.keys():
#             ctype = 'expr'
#             tokens.hold(tid, value)
#             ctx, match, fctx = parse_expr(tokens)
#             ctx = untokenize(prep_tokens(ctx)).strip().rstrip('.')
#             fctx = untokenize(prep_tokens(fctx)).strip().rstrip('.')
# 
#         else:
#             ctype, ctx, match, fctx = 'expr', '', '', ''
# 
#     return ctype, lineno, ctx, match, fctx
# 
# def get_docstring(project, source, position, filename):
#     position = get_id_ending(source, position)
#     ctx_type, lineno, ctx, match, fctx = get_context(source, position)
#     if ctx_type == 'expr' and fctx:
#         source = sanitize_encoding(source)
#         ast_nodes, fixed_source = fix(source)
#         scope = get_scope_at(project, fixed_source, lineno, filename, ast_nodes)
#         obj = infer(fctx, scope, lineno)
# 
#         sig = obj.get_signature()
#         if sig:
#             sig = '%s%s' % (sig[0], formatargspec(*sig[1:]))
# 
#         return sig, obj.get_docstring()
# 
#     return None
# 
# Path: tests/helpers.py
# def pytest_funcarg__project(request):
#     for crap_module in ('toimport', 'package'):
#         if crap_module in sys.modules:
#             del sys.modules[crap_module]
# 
#     p = Project('.')
#     p.create_module = create_module.__get__(p, Project)
#     p.create_scope = create_scope.__get__(p, Project)
#     p.set_root = set_project_root.__get__(p, Project)
#     return p
# 
# def get_source_and_pos(source):
#     source = cleantabs(source)
#     pos = source.find('|')
#     if pos < 0:
#         pos = len(source)
# 
#     return source.replace('|', ''), pos
# 
# def do_assist(project, source, filename=None):
#     filename = filename or 'test.py'
#     source, pos = get_source_and_pos(source)
#     return assist(project, source, pos, filename)[1]


# Path: tests/test_assist.py
import pytest
import time
    import os.path
from supplement.assistant import assist, get_location, get_context, get_docstring
from .helpers import pytest_funcarg__project, get_source_and_pos, do_assist




def test_assist_for_module_names(project):
    result = do_assist(project, '''
        test = 1

        te''')