Docs/Code-Based Modeling/Text Editor Basics

Text Editor Basics

The Code tab in CoModel provides the Monaco editor—the same editor that powers VS Code—for editing a model's Julia / ModelingToolkit (MTK) source directly. It represents the same model as the graphical canvas and suits scenarios that need precise control over equations, bulk parameter changes, or reuse of external .jl files.

The Code tab and the overall editor layout

Entering the Code Tab

Click the Code tab at the top of the model editor page to enter the code editing view. The editor's initial content is loaded with the following precedence:

  1. The model's existing authoritative .jl source.
  2. If the model has no source yet, the initial text is generated from the current graphical canvas.

After the first load, the system records the current content as the baseline for detecting later modifications.

Editor Capabilities

The Monaco editor provides the following capabilities for Julia code:

Feature Description Shortcut
Syntax highlighting Keywords, strings, comments, numbers, @... macros, and operators are colored by semantics —
Line numbers and current-line highlight Line numbers on the left; the line at the cursor is highlighted —
Code folding Folds blocks based on indentation, function...end, begin...end, and similar Ctrl+K Ctrl+0/1/2
Find / replace Case sensitivity, regex, and scope filters Ctrl+F / Ctrl+H
Undo / redo The editor maintains its own operation stack, independent of diagram undo/redo Ctrl+Z / Ctrl+Y
Word wrap Toggled from the toolbar Alt+Z

The editor theme follows the platform's light/dark mode automatically.

Edit State and the Dirty Flag

When the editor content changes relative to the baseline of the last save or initialization, the system sets textDirty = true and shows the following indications:

  • The Save button on the toolbar becomes enabled.
  • The tab title or status bar shows an unsaved indicator (a dot or a text hint).

After a successful save, the baseline updates to the saved content and textDirty returns to false.

# Example: editing a component parameter directly
@named r1 = Resistor(R=100.0)   # change the R value
@named c1 = Capacitor(C=1e-6)

Saving

Click Save on the toolbar or press Ctrl+S to save the current text as the model's authoritative .jl source. Basic front-end validation runs before saving:

Check Requirement
Not empty File content cannot be empty
UTF-8 encoding Only UTF-8 text is accepted
Size limit Keep a single file under 1 MB

If saving fails, the editor keeps the unsaved content and dirty state, and a toast shows the reason.

Source of Truth and Switching Safeguards

CoModel uses a Source of Truth mechanism to keep text and diagram from overwriting each other. The current authoritative source, activeSource, is either graph or text:

Switch direction Behavior
Diagram → Code If the diagram side has unsaved changes or is newer than the text, the system regenerates the text through code generation; if the text has unsaved edits, a confirmation dialog asks whether to overwrite.
Code → Diagram If the text has unsaved changes, the platform prompts you to save first or confirm the sync; for the full two-way sync mechanism, see Code ↔ Diagram Two-Way Sync.

When leaving the editor or closing the page with unsaved changes, the browser shows a confirmation prompt.

FAQ

Q: What if the editor fails to load?
A: The system falls back to a read-only <pre> text block with a Retry button; the whole page does not crash.

Q: Does the graphical canvas update immediately after the text is saved?
A: Saving only updates the authoritative .jl source. When you switch to the Diagram tab, the platform decides whether to re-parse the text according to the rules in Code ↔ Diagram Two-Way Sync.