From 8f8f037ea8cf84bf0fbafeb2e7b846e866dfa569 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 09:08:24 +0000 Subject: [PATCH 1/4] feat(script editor): One Dark Pro, Dracula and GitHub themes, finer colours, a status bar The Pine editor gets the three most installed editor themes, each with its light sibling (One Light, Alucard, GitHub Light), plus Console, the editor as it was. A theme re-defines the editor's own tokens, so the caret, gutter, line tint and selection follow it. refine() is a second pass over the tokenizer. It colours calls, types, literals, built-in series, constants, named arguments and operators apart, and the texts still join back to every line. A status bar under the code shows the Pine version, the caret's line and column, A-/A+ for the code size (starting at the operator's 6.25px) and the theme picker. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GLsbckEp88WEwC41dfyMeS --- CHANGELOG.md | 24 +++ console/backend/tests/test_editor_themes.py | 153 ++++++++++++++++++++ console/frontend/index.html | 16 ++ console/frontend/script-highlight.js | 112 +++++++++++++- console/frontend/styles.css | 91 +++++++++++- 5 files changed, 392 insertions(+), 4 deletions(-) create mode 100644 console/backend/tests/test_editor_themes.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 10abc26..bc1400d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,30 @@ All notable changes to this project are documented here. Format: [Keep a Changel ### Added +- **The Pine editor wears a real editor theme** (31 Oct). The operator asked for the script editor to look like a + code editor, in the most popular style. The three themes that top the lists of most-installed VS Code themes: + - **One Dark Pro** (the default), **Dracula** and **GitHub**, each with its own light sibling when the console + is light (One Light, Alucard, GitHub Light). **Console** keeps the editor as it was. + - The colours are each theme's published palette. Each theme re-defines the editor's own tokens, so the caret, + gutter, current-line tint and selection follow it. + - **Finer colours.** `refine()` in `script-highlight.js` adds a second pass over the tokenizer and colours + these apart: + - calls (`ta.rsi(`, `plot(`, your own `f(`); + - types (`float`, `int`); + - literals (`true`, `na`); + - built-in series (`close`, `bar_index`); + - constants (`color.red`, `label.style_none`); + - named arguments (`title =`); + - operators. + + The texts still join back to every line, so no character can go missing. + - **A status bar under the code**: the Pine version (`Pine v6`, read from `//@version`), `Ln 13, Col 11` with + the selection size, **Aβˆ’ / A+** for the code size, and the theme picker. The size starts at the operator's + 6.25px (6 Oct); A+ goes up to 14px. Both choices are kept per browser. + - The line tint and the selection are see-through, because they are drawn over the text. + - Pinned by `test_editor_themes.py`. Verified in Chromium in each theme, dark and light, at 6.25px and 11px: + alignment, selection, line numbers and no console errors. + - **The chart row's right-hand corner, checked and redone** (31 Oct). The operator asked for every control there to be checked and for the β‹― menu to look like the replay capsule. `topmenus.js`. - **The β‹― menu** is one frosted-glass sheet in two groups, Trade (Trader's desk, Edge Stats, Replay) and diff --git a/console/backend/tests/test_editor_themes.py b/console/backend/tests/test_editor_themes.py new file mode 100644 index 0000000..54679f9 --- /dev/null +++ b/console/backend/tests/test_editor_themes.py @@ -0,0 +1,153 @@ +"""The Pine editor's themes (31 Oct). + +The operator asked for the script editor to look like a real code editor, using the most popular themes. One Dark +Pro, Dracula and GitHub top every list of the most installed VS Code themes, so the editor offers those three +(each with its light sibling in the console's light mode) plus "Console", the editor as it was. + +A theme needs finer colours than the tokenizer's five, so refine() splits them further (call, type, literal, +built-in series, constant, named argument, operator). The promise the coloured copy lives by still holds: the +texts join back to the line, because the textarea's own text is invisible. +""" + +import json +import re +import shutil +import subprocess +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[3] +FRONT = ROOT / "console" / "frontend" +NODE = shutil.which("node") + +HL = (FRONT / "script-highlight.js").read_text() +CSS = (FRONT / "styles.css").read_text() +HTML = (FRONT / "index.html").read_text() + + +def run(body: str, data=None): + script = (f"const H = require({json.dumps(str(FRONT / 'script-highlight.js'))});\n" + f"const DATA = {json.dumps(data)};\n" + f"const out = (() => {{ {body} }})();\n" + "process.stdout.write(JSON.stringify(out === undefined ? null : out));") + done = subprocess.run([NODE, "-"], input=script, capture_output=True, text=True, timeout=60) + if done.returncode != 0: + raise AssertionError(done.stderr[-2000:]) + return json.loads(done.stdout) + + +def refined(line: str): + return [tuple(t) for t in run("return H.refine(H.tokenize(DATA)).filter((t) => t[0]);", line)] + + +CASES = [ + ('indicator("x", overlay = true)', [("f", "indicator"), ("s", '"x"'), ("a", "overlay"), ("o", "="), ("l", "true")]), + ("float r = ta.rsi(close, 14)", [("t", "float"), ("o", "="), ("f", "ta.rsi"), ("v", "close"), ("n", "14")]), + ("plot(r, color = color.new(#2962FF, 0))", + [("f", "plot"), ("a", "color"), ("o", "="), ("f", "color.new"), ("n", "#2962FF"), ("n", "0")]), + ("if barstate.islast", [("k", "if"), ("p", "barstate.islast")]), + ("x := na", [("o", ":="), ("l", "na")]), + ("f(x) => x * 2", [("f", "f"), ("o", "=>"), ("o", "*"), ("n", "2")]), + ("a = array.new()", [("o", "="), ("f", "array.new"), ("o", "<"), ("t", "float"), ("o", ">")]), + ("ok = syminfo.mintick < x", [("o", "="), ("p", "syminfo.mintick"), ("o", "<")]), + ("style = label.style_none", [("o", "="), ("p", "label.style_none")]), + ("// ta.sma(close) in a comment", [("c", "// ta.sma(close) in a comment")]), + ('"plot(x)" // s', [("s", '"plot(x)"'), ("c", "// s")]), +] + + +@unittest.skipUnless(NODE, "node is not installed") +class Refine(unittest.TestCase): + def test_a_table_of_lines(self): + for line, want in CASES: + self.assertEqual(refined(line), want, line) + + def test_the_texts_still_join_back_to_the_line(self): + pine = sorted(ROOT.glob("docs/studies/**/*.pine")) + corpus = [line for f in pine for line in f.read_text().split("\n")] + corpus += [line[:k] for line in list(corpus) for k in (1, len(line) // 2) if 0 < k < len(line)] + corpus += ["", " ", "a:=b", "x+=1", "a ? b : c", "ac", "f(", "(", "=>", "<", "x" * 5000, + "emoji πŸ˜€ \"πŸ˜€\" // πŸ˜€", "array.new<", "array.new("] + got = run(""" + const classes = new Set(['', 'c', 'd', 's', 'n', 'k', 'b', 't', 'l', 'f', 'v', 'p', 'a', 'o']); + const bad = []; + for (const line of DATA) { + const toks = H.refine(H.tokenize(line)); + const ok = toks.map((t) => t[1]).join('') === line && toks.every((t) => classes.has(t[0]) && t[1].length > 0); + if (!ok) bad.push(JSON.stringify(line.slice(0, 60))); + } + return { checked: DATA.length, bad };""", corpus) + self.assertEqual(got["bad"], []) + self.assertGreater(got["checked"], 50) + + def test_the_tokenizer_itself_is_unchanged(self): + # refine() is a second pass: tokenize()'s own classes (and its table in test_script_highlight) stand + self.assertIn("for (const [cls, s] of refine(tokenize(lines[i]))) {", HL) + self.assertIn("return { tokenize, refine, classify,", HL) + + +class Themes(unittest.TestCase): + def test_the_four_choices(self): + for value, name in (("onedark", "One Dark Pro"), ("dracula", "Dracula"), ("github", "GitHub"), ("console", "Console")): + self.assertIn(f'', HTML) + self.assertIn("const THEMES = ['onedark', 'dracula', 'github', 'console'];", HL) + self.assertIn("setTheme(read('ta-code-theme', 'onedark'))", HL, "One Dark Pro is the default") + + def test_each_theme_has_its_light_sibling(self): + for t in ("onedark", "dracula", "github"): + self.assertIn(f'[data-code-theme="{t}"] {{', CSS) + self.assertIn(f':root[data-theme="light"] [data-code-theme="{t}"] {{', CSS) + + def test_published_palettes(self): + one = re.search(r'\n\[data-code-theme="onedark"\] \{([^}]*)\}', CSS).group(1) + for c in ("#282c34", "#abb2bf", "#c678dd", "#61afef", "#98c379", "#d19a66", "#e5c07b", "#e06c75", "#56b6c2"): + self.assertIn(c, one) + dra = re.search(r'\n\[data-code-theme="dracula"\] \{([^}]*)\}', CSS).group(1) + for c in ("#282a36", "#f8f8f2", "#ff79c6", "#50fa7b", "#f1fa8c", "#bd93f9", "#8be9fd", "#ffb86c", "#6272a4"): + self.assertIn(c, dra) + gh = re.search(r'\n\[data-code-theme="github"\] \{([^}]*)\}', CSS).group(1) + for c in ("#0d1117", "#e6edf3", "#ff7b72", "#d2a8ff", "#a5d6ff", "#79c0ff", "#ffa657", "#8b949e"): + self.assertIn(c, gh) + + def test_console_is_the_editor_as_it_was(self): + # no attribute, so none of the theme rules apply and the five console colours stand + self.assertIn("if (theme === 'console') delete el.dataset.codeTheme;", HL) + for cls, fallback in (("f", "--lx-syn-builtin"), ("t", "--lx-syn-keyword"), ("l", "--lx-syn-number")): + self.assertRegex(CSS, r"\.hl-" + cls + r" \{ color: var\(--syn-\w+, var\(" + fallback + r"\)\); \}") + + def test_the_line_tint_and_the_selection_are_see_through(self): + # both are drawn over the text: an opaque one hid the line number and the selected word + self.assertIn(".script__edit[data-code-theme] .script__mark--cur { background: color-mix(in srgb, var(--lx-fg) 6%, transparent); }", CSS) + self.assertEqual(len(re.findall(r"--code-sel: rgba\(", CSS)), 6) + self.assertNotRegex(CSS, r"--code-sel: #") + + +class Footer(unittest.TestCase): + def test_it_is_on_the_page(self): + for part in ('id="script-foot"', 'id="script-lang"', 'id="script-pos"', 'id="script-zoom-in"', 'id="script-zoom-out"', + 'id="script-theme"'): + self.assertIn(part, HTML) + self.assertIn(".script.is-settings .script__foot, .script.is-scripts .script__foot { display: none; }", CSS) + + def test_the_operators_size_is_the_first_step(self): + self.assertIn("const SIZES = [6.25, 8, 9.5, 11, 12.5, 14];", HL) + self.assertIn("font-size: 6.25px; font-size: var(--code-size, 6.25px);", CSS) + # the gutter scales with the code, so the numbers stay on their lines + gutter = re.search(r"\.script__gutter \{([^}]*)\}", CSS).group(1) + self.assertIn("var(--code-size, 6.25px)", gutter) + + def test_a_size_change_repaints_the_gutter_and_the_colours(self): + self.assertIn("ta.dispatchEvent(new Event('scroll'));", HL) + self.assertIn("window.dispatchEvent(new Event('scriptpane:painted'));", HL) + + def test_position_and_version(self): + self.assertIn("pos.textContent = `Ln ${line}, Col ${col}`", HL) + self.assertIn("lang.textContent = m ? 'Pine v' + m[1] : 'Pine';", HL) + + def test_storage_is_optional(self): + self.assertIn("try { const v = localStorage.getItem(k);", HL) + self.assertIn("try { localStorage.setItem(k, String(v)); } catch (err)", HL) + + +if __name__ == "__main__": + unittest.main() diff --git a/console/frontend/index.html b/console/frontend/index.html index 6ec88ab..02006b3 100644 --- a/console/frontend/index.html +++ b/console/frontend/index.html @@ -182,6 +182,22 @@

Nothing selected

placeholder="// Paste Pine here, then press β–Ά Run. // Plots become Vela series; boxes, lines and labels go on the chart."> + +
+ Pine + Ln 1, Col 1 + + + + + +