def eval_snips( nvim: Nvim, stack: Stack, visual: bool, maybe_grammar: str = REPL_GRAMMAR, ) -> None: try: grammar = SnippetGrammar[maybe_grammar] except KeyError: grammar = SnippetGrammar.lsp log.warn("%s", "bad snippet grammar -- reverting to LSP") win = cur_win(nvim) buf = win_get_buf(nvim, win=win) line_count = buf_line_count(nvim, buf=buf) path = PurePath(normcase(buf_name(nvim, buf=buf))) comment_str = buf_commentstr(nvim, buf=buf) clipboard = nvim.funcs.getreg() info = ParseInfo(visual="", clipboard=clipboard, comment_str=comment_str) if visual: (lo, _), (hi, _) = operator_marks(nvim, buf=buf, visual_type=None) hi = min(line_count, hi + 1) else: lo, hi = 0, line_count lines = buf_get_lines(nvim, buf=buf, lo=lo, hi=hi) try: compiled = compile_one( stack, grammar=grammar, path=path, info=info, lines=enumerate(lines, start=lo + 1), ) except (LoadError, ParseError) as e: preview = str(e).splitlines() with hold_win_pos(nvim, win=win): set_preview(nvim, syntax="", preview=preview) write(nvim, LANG("snip parse fail")) else: preview = _pprn(compiled).splitlines() with hold_win_pos(nvim, win=win): set_preview(nvim, syntax="yaml", preview=preview) if preview: write(nvim, LANG("snip parse succ")) else: write(nvim, LANG("no snippets found"))
def kill_buffers( nvim: Nvim, last_used: Mapping[int, None], paths: AbstractSet[PurePath], reopen: Mapping[PurePath, PurePath], ) -> None: active = ({ win_get_buf(nvim, win=win): win for win in find_non_fm_windows_in_tab( nvim, last_used=last_used, ) } if reopen else {}) for buf in list_bufs(nvim, listed=True): name = PurePath(buf_name(nvim, buf=buf)) buf_paths = ancestors(name) | {name} if not buf_paths.isdisjoint(paths): win = active.get(buf) new_path = reopen.get(name) if reopen and win and new_path: p = Path(name) p.touch() with hold_win_pos(nvim): set_cur_win(nvim, win=win) escaped = nvim.funcs.fnameescape(normcase(new_path)) nvim.command(f"edit! {escaped}") p.unlink(missing_ok=True) buf_close(nvim, buf=buf)
def _show_file(nvim: Nvim, *, state: State, settings: Settings, click_type: ClickType) -> None: if click_type is ClickType.tertiary: nvim.api.command("tabnew") win = cur_win(nvim) for key, val in settings.win_actual_opts.items(): win_set_option(nvim, win=win, key=key, val=val) path = state.current if path: hold = click_type is ClickType.secondary mgr = hold_win_pos(nvim) if hold else nullcontext() with mgr: non_fm_windows = tuple( find_non_fm_windows_in_tab(nvim, last_used=state.window_order)) buf = next(find_buffers_with_file(nvim, file=path), None) win = next( chain( find_window_with_file_in_tab( nvim, last_used=state.window_order, file=path), non_fm_windows, ), None, ) or new_window( nvim, last_used=state.window_order, win_local=settings.win_actual_opts, open_left=not settings.open_left, width=None if len(non_fm_windows) else nvim.options["columns"] - state.width - 1, ) set_cur_win(nvim, win=win) non_fm_count = len(non_fm_windows) if click_type is ClickType.v_split and non_fm_count: nvim.api.command("vnew") temp_buf = cur_buf(nvim) buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe") elif click_type is ClickType.h_split and non_fm_count: nvim.api.command("new") temp_buf = cur_buf(nvim) buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe") win = cur_win(nvim) if buf is None: escaped = nvim.funcs.fnameescape(normcase(path)) nvim.command(f"edit! {escaped}") else: win_set_buf(nvim, win=win, buf=buf) resize_fm_windows(nvim, last_used=state.window_order, width=state.width) nvim.api.command("filetype detect")
def _show_file( nvim: Nvim, *, state: State, settings: Settings, click_type: ClickType ) -> None: if click_type is ClickType.tertiary: nvim.api.command("tabnew") path = state.current if path: hold = click_type is ClickType.secondary mgr = hold_win_pos(nvim) if hold else nullcontext() with mgr: non_fm_windows = tuple(find_non_fm_windows_in_tab(nvim)) buf = next(find_buffers_with_file(nvim, file=path), None) win = next( chain( find_window_with_file_in_tab(nvim, file=path), (win for win in non_fm_windows if _win_nochange(nvim, win=win)), ), None, ) or new_window( nvim, open_left=not settings.open_left, width=None if len(non_fm_windows) else nvim.options["columns"] - state.width - 1, ) set_cur_win(nvim, win=win) non_fm_count = len(non_fm_windows) if click_type is ClickType.v_split and non_fm_count: nvim.api.command("vnew") temp_buf = cur_buf(nvim) buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe") elif click_type is ClickType.h_split and non_fm_count: nvim.api.command("new") temp_buf = cur_buf(nvim) buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe") win = cur_win(nvim) if buf is None: nvim.command(f"edit {path}") else: win_set_buf(nvim, win=win, buf=buf) resize_fm_windows(nvim, state.width) nvim.api.command("filetype detect")
def cont() -> None: with hold_win_pos(nvim): set_preview(nvim, preview=text)