summaryrefslogtreecommitdiff
path: root/test/hxcases/hx_indent.py
blob: c85c94170bce35748025e4c6ee361c820111f794 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
"""The goldens of indent.jsonl from helix itself, in a pty: $HX (else `hx`
on PATH) with its grammars and queries ($HELIX_RUNTIME) and its defaults
(auto-pairs on), LSP and auto-format off. They were made by the reference
helix (docs/helix-keys.md), 278b24389's `hx` built from the genizah checkout
with HELIX_RUNTIME its runtime/. Each case's file is named by its "path" (its
extension picks the language) and holds its "text"; the keys are typed, then
a marker where the cursor is, then `:wq!`. The marker's place is the cursor,
and the anchor: every case ends in insert mode with one cursor no wider than
helix's block (no `a` or `c` over a selection, whose anchor stays behind).

    HX=... HELIX_RUNTIME=... python3 -B test/hxcases/hx_indent.py \
        test/hxcases/indent.jsonl > test/hxcases/indent-goldens.jsonl
"""
import json, os, pty, select, struct, sys, tempfile, time, fcntl, termios

MARK = "§"
KEYS = {"ret": "\r", "esc": "\x1b", "tab": "\t", "backspace": "\x7f", "lt": "<", "gt": ">"}


def keys(spec):
    out, i = [], 0
    while i < len(spec):
        if spec[i] == "<":
            j = spec.index(">", i)
            out.append(KEYS[spec[i + 1 : j]])
            i = j + 1
        else:
            out.append(spec[i])
            i += 1
    return out


def drain(fd, secs):
    end = time.time() + secs
    while time.time() < end:
        r, _, _ = select.select([fd], [], [], 0.02)
        if r:
            try:
                os.read(fd, 65536)
            except OSError:
                return


def run(case, home):
    path = os.path.join(home, os.path.basename(case["path"]))
    with open(path, "w") as f:
        f.write(case["text"])
    cfg = os.path.join(home, "config.toml")
    with open(cfg, "w") as f:
        f.write("[editor]\nauto-format = false\n[editor.lsp]\nenable = false\n")
    pid, fd = pty.fork()
    if pid == 0:
        for v in ("CONFIG", "CACHE", "DATA", "STATE"):
            os.environ["XDG_%s_HOME" % v] = home
        os.environ["TERM"] = "xterm-256color"
        hx = os.environ.get("HX", "hx")
        os.execvp(hx, [hx, "-c", cfg, path])
    fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", 24, 80, 0, 0))
    drain(fd, 1.0)
    for k in keys(case["keys"]) + [MARK, "\x1b"]:
        os.write(fd, k.encode())
        drain(fd, 0.25 if k == "\x1b" else 0.05)
    for k in ":wq!\r":
        os.write(fd, k.encode())
        drain(fd, 0.05)
    for _ in range(100):
        done, _ = os.waitpid(pid, os.WNOHANG)
        if done:
            break
        drain(fd, 0.05)
    else:
        os.kill(pid, 9)
        os.waitpid(pid, 0)
        sys.exit("hx did not quit on case " + case["name"])
    os.close(fd)
    with open(path) as f:
        text = f.read()
    at = text.index(MARK)
    row = text.count("\n", 0, at)
    col = len(text[text.rfind("\n", 0, at) + 1 : at].encode())
    cursor = {"row": row, "col": col}
    return {"name": case["name"], "text": text.replace(MARK, "", 1), "mode": "insert", "cursor": cursor, "anchor": cursor}


with tempfile.TemporaryDirectory() as home:
    for line in open(sys.argv[1]):
        if line.strip():
            print(json.dumps(run(json.loads(line), home), separators=(",", ":"), ensure_ascii=False))