Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / 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): the first check of a
## package set checks its packages and keeps them (about 90 ms), each later one
## takes about 0.3 ms. A package set with registry packages is synced once
## (`luc sync`, in the background) before its first check.
##
## 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.
from luce_std.process import Command
from luce_std import files, paths
from luce_geocore.geocore import ScriptHelp, CodeCheck
from script_build import plan, tool
from script_packages import editor_packages, manifest_dependencies
import time

pub class ScriptChecker:
    ## 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 = ""
    ## `luc sync` of a new package set with registry packages, and the text
    ## that waits for it.
    var syncing: Command? = none
    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 it took (s).
    pub var text: str = ""
    pub var failure: str = ""
    pub var seconds: float = 0.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: the path of its script file. Completion
    ## and hover read it too.
    pub func prepare(self, packages: str, lock: str, folder: 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.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.
        self.stop()
        if not synced(made.work, made.manifest):
            self.syncing = Command(tool("luc"), ["sync"], directory = made.work, output_limit = 1048576)
        return self.file

    ## Check `code` with `packages` and `lock` for a project in `folder`: now,
    ## or once a new package set is synced.
    pub func start(self, code: str, packages: str, lock: str, folder: str) -> unit!:
        _ = self.prepare(packages, lock, folder)
        self.text = code
        if self.syncing != none:
            self.waiting = code
            return
        self.check(code)

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

    ## Whether a check has finished since the last call: its result is in
    ## `failure`.
    pub func poll(self) -> bool!:
        if let command = self.syncing:
            if not command.is_finished():
                return false
            let code = command.exit_code() else -1
            command.close()
            self.syncing = none
            if code != 0:
                # Offline, or a package that cannot be found: the build says
                # which; the text goes on.
                self.failure = ""
                self.seconds = 0.0
                self.finished = true
            else:
                self.check(self.waiting)
        if not self.finished:
            return false
        self.finished = false
        return true

    ## Whether a check is waiting for its packages.
    pub func running(self) -> bool:
        return self.syncing != none

    ## Stop a sync still running (its process, by PID); its text is not checked.
    pub func stop(self):
        if let command = self.syncing:
            command.cancel()
            command.close()
            # The set is synced again at its next check.
            self.prepared = ""
        self.syncing = none
        self.finished = false

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

## The first error a check found in the code, as "line L, column C: message";
## "" when there is none (another package's error is the build's to report).
func first_error(checked: CodeCheck) -> str!:
    for at in 0..<checked.diagnostic_count():
        if checked.diagnostic_severity(at) == 0 and checked.diagnostic_part(at) == 0:
            return f"line {checked.diagnostic_line(at)}, column {checked.diagnostic_column(at)}: {checked.diagnostic_message(at)}"
    return ""