Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.5

0.11.5 / tests / editor / code_editor_tests.luc

## The Code node's editor: Luce highlighting (keywords in the accent,
## numbers and comments their own colors), and text handed on once per
## commit, not per keystroke; text shown from the node replaces what is
## there unless typing is under way.
from code_editor import CodeEditor
from luce_ui.ui import Font
import theme

pub func code_editor_tests() -> unit!:
    let code = CodeEditor(Font())
    let taken: list[str] = []
    code.on_commit = func (text: str) -> unit!: taken.append(text)
    code.show("let x = 4.0  # four\n")
    let editor = code.editor
    assert(editor.text() == "let x = 4.0  # four\n")
    assert(editor.highlight_at(0) == theme.accent())
    assert(editor.highlight_at(8) == theme.code_number())
    assert(editor.highlight_at(14) == theme.text_muted())
    # Typing then committing hands the text on once.
    editor.set_selection(editor.length(), editor.length())
    editor.insert("p.P[1] += x\n")
    editor.insert("p.P[0] *= 2.0\n")
    code.commit()
    assert(taken.length == 1 and taken[0].ends_with("p.P[0] *= 2.0\n"))
    code.commit()
    assert(taken.length == 1)
    # The node's text shown (an undo) replaces it.
    code.show("# empty\n")
    assert(editor.text() == "# empty\n" and taken.length == 1)
    # A cook error marks its line; none clears it.
    code.show_error(1, 1, 7, "unknown name `x`")
    assert(editor.diagnostic_count() == 1)
    code.show_error(0, 0, 0, "")
    assert(editor.diagnostic_count() == 0)
    # Completions from luce-geocore after `p.` (Run Over Points).
    code.show("p.")
    editor.set_selection(2, 2)
    editor.complete()
    var labels: list[str] = []
    for at in 0..<editor.completion_count():
        labels.append(editor.completion_label(at))
    assert(labels.contains("Cd") and labels.contains("ptnum"))
    editor.close_completion()
    code.close()
    print("PASS code editor: Luce highlighting, one commit per pause, node text shown, error marks, completions")