Skip to content

Commit

Permalink
snip auto compile
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-jpq committed Jan 24, 2023
1 parent ebe7108 commit bd310c6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
98 changes: 50 additions & 48 deletions coq/server/registrants/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
from asyncio.tasks import as_completed
from contextlib import suppress
from dataclasses import dataclass
from datetime import datetime
from itertools import chain
from json import JSONDecodeError, dumps, loads
from math import inf
from os import linesep
from os.path import expanduser, expandvars
from pathlib import Path, PurePath
from posixpath import normcase
Expand All @@ -28,6 +26,7 @@
from pynvim_pp.lib import decode
from pynvim_pp.logging import log
from pynvim_pp.nvim import Nvim
from pynvim_pp.preview import set_preview
from std2.asyncio import to_thread
from std2.graphlib import recur_sort
from std2.pathlib import walk
Expand All @@ -46,8 +45,8 @@
from ...snippets.loaders.load import load_direct
from ...snippets.loaders.neosnippet import load_neosnippet
from ...snippets.parse import parse_basic
from ...snippets.parsers.types import ParseInfo
from ...snippets.types import SCHEMA, LoadedSnips, ParsedSnippet
from ...snippets.parsers.types import ParseError, ParseInfo
from ...snippets.types import SCHEMA, LoadedSnips, LoadError, ParsedSnippet
from ..rt_types import Stack

BUNDLED_PATH_TPL = Template("coq+snippets+${schema}.json")
Expand Down Expand Up @@ -210,9 +209,30 @@ def _trans(
yield snip, parsed, marks


async def _slurp(
stack: Stack, warn: AbstractSet[SnippetWarnings], worker: SnipWorker
async def _rolling_load(
worker: SnipWorker, cwd: PurePath, compiled: Mapping[Path, float]
) -> None:
for fut in as_completed(
tuple(_load_compiled(path, mtime) for path, mtime in compiled.items())
):
try:
path, mtime, loaded = await fut
except (OSError, JSONDecodeError, DecodeError) as e:
tpl = """
Failed to load compiled snips
${e}
""".rstrip()
log.warn("%s", Template(dedent(tpl)).substitute(e=type(e)))
else:
await worker.populate(path, mtime=mtime, loaded=loaded)
await Nvim.write(
LANG("fs snip load succ", path=fmt_path(cwd, path=path, is_dir=False))
)


async def slurp_compiled(
stack: Stack, warn: AbstractSet[SnippetWarnings], worker: SnipWorker
) -> Mapping[Path, float]:
with timeit("LOAD SNIPS"):
(
cwd,
Expand All @@ -228,61 +248,45 @@ async def _slurp(
worker.db_mtimes(),
)

stale = db_mtimes.keys() - (bundled.keys() | user_compiled.keys())
compiled = {
if stale := db_mtimes.keys() - (bundled.keys() | user_compiled.keys()):
await worker.clean(stale)

if needs_loading := {
path: mtime
for path, mtime in chain(bundled.items(), user_compiled.items())
if mtime > db_mtimes.get(path, -inf)
}
new_user_snips = {
fmt_path(cwd, path=path, is_dir=False): (
datetime.fromtimestamp(mtime).strftime(stack.settings.display.time_fmt),
datetime.fromtimestamp(prev).strftime(stack.settings.display.time_fmt)
if (prev := user_compiled_mtimes.get(path))
else "??",
)
}:
await _rolling_load(worker, cwd=cwd, compiled=needs_loading)

needs_compilation = {
path: mtime
for path, mtime in user_snips_mtimes.items()
if mtime > user_compiled_mtimes.get(path, -inf)
}

await worker.clean(stale)
if SnippetWarnings.missing in warn and not (bundled or user_compiled):
await Nvim.write(LANG("fs snip load empty"))

for fut in as_completed(
tuple(_load_compiled(path, mtime) for path, mtime in compiled.items())
):
try:
path, mtime, loaded = await fut
except (OSError, JSONDecodeError, DecodeError) as e:
tpl = """
Failed to load compiled snips
${e}
""".rstrip()
log.warn("%s", Template(dedent(tpl)).substitute(e=type(e)))
else:
await worker.populate(path, mtime=mtime, loaded=loaded)
await Nvim.write(
LANG(
"fs snip load succ", path=fmt_path(cwd, path=path, is_dir=False)
)
)

if SnippetWarnings.outdated in warn and new_user_snips:
paths = linesep.join(
f"{path} -- {prev} -> {cur}"
for path, (cur, prev) in new_user_snips.items()
)
await Nvim.write(LANG("fs snip needs compile", paths=paths))
return needs_compilation


@rpc()
async def _load_snips(stack: Stack) -> None:
for worker in stack.workers:
if isinstance(worker, SnipWorker):
await _slurp(
stack=stack, warn=stack.settings.clients.snippets.warn, worker=worker
)
try:
needs_compilation = await slurp_compiled(
stack=stack,
warn=stack.settings.clients.snippets.warn,
worker=worker,
)
if needs_compilation:
await compile_user_snippets(stack)
await slurp_compiled(stack, warn=frozenset(), worker=worker)
except (LoadError, ParseError) as e:
preview = str(e).splitlines()
await set_preview(syntax="", preview=preview)
await Nvim.write(LANG("snip parse fail"))
break


Expand Down Expand Up @@ -315,7 +319,7 @@ def compile_one(
return compiled


async def compile_user_snippets(stack: Stack, worker: SnipWorker) -> None:
async def compile_user_snippets(stack: Stack) -> None:
with timeit("COMPILE SNIPS"):
info = ParseInfo(visual="", clipboard="", comment_str=("", ""))
_, mtimes = await user_mtimes(
Expand Down Expand Up @@ -344,5 +348,3 @@ async def compile_user_snippets(stack: Stack, worker: SnipWorker) -> None:
)
except OSError as e:
await Nvim.write(e)
else:
await _slurp(stack=stack, warn={SnippetWarnings.missing}, worker=worker)
4 changes: 3 additions & 1 deletion coq/server/registrants/user_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Compiled,
compile_one,
compile_user_snippets,
slurp_compiled,
snippet_paths,
user_mtimes,
)
Expand Down Expand Up @@ -211,7 +212,8 @@ async def snips(stack: Stack, args: Sequence[str]) -> None:
if isinstance(worker, SnipWorker):
await Nvim.write(LANG("waiting..."))
try:
await compile_user_snippets(stack=stack, worker=worker)
await compile_user_snippets(stack)
await slurp_compiled(stack, warn=frozenset(), worker=worker)
except (LoadError, ParseError) as e:
preview = str(e).splitlines()
await set_preview(syntax="", preview=preview)
Expand Down
4 changes: 0 additions & 4 deletions locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
"fs snip load succ": |-
✅ Snippets updated -- ${path}
"fs snip needs compile": |-
⚠️ Snippets require manual compilation :: -- `:COQsnips compile`
${paths}
"begin T9 download": |-
⏳ Downloading T9 ...
Expand Down
4 changes: 0 additions & 4 deletions locale/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
"fs snip load succ": |-
✅ 代码片段已更新 —— ${path}
"fs snip needs compile": |-
⚠️ 代码片段须使用「:COQsnips compile」手动编译
${paths}
"begin T9 download": |-
⏳ 开始下载 T9……
Expand Down

0 comments on commit bd310c6

Please sign in to comment.