def inst(nvim: Nvim) -> Atomic: pkgs = { path: spec for path, spec in ((p_name(spec.git.uri), spec) for spec in pkg_specs) if path.exists() } atomic1 = Atomic() keymap = Keymap() for spec in pkgs.values(): for key in spec.keys: for lhs, rhs in key.maps.items(): attrgetter(key.modes)(keymap)(lhs, **asdict(key.opts)) << rhs for lhs, rhs in spec.vals.items(): atomic1.set_var(lhs, rhs) atomic2 = rtp_packages(nvim, plugins=pkgs) for spec in pkgs.values(): if spec.lua: lua = indent(spec.lua, " " * 2) lua_block = f"(function(){linesep}{lua}{linesep}end)()" atomic2.call_function("luaeval", (lua_block, ())) if spec.viml: atomic2.command(spec.viml) return atomic1 + keymap.drain(buf=None) + atomic2
def drain(nvim: Nvim) -> Tuple[Atomic, Sequence[RpcSpec]]: _atomic = Atomic() _atomic.call_function("setenv", ("PATH", PATH)) _atomic.call_function("setenv", ("PYTHONPATH", PYTHONPATH)) _atomic.set_var("mapleader", " ") _atomic.set_var("maplocalleader", " ") a0 = inst(nvim) a1 = settings.drain() a2, s0 = rpc.drain(nvim.channel_id) a3 = keymap.drain(buf=None) a4 = autocmd.drain() return _atomic + a0 + a1 + a2 + a3 + a4 + atomic, s0
def context( nvim: Nvim, db: BDB, options: MatchOptions, state: State, manual: bool ) -> Context: with Atomic() as (atomic, ns): ns.scr_col = atomic.call_function("screencol", ()) ns.buf = atomic.get_current_buf() ns.name = atomic.buf_get_name(0) ns.line_count = atomic.buf_line_count(0) ns.filetype = atomic.buf_get_option(0, "filetype") ns.commentstring = atomic.buf_get_option(0, "commentstring") ns.fileformat = atomic.buf_get_option(0, "fileformat") ns.tabstop = atomic.buf_get_option(0, "tabstop") ns.expandtab = atomic.buf_get_option(0, "expandtab") ns.cursor = atomic.win_get_cursor(0) atomic.commit(nvim) scr_col = ns.scr_col buf = cast(Buffer, ns.buf) (r, col) = cast(Tuple[int, int], ns.cursor) row = r - 1 pos = (row, col) buf_line_count = ns.line_count filename = normcase(cast(str, ns.name)) filetype = cast(str, ns.filetype) comment_str = cast(str, ns.commentstring) tabstop = ns.tabstop expandtab = cast(bool, ns.expandtab) linefeed = cast(Literal["\n", "\r", "\r\n"], LFfmt[cast(str, ns.fileformat)].value) lo = max(0, row - options.proximate_lines) hi = min(buf_line_count, row + options.proximate_lines + 1) lines = buf_get_lines(nvim, buf=buf, lo=lo, hi=hi) if DEBUG: db_line_count, db_lit = db.lines(buf.number, lo=lo, hi=hi) db_lines = tuple(db_lit) assert db_line_count in { buf_line_count - 1, buf_line_count, buf_line_count + 1, }, (db_line_count, buf_line_count) assert tuple( "" if idx == row else line for idx, line in enumerate(db_lines, start=lo) ) == tuple( "" if idx == row else line for idx, line in enumerate(lines, start=lo) ), linesep.join( unified_diff(lines, db_lines) ) r = row - lo line = lines[r] lines_before, lines_after = lines[:r], lines[r + 1 :] lhs, _, rhs = comment_str.partition("%s") b_line = encode(line) before, after = decode(b_line[:col]), decode(b_line[col:]) split = gen_split(lhs=before, rhs=after, unifying_chars=options.unifying_chars) ctx = Context( manual=manual, change_id=state.change_id, commit_id=state.commit_id, cwd=state.cwd, buf_id=buf.number, filename=filename, filetype=filetype, line_count=buf_line_count, linefeed=linefeed, tabstop=tabstop, expandtab=expandtab, comment=(lhs, rhs), position=pos, scr_col=scr_col, line=split.lhs + split.rhs, line_before=split.lhs, line_after=split.rhs, lines=lines, lines_before=lines_before, lines_after=lines_after, words=split.word_lhs + split.word_rhs, words_before=split.word_lhs, words_after=split.word_rhs, syms=split.syms_lhs + split.syms_rhs, syms_before=split.syms_lhs, syms_after=split.syms_rhs, ws_before=split.ws_lhs, ws_after=split.ws_rhs, ) return ctx
from typing import Sequence, Tuple from pynvim import Nvim from pynvim_pp.atomic import Atomic from pynvim_pp.autocmd import AutoCMD from pynvim_pp.keymap import Keymap from pynvim_pp.rpc import RPC, RpcSpec from pynvim_pp.settings import Settings from .components.localization import load from .components.rtp import inst from .consts import PATH, PYTHONPATH LANG = load(code=None) atomic = Atomic() autocmd = AutoCMD() keymap = Keymap() rpc = RPC() settings = Settings() def drain(nvim: Nvim) -> Tuple[Atomic, Sequence[RpcSpec]]: _atomic = Atomic() _atomic.call_function("setenv", ("PATH", PATH)) _atomic.call_function("setenv", ("PYTHONPATH", PYTHONPATH)) _atomic.set_var("mapleader", " ") _atomic.set_var("maplocalleader", " ") a0 = inst(nvim) a1 = settings.drain() a2, s0 = rpc.drain(nvim.channel_id)
def redraw(nvim: Nvim, state: State, focus: Optional[str]) -> None: derived, current = state.derived, state.current focus_row = derived.path_row_lookup.get(focus) if focus else None current_row = derived.path_row_lookup.get(current or "") cwin = cur_win(nvim) ns = nvim.api.create_namespace(FM_NAMESPACE) for win, buf in find_fm_windows(nvim): p_count = buf_line_count(nvim, buf=buf) n_count = len(state.derived.lines) row, col = win_get_cursor(nvim, win=win) (r1, c1), (r2, c2) = operator_marks(nvim, buf=buf, visual_type=None) if focus_row is not None: new_row: Optional[int] = focus_row + 1 elif win != cwin and current_row is not None: new_row = current_row + 1 elif row >= n_count: new_row = n_count elif p_count != n_count: new_row = row + 1 else: new_row = None a1 = Atomic() a1.buf_set_option(buf, "modifiable", True) a2 = _update(nvim, buf=buf, ns=ns, derived=derived) a3 = Atomic() a3.buf_set_option(buf, "modifiable", False) a3.call_function("setpos", ("'<", (buf.number, r1 + 1, c1 + 1, 0))) a3.call_function("setpos", ("'>", (buf.number, r2 + 1, c2 + 1, 0))) if new_row is not None: a3.win_set_cursor(win, (new_row, col)) (a1 + a2 + a3).commit(nvim)
def _update(nvim: Nvim, buf: Buffer, ns: int, derived: Derived) -> Atomic: n_hash = derived.hashed try: p_hash: Sequence[str] = decode( Sequence[str], buf_get_var(nvim, buf=buf, key=_FM_HASH_VAR)) except DecodeError: p_hash = ("", ) atomic = Atomic() for (i1, i2), (j1, j2) in trans_inplace(src=p_hash, dest=n_hash, unifying=10): atomic.buf_clear_namespace(buf, ns, i1, i2) atomic.buf_set_lines(buf, i1, i2, True, derived.lines[j1:j2]) for idx, highlights in enumerate(derived.highlights[j1:j2], start=i1): for hl in highlights: atomic.buf_add_highlight(buf, ns, hl.group, idx, hl.begin, hl.end) for idx, badges in enumerate(derived.badges[j1:j2], start=i1): vtxt = tuple((bdg.text, bdg.group) for bdg in badges) atomic.buf_set_virtual_text(buf, ns, idx, vtxt, {}) atomic.buf_set_var(buf, _FM_HASH_VAR, n_hash) return atomic
def redraw(nvim: Nvim, state: State, focus: Optional[PurePath]) -> None: focus_row = state.derived.path_row_lookup.get(focus) if focus else None ns = nvim.api.create_namespace(FM_NAMESPACE) use_extmarks = nvim.funcs.has("nvim-0.6") for win, buf in find_fm_windows(nvim): p_count = buf_line_count(nvim, buf=buf) n_count = len(state.derived.lines) row, col = win_get_cursor(nvim, win=win) (r1, c1), (r2, c2) = operator_marks(nvim, buf=buf, visual_type=None) try: hashed_lines = _DECODER( buf_get_var(nvim, buf=buf, key=_FM_HASH_VAR)) except DecodeError: hashed_lines = ("", ) if focus_row is not None: new_row: Optional[int] = focus_row + 1 elif row >= n_count: new_row = n_count elif p_count != n_count: new_row = row + 1 else: new_row = None a1 = Atomic() a1.buf_set_option(buf, "modifiable", True) a2 = _update( use_extmarks, buf=buf, ns=ns, derived=state.derived, hashed_lines=hashed_lines, ) a3 = Atomic() a3.buf_set_option(buf, "modifiable", False) a3.call_function("setpos", ("'<", (buf.number, r1 + 1, c1 + 1, 0))) a3.call_function("setpos", ("'>", (buf.number, r2 + 1, c2 + 1, 0))) if new_row is not None: a3.win_set_cursor(win, (new_row, col)) try: (a1 + a2 + a3).commit(nvim) except NvimError as e: raise UnrecoverableError(e)
def _update( use_extmarks: bool, buf: Buffer, ns: int, derived: Derived, hashed_lines: Sequence[str], ) -> Atomic: atomic = Atomic() for (i1, i2), (j1, j2) in trans_inplace(src=hashed_lines, dest=derived.hashed, unifying=10): atomic.buf_clear_namespace(buf, ns, i1, i2) atomic.buf_set_lines(buf, i1, i2, True, derived.lines[j1:j2]) for idx, highlights in enumerate(derived.highlights[j1:j2], start=i1): for hl in highlights: atomic.buf_add_highlight(buf, ns, hl.group, idx, hl.begin, hl.end) for idx, badges in enumerate(derived.badges[j1:j2], start=i1): vtxt = tuple((bdg.text, bdg.group) for bdg in badges) if use_extmarks: atomic.buf_set_extmark(buf, ns, idx, -1, { "virt_text": vtxt, "hl_mode": "combine" }) else: atomic.buf_set_virtual_text(buf, ns, idx, vtxt, {}) atomic.buf_set_var(buf, _FM_HASH_VAR, derived.hashed) return atomic
def inst(nvim: Nvim) -> Atomic: pkgs = { path: spec for path, spec in ((p_name(spec.git.uri), spec) for spec in pkg_specs) if path.exists() } atomic1 = Atomic() keymap = Keymap() for spec in pkgs.values(): for key in spec.keys: for lhs, rhs in key.maps.items(): attrgetter(key.modes)(keymap)(lhs, **asdict(key.opts)) << rhs for lhs, rhs in spec.vals.items(): atomic1.set_var(lhs, rhs) atomic2 = Atomic() atomic2.command("packloadall") for spec in pkgs.values(): if spec.lua: body = indent(spec.lua, " " * 2) lua = f""" (function() local _, err = pcall(function() {body} end) if err then vim.api.nvim_err_writeln(err) end end)() """ atomic2.exec_lua(dedent(lua), ()) if spec.viml: lua = f""" (function(viml) local _, err = pcall(vim.cmd, viml) if err then vim.api.nvim_err_writeln(err) end end)(...) """ atomic2.exec_lua(dedent(lua), (spec.viml, )) return atomic1 + keymap.drain(buf=None) + atomic2