Skip to content

Commit

Permalink
render
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-jpq committed Sep 2, 2024
1 parent b61d043 commit c144514
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 83 deletions.
70 changes: 44 additions & 26 deletions chadtree/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from concurrent.futures import Future, ThreadPoolExecutor
from contextlib import AbstractAsyncContextManager, suppress
from dataclasses import replace
from functools import wraps
from logging import DEBUG as DEBUG_LVL
from logging import INFO
Expand All @@ -36,7 +37,7 @@
ServerAddr,
)
from pynvim_pp.types import NoneType
from std2.asyncio import cancel
from std2.asyncio import Cancellation, cancel
from std2.cell import RefCell
from std2.contextlib import nullacontext
from std2.pickle.types import DecodeError
Expand All @@ -61,6 +62,8 @@

_CB = RPCallable[Optional[Stage]]

_die = Cancellation()


def _autodie(ppid: int) -> AbstractAsyncContextManager:
if os is OS.windows:
Expand Down Expand Up @@ -123,7 +126,7 @@ async def _go(loop: AbstractEventLoop, client: RPClient) -> None:
else:
hl = highlight(*settings.view.hl_context.groups)
await (atomic + autocmd.drain() + hl).commit(NoneType)
state = RefCell(await initial_state(settings, th=th))
state_ref = RefCell(await initial_state(settings, th=th))

init_locale(settings.lang)
with suppress_and_log():
Expand All @@ -141,8 +144,8 @@ async def step(method: Method, params: Sequence[Any]) -> None:
if handler := cast(Optional[_CB], handlers.get(method)):
with suppress_and_log():
async with lock:
if stage := await handler(state.val, *params):
state.val = stage.state
if stage := await handler(state_ref.val, *params):
state_ref.val = stage.state
staged.val = stage
event.set()
else:
Expand Down Expand Up @@ -177,30 +180,45 @@ async def c1() -> None:
async def c2() -> None:
t1, has_drawn = monotonic(), False

@_die
async def cont() -> None:
nonlocal has_drawn
with suppress_and_log():
if stage := staged.val:
state = stage.state

for _ in range(RENDER_RETRIES - 1):
with suppress(NvimError):
derived = await redraw(state, focus=stage.focus)
next_state = replace(
state, node_row_lookup=derived.node_row_lookup
)
break
else:
try:
derived = await redraw(state, focus=stage.focus)
next_state = replace(
state, node_row_lookup=derived.node_row_lookup
)
except NvimError as e:
log.warn("%s", e)
next_state = state

state_ref.val = next_state

if settings.profiling and not has_drawn:
has_drawn = True
await _profile(t1=t1)

while True:
await event.wait()
with suppress_and_log():
try:
if stage := staged.val:
state = stage.state

for _ in range(RENDER_RETRIES - 1):
with suppress(NvimError):
await redraw(state, focus=stage.focus)
break
else:
try:
await redraw(state, focus=stage.focus)
except NvimError as e:
log.warn("%s", e)

if settings.profiling and not has_drawn:
has_drawn = True
await _profile(t1=t1)
finally:
event.clear()

await gather(c1(), c2(), _sched(state))
try:

create_task(cont())
finally:
event.clear()

await gather(c1(), c2(), _sched(state_ref))


async def init(socket: ServerAddr, ppid: int, th: ThreadPoolExecutor) -> None:
Expand Down
25 changes: 0 additions & 25 deletions chadtree/state/cache.py

This file was deleted.

6 changes: 4 additions & 2 deletions chadtree/state/load.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from asyncio import gather
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from uuid import uuid4

from pynvim_pp.nvim import Nvim

Expand All @@ -9,7 +10,6 @@
from ..nvim.markers import markers
from ..settings.types import Settings
from ..version_ctl.types import VCStatus
from .cache import DeepState
from .executor import AsyncExecutor
from .ops import load_session
from .types import Selection, Session, State
Expand Down Expand Up @@ -48,7 +48,8 @@ async def initial(settings: Settings, th: ThreadPoolExecutor) -> State:
current = None
filter_pattern = None

state = DeepState(
state = State(
id=uuid4(),
executor=executor,
settings=settings,
session=session,
Expand All @@ -68,5 +69,6 @@ async def initial(settings: Settings, th: ThreadPoolExecutor) -> State:
vc=vc,
current=current,
window_order={},
node_row_lookup=(),
)
return state
6 changes: 4 additions & 2 deletions chadtree/state/next.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import PurePath
from typing import AbstractSet, Mapping, Optional, Union, cast
from uuid import uuid4

from pynvim_pp.rpc_types import ExtData
from std2.types import Void, VoidType, or_else
Expand All @@ -8,7 +9,6 @@
from ..fs.types import Node
from ..nvim.types import Markers
from ..version_ctl.types import VCStatus
from .cache import DeepState
from .types import Diagnostics, FilterPattern, Index, Selection, Session, State


Expand Down Expand Up @@ -60,7 +60,8 @@ async def forward(
new_hidden = or_else(show_hidden, state.show_hidden)
new_vim_focus = or_else(vim_focus, state.vim_focus)

new_state = DeepState(
new_state = State(
id=uuid4(),
executor=state.executor,
settings=state.settings,
session=or_else(session, state.session),
Expand All @@ -80,6 +81,7 @@ async def forward(
vc=new_vc,
current=new_current,
window_order=or_else(window_order, state.window_order),
node_row_lookup=state.node_row_lookup,
)

return new_state
10 changes: 4 additions & 6 deletions chadtree/state/types.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from dataclasses import dataclass
from pathlib import Path, PurePath
from typing import AbstractSet, Mapping, Optional
from typing import AbstractSet, Mapping, Optional, Sequence
from uuid import UUID

from pynvim_pp.rpc_types import ExtData

from ..fs.types import Node
from ..nvim.types import Markers
from ..settings.types import Settings
from ..version_ctl.types import VCStatus
from ..view.types import Derived
from .executor import AsyncExecutor

Index = AbstractSet[PurePath]
Expand All @@ -29,6 +29,7 @@ class Session:

@dataclass(frozen=True)
class State:
id: UUID
executor: AsyncExecutor
settings: Settings
session: Session
Expand All @@ -48,10 +49,7 @@ class State:
width: int
diagnostics: Diagnostics
window_order: Mapping[ExtData, None]

@property
def derived(self) -> Derived:
raise NotImplementedError()
node_row_lookup: Sequence[Node]


@dataclass(frozen=True)
Expand Down
33 changes: 25 additions & 8 deletions chadtree/transitions/redraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@

from ..consts import URI_SCHEME
from ..state.types import State
from ..view.render import render
from ..view.types import Derived
from .shared.wm import find_fm_windows


class UnrecoverableError(Exception):
...
class UnrecoverableError(Exception): ...


_NS = uuid4()


_DECODER = new_decoder[Sequence[str]](Sequence[str])
_HOME = Path.home()


async def _derived(state: State) -> Derived:
return await render(
state.root,
settings=state.settings,
index=state.index,
selection=state.selection,
filter_pattern=state.filter_pattern,
markers=state.markers,
diagnostics=state.diagnostics,
vc=state.vc,
follow_links=state.follow_links,
show_hidden=state.show_hidden,
current=state.current,
)


def _buf_name(root: PurePath) -> str:
try:
rel = root.relative_to(_HOME)
Expand Down Expand Up @@ -72,8 +86,9 @@ def _update(
return atomic


async def redraw(state: State, focus: Optional[PurePath]) -> None:
focus_row = state.derived.path_row_lookup.get(focus) if focus else None
async def redraw(state: State, focus: Optional[PurePath]) -> Derived:
derived = await _derived(state)
focus_row = derived.path_row_lookup.get(focus) if focus else None
buf_name = _buf_name(state.root.path)

ns = await Nvim.create_namespace(_NS)
Expand All @@ -82,7 +97,7 @@ async def redraw(state: State, focus: Optional[PurePath]) -> None:
async for win, buf in find_fm_windows():
is_fm_win = await win.vars.get(bool, URI_SCHEME)
p_count = await buf.line_count()
n_count = len(state.derived.lines)
n_count = len(derived.lines)
row, col = await win.get_cursor()
(r1, c1), (r2, c2) = await operator_marks(buf, visual_type=None)
buf_var = await buf.vars.get(NoneType, str(_NS))
Expand All @@ -108,7 +123,7 @@ async def redraw(state: State, focus: Optional[PurePath]) -> None:
use_extmarks,
buf=buf,
ns=ns,
derived=state.derived,
derived=derived,
hashed_lines=hashed_lines,
)

Expand Down Expand Up @@ -143,3 +158,5 @@ async def redraw(state: State, focus: Optional[PurePath]) -> None:
await a4.commit(NoneType)
except NvimError as e:
raise UnrecoverableError(e)

return derived
4 changes: 2 additions & 2 deletions chadtree/transitions/shared/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


def _row_index(state: State, row: int) -> Optional[Node]:
if (row >= 0) and (row < len(state.derived.node_row_lookup)):
return state.derived.node_row_lookup[row]
if (row >= 0) and (row < len(state.node_row_lookup)):
return state.node_row_lookup[row]
else:
return None

Expand Down
Loading

0 comments on commit c144514

Please sign in to comment.