# Path: supplement/fixer.py
# def fix(code, tries=10):
#     try:
#         return ast.parse(code), code
#     except Exception, e:
#         tries -= 1
#         if tries <= 0:
#             raise
# 
#         code, fixed_location = try_to_fix(e, code)
#         if fixed_location:
#             return fix(code, tries)
#         else:
#             raise
# 
# def sanitize_encoding(source):
#     if isinstance(source, unicode):
#         parts = source.split(u'\n', 4)
#         for i in range(min(3, len(parts))):
#             parts[i] = parts[i].replace(u'coding=', u'codang=').replace(u'coding:', u'codang:')
# 
#         return u'\n'.join(parts)
#     else:
#         return source
# 
# Path: supplement/scope.py
# def get_scope_at(project, source, lineno, filename=None, ast_node=None, continous=True):
#     ast_node = ast_node or ast.parse(source)
# 
#     scope = Scope(ast_node, '', None, 'module')
#     scope.project = project
#     scope.filename = filename
#     return scope.get_scope_at(source, lineno, continous)
# 
# Path: supplement/evaluator.py
# def infer(string, scope, lineno=None):
#     tree = ast.parse(string, '<string>', 'eval')
# 
#     if lineno:
#         ast.increment_lineno(tree, lineno-1)
# 
#     return Evaluator().process(tree, scope)


import logging
from keyword import iskeyword
from inspect import formatargspec
from tokenize import (NAME, NL, NEWLINE, TokenError, generate_tokens,
    untokenize, ERRORTOKEN, INDENT, DEDENT)
from .fixer import fix, sanitize_encoding
from .scope import get_scope_at
from .evaluator import infer
        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 char_is_id(c):
    return c == '_' or c.isalnum()

def get_id_ending(source, position):
    source_len = len(source)
    while position < source_len and char_is_id(source[position]):
        position += 1

    return position

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)

# Path: supplement/fixer.py
# def fix(code, tries=10):
#     try:
#         return ast.parse(code), code
#     except Exception, e:
#         tries -= 1
#         if tries <= 0:
#             raise
# 
#         code, fixed_location = try_to_fix(e, code)
#         if fixed_location:
#             return fix(code, tries)
#         else:
#             raise
# 
# def sanitize_encoding(source):
#     if isinstance(source, unicode):
#         parts = source.split(u'\n', 4)
#         for i in range(min(3, len(parts))):
#             parts[i] = parts[i].replace(u'coding=', u'codang=').replace(u'coding:', u'codang:')
# 
#         return u'\n'.join(parts)
#     else:
#         return source
# 
# Path: supplement/scope.py
# def get_scope_at(project, source, lineno, filename=None, ast_node=None, continous=True):
#     ast_node = ast_node or ast.parse(source)
# 
#     scope = Scope(ast_node, '', None, 'module')
#     scope.project = project
#     scope.filename = filename
#     return scope.get_scope_at(source, lineno, continous)
# 
# Path: supplement/evaluator.py
# def infer(string, scope, lineno=None):
#     tree = ast.parse(string, '<string>', 'eval')
# 
#     if lineno:
#         ast.increment_lineno(tree, lineno-1)
# 
#     return Evaluator().process(tree, scope)


# Path: supplement/assistant.py
import logging
from keyword import iskeyword
from inspect import formatargspec
from tokenize import (NAME, NL, NEWLINE, TokenError, generate_tokens,
    untokenize, ERRORTOKEN, INDENT, DEDENT)
from .fixer import fix, sanitize_encoding
from .scope import get_scope_at
from .evaluator import infer

        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 char_is_id(c):
    return c == '_' or c.isalnum()

def get_id_ending(source, position):
    source_len = len(source)
    while position < source_len and char_is_id(source[position]):
        position += 1

    return position

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)