Exemplo n.º 1
0
def fix_imports(index, source):
    scope = importmagic.Scope.from_source(source)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    source = importmagic.update_imports(
        source, index, unresolved, unreferenced
    )
    return source
Exemplo n.º 2
0
def fix_imports_in_code(index, source):
    scope = importmagic.Scope.from_source(source)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    # start_line, end_line, import_block = importmagic.get_update(source, index, unresolved, unreferenced)
    source = importmagic.update_imports(source, index, unresolved,
                                        unreferenced)
    return source
Exemplo n.º 3
0
def magic(refresh=None):
    if not imported:
        print("No importmagic. Run 'pip install importmagic'")
        return
    refresh = True if refresh == "!" else False
    current = vim.current.buffer  # or vim.current.range
    text = '\n'.join(current)

    config = vim.eval('g:vim_importmagic_config')
    if not isinstance(config, dict):
        print('g:vim_importmagic_config should be dict, found %s' %
              type(config))
        return

    path = sys.path + [os.getcwd()]

    # XXX indexing takes too long do it async in background!?
    index = importmagic.SymbolIndex()
    index.get_or_create_index(paths=path, refresh=refresh)

    scope = importmagic.Scope.from_source(text)

    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    text_updated = importmagic.update_imports(text, index, unresolved,
                                              unreferenced)
    text_updated = text_updated.split('\n')[:-1]
    current[:] = text_updated
Exemplo n.º 4
0
def add_import(paths, index):
    logging.info("add_import :" + str(paths))
    path_ = sys.path + [os.getcwd()]
    for path in paths:
        with open(path) as f:
            python_source = f.read()
        scope = importmagic.Scope.from_source(python_source)
        unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols(
        )
        python_source = importmagic.update_imports(python_source, index,
                                                   unresolved, unreferenced)
        with open(path, 'w') as f:
            f.write(python_source)
Exemplo n.º 5
0
def importmagic_fixup(index, code):
    scope = importmagic.Scope.from_source(code)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    code = importmagic.update_imports(code, index, unresolved, unreferenced)
    return code