Example #1
0
def setup(vim: Nvim, level: str, output_file: str = '') -> None:
    """Setup logging for Deoplete
    """
    global init
    if init:
        return
    init = True

    if output_file:
        formatter = logging.Formatter(log_format)
        handler = logging.FileHandler(filename=output_file)
        handler.setFormatter(formatter)
        handler.addFilter(DeopleteLogFilter(vim))
        root.addHandler(handler)

        level = str(level).upper()
        if level not in ('DEBUG', 'INFO', 'WARN', 'WARNING', 'ERROR',
                         'CRITICAL', 'FATAL'):
            level = 'DEBUG'
        root.setLevel(getattr(logging, level))

        log = getLogger('logging')
        log.info('--- Deoplete Log Start ---')
        log.info('%s, Python %s', vim.call('deoplete#util#neovim_version'),
                 '.'.join(map(str, sys.version_info[:3])))

        if 'deoplete#_logging_notified' not in vim.vars:
            vim.vars['deoplete#_logging_notified'] = 1
            vim.call('deoplete#util#print_debug',
                     'Logging to %s' % (output_file))
Example #2
0
def error(vim: Nvim, expr: typing.Any) -> None:
    """
    Prints the error messages to Vim/Nvim's :messages buffer.
    """
    if isinstance(expr, set):
        expr = [str(x) for x in expr]
    vim.call('defx#util#print_error', str(expr))
Example #3
0
def error_tb(vim: Nvim, msg: str) -> None:
    lines: typing.List[str] = []
    t, v, tb = sys.exc_info()
    if t and v and tb:
        lines += traceback.format_exc().splitlines()
    lines += ['%s.  Use :messages / see above for error details.' % msg]
    if hasattr(vim, 'err_write'):
        vim.err_write('[deoplete] %s\n' % '\n'.join(lines))
    else:
        for line in lines:
            vim.call('deoplete#util#print_error', line)
Example #4
0
def cwd_input(vim: Nvim, cwd: str, prompt: str,
              text: str = '', completion: str = '') -> str:
    """
    Returns the absolute input path in cwd.
    """
    save_cwd = vim.call('getcwd')
    cd(vim, cwd)

    filename: str = str(vim.call('defx#util#input', prompt, text, completion))
    filename = filename.strip()

    cd(vim, save_cwd)

    return filename
Example #5
0
def _ex(vim: Nvim, word: str) -> None:
    # NOTE:
    # <C-b> (\x02) in a command-line move the caret to the beginning.
    # Somehow the key above works in 'input()' function as well.
    expr = vim.call('input', ':', ' %s\x02' % word, 'command')
    if expr:
        vim.command(expr)
Example #6
0
def input(vim: Nvim, context, prompt='', text='', completion=''):
    try:
        if completion != '':
            return vim.call('input', prompt, text, completion)
        else:
            return vim.call('input', prompt, text)
    except vim.error as e:
        # NOTE:
        # neovim raise nvim.error instead of KeyboardInterrupt when Ctrl-C
        # has pressed so treat it as a real KeyboardInterrupt exception.
        if str(e) != "b'Keyboard interrupt'":
            raise e
    except KeyboardInterrupt:
        pass
    # Ignore the interrupt
    return ''
Example #7
0
def abspath(vim: Nvim, pathstr: str) -> str:
    path = Path(expand(pathstr))
    if not path.is_absolute():
        path = Path(vim.call('getcwd')).joinpath(pathstr)
    if readable(path):
        path = path.resolve()
    return str(path)
Example #8
0
def confirm(vim: Nvim, question: str) -> bool:
    """
    Confirm action
    """
    option: int = vim.call('defx#util#confirm',
                           question, '&Yes\n&No\n&Cancel', 2)
    return option == 1
Example #9
0
def cwd_input(vim: Nvim, cwd: str, prompt: str,
              text: str = '', completion: str = '') -> typing.Optional[Path]:
    """
    Returns the absolute input path in cwd.
    """
    save_cwd = vim.call('getcwd')
    vim.command(f'silent lcd {cwd}')

    filename: str = vim.call('input', prompt, text, completion)

    vim.command(f'silent lcd {save_cwd}')

    if not filename:
        return None

    return Path(cwd).joinpath(filename).resolve()
Example #10
0
def truncate(vim: Nvim, word: str, max_length: int) -> str:
    if (strwidth(vim, word) > max_length
            or len(word) != len(bytes(word, 'utf-8', 'surrogatepass'))):
        return str(
            vim.call('denite#util#truncate', word, max_length,
                     int(max_length / 2), '...'))

    return word
Example #11
0
def clearmatch(vim: Nvim) -> None:
    if not vim.call('exists', 'w:denite_match_id'):
        return

    # For async RPC error
    vim.command('silent! call matchdelete({})'.format(
        vim.current.window.vars['denite_match_id']))
    vim.command('silent! unlet w:denite_match_id')
Example #12
0
def getlines(vim: Nvim, start=1, end='$'):
    if end == '$':
        end = len(vim.current.buffer)
    max_len = min([end - start, 5000])
    lines: typing.List[str] = []
    current = start
    while current <= end:
        lines += vim.call('getline', current, current + max_len)
        current += max_len + 1
    return lines
Example #13
0
def getlines(vim: Nvim, start: int = 1,
             end: typing.Union[int, str] = '$') -> typing.List[str]:
    if end == '$':
        end = len(vim.current.buffer)
    max_len = min([int(end) - start, 5000])
    lines: typing.List[str] = []
    current = start
    while current <= int(end):
        lines += vim.call('getline', current, current + max_len)
        current += max_len + 1
    return lines
Example #14
0
def _paste(vim: Nvim, word: str, command: str, regtype: str) -> None:
    if regtype == '':
        regtype = 'v'

    # Paste.
    old_reg = [vim.call('getreg', '"'), vim.call('getregtype', '"')]

    vim.call('setreg', '"', word, regtype)
    try:
        vim.command('normal! ""' + command)
    finally:
        vim.call('setreg', '"', old_reg[0], old_reg[1])

    # Open folds
    vim.command('normal! zv')
Example #15
0
def set_default(vim: Nvim, var: str, val: typing.Any) -> typing.Any:
    return vim.call('denite#util#set_default', var, val)
Example #16
0
def relpath(vim: Nvim, path: str) -> str:
    return str(Path(vim.call('fnamemodify', expand(path), ':~:.')))
Example #17
0
def path2project(vim: Nvim, path: str, root_markers: typing.List[str]) -> str:
    return str(
        vim.call('denite#project#path2project_directory', path, root_markers))
Example #18
0
def debug(vim: Nvim, expr: typing.Any) -> None:
    if hasattr(vim, 'out_write'):
        vim.out_write(f'[deoplete] {expr}\n')
    else:
        vim.call('deoplete#util#print_debug', expr)
Example #19
0
def debug(vim: Nvim, expr: typing.Any) -> None:
    if hasattr(vim, 'out_write'):
        string = (expr if isinstance(expr, str) else str(expr))
        vim.out_write('[deoplete] ' + string + '\n')
    else:
        vim.call('deoplete#util#print_debug', expr)
Example #20
0
def get_syn_names(vim: Nvim) -> str:
    return str(vim.call('deoplete#util#get_syn_names'))
Example #21
0
def error(vim: Nvim, expr: typing.Any) -> None:
    """
    Prints the error messages to Vim/Nvim's :messages buffer.
    """
    vim.call('defx#util#print_error', expr)
Example #22
0
def fnamemodify(vim: Nvim, word: str, mod: str) -> str:
    return str(vim.call('fnamemodify', word, mod))
Example #23
0
def echo(vim: Nvim, color: str, string: str) -> None:
    vim.call('denite#util#echo', color, string)
Example #24
0
def get_syn_names(vim: Nvim):
    return vim.call('deoplete#util#get_syn_names')
Example #25
0
def error(vim: Nvim, expr: typing.Any) -> None:
    if hasattr(vim, 'err_write'):
        vim.err_write(f'[deoplete] {expr}\n')
    else:
        vim.call('deoplete#util#print_error', expr)
Example #26
0
def strwidth(vim: Nvim, word: str) -> int:
    return (int(vim.call('strwidth', word))
            if len(word) != len(bytes(word, 'utf-8', 'surrogatepass')) else
            len(word))
Example #27
0
def error(vim: Nvim, expr: typing.Any) -> None:
    if hasattr(vim, 'err_write'):
        string = (expr if isinstance(expr, str) else str(expr))
        vim.err_write('[deoplete] ' + string + '\n')
    else:
        vim.call('deoplete#util#print_error', expr)
Example #28
0
def get_syn_names(vim: Nvim) -> typing.List[str]:
    return list(vim.call('deoplete#util#get_syn_names'))
Example #29
0
def error(vim: Nvim, expr: typing.Any) -> None:
    string = (expr if isinstance(expr, str) else str(expr))
    vim.call('denite#util#print_error', string)
Example #30
0
def cd(vim: Nvim, path: str) -> None:
    vim.call('defx#util#cd', path)