# 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: 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 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]
# 
# def cleantabs(text):
#     lines = text.splitlines()
#     if not lines[0].strip():
#         lines = lines[1:]
# 
#     toremove = 999
#     for l in lines:
#         stripped = l.lstrip()
#         if stripped:
#             toremove = min(toremove, len(l) - len(stripped))
# 
#     if toremove < 999:
#         return '\n'.join(l[toremove:] for l in lines)
#     else:
#         return '\n'.join(lines)


import pytest
from supplement.fixer import fix, sanitize_encoding
from .helpers import pytest_funcarg__project, do_assist, cleantabs
    source = sanitize_encoding(u'# coding: utf-8\n\n\ncoding="utf-8"')
    assert source == u'# codang: utf-8\n\n\ncoding="utf-8"'

def test_incomlete_if(project):
    result = do_assist(project, '''
        var = 1
        if |
    ''')
    assert 'var' in result

    result = do_assist(project, '''
        var = 1
        if v|
    ''')
    assert 'var' in result

    result = do_assist(project, '''
        def foo():
            var = 1
            if |
    ''')
    assert 'var' in result

    result = do_assist(project, '''
        def foo():
            var = 1
            if v|
    ''')
    assert 'var' in result


# 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: 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 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]
# 
# def cleantabs(text):
#     lines = text.splitlines()
#     if not lines[0].strip():
#         lines = lines[1:]
# 
#     toremove = 999
#     for l in lines:
#         stripped = l.lstrip()
#         if stripped:
#             toremove = min(toremove, len(l) - len(stripped))
# 
#     if toremove < 999:
#         return '\n'.join(l[toremove:] for l in lines)
#     else:
#         return '\n'.join(lines)


# Path: tests/test_fixer.py
import pytest
from supplement.fixer import fix, sanitize_encoding
from .helpers import pytest_funcarg__project, do_assist, cleantabs

    source = sanitize_encoding(u'# coding: utf-8\n\n\ncoding="utf-8"')
    assert source == u'# codang: utf-8\n\n\ncoding="utf-8"'

def test_incomlete_if(project):
    result = do_assist(project, '''
        var = 1
        if |
    ''')
    assert 'var' in result

    result = do_assist(project, '''
        var = 1
        if v|
    ''')
    assert 'var' in result

    result = do_assist(project, '''
        def foo():
            var = 1
            if |
    ''')
    assert 'var' in result

    result = do_assist(project, '''
        def foo():
            var = 1
            if v|
    ''')
    assert 'var' in result


    result = do_assist(project, '''