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))
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))
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)
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
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)
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 ''
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)
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
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()
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
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')
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
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
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')
def set_default(vim: Nvim, var: str, val: typing.Any) -> typing.Any: return vim.call('denite#util#set_default', var, val)
def relpath(vim: Nvim, path: str) -> str: return str(Path(vim.call('fnamemodify', expand(path), ':~:.')))
def path2project(vim: Nvim, path: str, root_markers: typing.List[str]) -> str: return str( vim.call('denite#project#path2project_directory', path, root_markers))
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)
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)
def get_syn_names(vim: Nvim) -> str: return str(vim.call('deoplete#util#get_syn_names'))
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)
def fnamemodify(vim: Nvim, word: str, mod: str) -> str: return str(vim.call('fnamemodify', word, mod))
def echo(vim: Nvim, color: str, string: str) -> None: vim.call('denite#util#echo', color, string)
def get_syn_names(vim: Nvim): return vim.call('deoplete#util#get_syn_names')
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)
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))
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)
def get_syn_names(vim: Nvim) -> typing.List[str]: return list(vim.call('deoplete#util#get_syn_names'))
def error(vim: Nvim, expr: typing.Any) -> None: string = (expr if isinstance(expr, str) else str(expr)) vim.call('denite#util#print_error', string)
def cd(vim: Nvim, path: str) -> None: vim.call('defx#util#cd', path)