Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / script_check.luc

## Checking a Script node's code while it is typed, and its completion and
## hover (Houdini has none of these; an IDE does). The node's packages make a
## check package, one per package set, apart from the build projects so a
## running build never sees it: its package.prisma names the same dependencies
## a build of the node takes (script_build.plan), and its src/script.lucb is the
## text. The editor's own checker checks that file in process (luce-geocore's
## ScriptHelp, over luce-base's embed.Library.check_file).
##
## A new package set is warmed off the UI thread: its registry packages synced
## once into .luc/deps by a child process (`luc lock` when the set has no lock
## yet, as `luc build` would, else `luc sync`), then checked once on a thread of
## its own (ScriptCheckJob), which reads and keeps its packages
## (about 0.1 s). Both wake the editor's loop when they end (`waker`, a
## notifier's), which then polls; nothing looks on a timer. Until then the set is not `ready`: a pause's check waits
## for it, and completion and hover answer nothing (the editor asks again once
## it is ready). Once warm, a check takes about 0.4 ms and runs at once.
## Checks run one at a time in the process (luce-kernel's check lock), so
## nothing here asks for one while a job runs.
##
## An error underlines its line at once and the text waits; a clean check sends
## the text to the node, which builds and cooks it. A check never passes when it
## could not check: a sync that failed, or a package the script imports that
## cannot be found or does not check, is its failure (with no line: the text goes
## on, and the build says the same on the node).
from luce_std.process import Command
from luce_std.wake import Waker
from luce_std import files, paths
from luce_geocore.geocore import ScriptHelp, ScriptCheckJob, CodeCheck
from script_build import plan, tool
from script_packages import editor_packages, manifest_dependencies, is_package, deps_folder
import time

pub class ScriptChecker:
    ## Woken from the sync's and the check's threads when they end (the
    ## editor's loop then polls); none: the owner polls on its own.
    pub var waker: Waker? = none
    ## The packages the editor was built with (found once).
    var editor: map[str, str]? = none
    ## The check package of the last package set (its packages, lock and
    ## project folder as `prepared`) and its script file.
    var prepared: str = ""
    var project: str = ""
    pub var file: str = ""
    ## The set's warming: its `luc lock` or `luc sync`, then its first check on
    ## a thread; `warm` once that check finished (or the sync failed).
    var syncing: Command? = none
    var job: ScriptCheckJob? = none
    var warm: bool = false
    ## What luc said when the set's sync failed (offline, or a package that
    ## cannot be found), "" when it did not: every check of the set fails with it.
    var unsynced: str = ""
    ## Jobs of sets left behind, released once they finish (a running check
    ## cannot be stopped, and joining it would hold up the editor).
    var leaving: list[ScriptCheckJob] = []
    ## A pause's text that waits for the set to be warm.
    var pending: bool = false
    var waiting: str = ""
    ## Whether a check finished that `poll` has not reported yet.
    var finished: bool = false
    ## The text the last check read, its failure ("" clean: "line L, column
    ## C: message" for an error in the code) and how long the editor waited
    ## for it (s); `warmed_in` how long the set's first check took on its
    ## thread.
    pub var text: str = ""
    pub var failure: str = ""
    pub var seconds: float = 0.0
    pub var warmed_in: float = 0.0
    var warm_started: int = 0

    pub func init(self):
        self.finished = false

    ## The check package for `packages` and `lock` of a project in `folder`,
    ## written when the set changed, and its warming started with `code`: the
    ## path of its script file. Completion and hover read it too.
    pub func prepare(self, packages: str, lock: str, folder: str, code: str) -> str!:
        let key = f"{packages}\n{lock}\n{folder}"
        if key == self.prepared and self.file != "":
            return self.file
        let editor = self.editor else editor_packages()
        self.editor = editor
        let made = plan(-1, "", packages, lock, folder, "", editor, checking = true)
        made.write()
        self.stop()
        self.prepared = key
        self.project = made.work
        self.file = paths.join(paths.join(made.work, "src"), "script.lucb")
        # Registry packages are synced into the project's .luc/deps once:
        # resolved first when the set has no lock (`luc sync` needs one).
        if not synced(made.work, made.manifest):
            let locked = files.exists(paths.join(made.work, "luc.lock"))
            self.syncing = Command(tool("luc"), ["sync" if locked else "lock"], directory = made.work, output_limit = 1048576, waker = self.waker)
            self.waiting = code
        else:
            self.warm_up(code)
        return self.file

    ## Check `code` with `packages` and `lock` for a project in `folder`: now
    ## when the set is warm, else once it is.
    pub func start(self, code: str, packages: str, lock: str, folder: str) -> unit!:
        _ = self.prepare(packages, lock, folder, code)
        self.text = code
        if self.ready():
            self.check(code)
            return
        self.waiting = code
        self.pending = true

    ## Whether a sync or a check runs (or a left set's job), which `poll`
    ## must look at again.
    pub func running(self) -> bool:
        return self.syncing != none or self.job != none or self.leaving.length > 0

    ## Whether the set is warm and nothing runs: a check, a completion or a
    ## hover now takes a fraction of a millisecond.
    pub func ready(self) -> bool:
        return self.warm and self.syncing == none and self.job == none

    ## Check `code` in the check package now (the set is warm).
    func check(self, code: str) -> unit!:
        let started = time.now()
        self.failure = self.unsynced
        if self.unsynced == "":
            self.failure = first_error(ScriptHelp.check(self.file, code))
        self.seconds = float(time.now() - started) / 1000000000.0
        self.finished = true

    ## The set's first check, of `code`, on a thread of its own.
    func warm_up(self, code: str) -> unit!:
        self.warm_started = time.now()
        self.job = ScriptCheckJob.start(self.file, code, self.waker)

    ## Whether a check has finished since the last call: its result is in
    ## `failure`. Polled when the loop runs (the waker runs it when the sync
    ## or the job ends): it moves the set's warming along.
    pub func poll(self) -> bool!:
        self.release_left()
        if let command = self.syncing:
            if not command.is_finished():
                return false
            let code = command.exit_code() else -1
            let said = command.output() if code != 0 else ""
            command.close()
            self.syncing = none
            if code != 0:
                # Offline, or a package that cannot be found: each check
                # of the set says so.
                self.unsynced = sync_failure(said)
                self.warm = true
            else:
                self.warm_up(self.waiting)
        if let job = self.job:
            if not job.finished():
                return false
            job.close()
            self.job = none
            self.warm = true
            self.warmed_in = float(time.now() - self.warm_started) / 1000000000.0
        if self.pending and self.ready():
            self.pending = false
            self.check(self.waiting)
        if not self.finished:
            return false
        self.finished = false
        return true

    ## Stop warming the set: its sync (its process, by PID) is cancelled, its
    ## job left to finish on its own; a waiting text is not checked.
    pub func stop(self):
        if let command = self.syncing:
            command.cancel()
            command.close()
        self.syncing = none
        if let job = self.job:
            self.leaving.append(job)
        self.job = none
        # The set is prepared (and warmed) again at its next check.
        self.prepared = ""
        self.warm = false
        self.unsynced = ""
        self.pending = false
        self.finished = false

    ## Give back the jobs of sets left behind that have finished.
    func release_left(self) -> unit!:
        if self.leaving.length == 0:
            return
        let running: list[ScriptCheckJob] = []
        for job in self.leaving:
            if job.finished():
                job.close()
            else:
                running.append(job)
        self.leaving = running

    ## Stop, and wait for every job (the editor is closing).
    pub func close(self):
        self.stop()
        for job in self.leaving:
            job.close()
        self.leaving = []

## Whether every registry dependency of `manifest` is in `project`'s .luc/deps
## (a checkout dependency is read where it is; one whose checkout is not there
## comes from the registry, as the compilers take it).
func synced(project: str, manifest: str) -> bool!:
    for d in manifest_dependencies(manifest):
        if d.path != "" and is_package(d.path):
            continue
        let kind = files.metadata(paths.join(deps_folder(project, d.name), "package.prisma")).kind catch failure:
            return false
        if kind != files.FileKind.regular:
            return false
    return true

## The failure of a set whose sync failed: luc's last line that says
## something (its error), after what it was doing.
func sync_failure(said: str) -> str:
    var last = ""
    for line in said.split("\n"):
        if line.trim() != "":
            last = line.trim()
    if last == "":
        return "the script's packages could not be synced"
    return f"the script's packages could not be synced: {last}"

## The first error a check found in the code, as "line L, column C: message";
## else the first error elsewhere (an import that cannot be found, a package
## that does not check), as "packages: message", with no line; "" when the
## check passed. A check that did not pass always says something.
func first_error(checked: CodeCheck) -> str!:
    var elsewhere = ""
    for at in 0..<checked.diagnostic_count():
        if checked.diagnostic_severity(at) != 0:
            continue
        if checked.diagnostic_part(at) == 0:
            return f"line {checked.diagnostic_line(at)}, column {checked.diagnostic_column(at)}: {checked.diagnostic_message(at)}"
        if elsewhere == "":
            elsewhere = f"packages: {checked.diagnostic_message(at)}"
    if elsewhere != "":
        return elsewhere
    if not checked.ok():
        return "packages: the script did not check, and the checker said nothing"
    return ""