Rolando Andrade
Back to projects

Built in 2023

StepCode | Editor

Web-based Code Editor

StepCode | Editor preview

StepCode Editor is a web-based editor for the StepCode language. It is written in TypeScript and uses the CodeMirror editor.

Students who learn programming with pseudocode usually write it on paper or in a basic text editor. They cannot run it. They cannot see their errors as they type. The jump from pseudocode to real programming feels too big.

StepCode Editor is a browser based IDE (integrated development environment) for writing and running pseudocode. I built it to give students a real coding experience while they learn the basics. There is nothing to install. You open the browser and start coding.

Closing the feedback gap

I wanted to close the gap between writing pseudocode and running it. StepCode Editor runs pseudocode right in the browser. Students watch their algorithms run and get feedback right away. The loop between writing and learning gets much shorter.

An editor on CodeMirror 6

The editor is built on CodeMirror 6, a full rewrite of the popular CodeMirror library. It is modular, so it is easy to customize.

CodeMirror 6 does the heavy work: syntax highlighting, line numbers, code folding and keyboard shortcuts. I only had to teach it to understand StepCode.

The editor state is immutable. Every change creates a new state instead of editing the old one. That makes undo and redo simple. It also makes the editor predictable and easy to debug.

A grammar written in Lezer

CodeMirror 6 uses Lezer for parsing. Lezer is an incremental parser, which means it only parses again the parts of the document that changed. The editor stays fast, even with large files.

I wrote the grammar as a separate npm package called lezer-stepcode. It understands StepCode’s structure: processes, functions, loops, conditionals and variable definitions.

The grammar powers the smart features. The completion system knows which suggestions make sense where you are typing. Inside a loop, it suggests loop keywords. Inside a function, it suggests return statements. All of that comes from Lezer’s parse tree.

Arrows that look like textbooks

One feature I am proud of is automatic symbol replacement. When you type <-, it becomes ←. Type != and it becomes ≠. The same happens for <=, >= and ->.

It happens while you type. The editor watches for these patterns and swaps them right away. Your code looks cleaner and matches how pseudocode appears in textbooks.

The code uses CodeMirror’s update listener. On every change, it scans for the patterns and sends replacement transactions. The user never notices the swap.

const matches = value.matchAll(/(->|<-|!=|<=|>=)/g);
const changes: ChangeSpec[] = [];
for (const match of matches) {
  changes.push({
    from: match.index!,
    to: match.index! + match[0].length,
    insert: match[0]
      .replace('<-', '←')
      .replace('!=', '≠')
      .replace('<=', '≤')
      .replace('>=', '≥')
      .replace('->', '→'),
  });
}

Errors as you type

The editor checks your code while you type. Syntax errors get a red underline right away. Hover over one to see what went wrong.

The linter also suggests fixes. If you forget a semicolon, it offers to add it. Click the suggestion and the fix is applied.

The linter uses the same StepCode interpreter that runs the code. So the messages are consistent. The error you see in the editor is the error you would get at runtime.

Code runs in a Web Worker

StepCode can run infinite loops. A student might write Mientras Verdadero with no way out. Without protection, that would freeze the browser.

Web Workers solve this. A Web Worker runs code in a separate thread from the page. The interpreter runs there, so the interface stays responsive no matter what the code does. Users can stop it at any time.

The worker talks to the page through messages. It sends output to display and asks for input when it needs it. The main thread runs the terminal interface and coordinates everything.

self.onmessage = (e: MessageEvent<Message>) => {
  const eventBus = new EventBus();
  eventBus.on('input-request', (resolve) => {
    self.postMessage({ type: 'input' });
    resolveInput = resolve;
  });
  eventBus.on('output-request', (content: string) => {
    self.postMessage({ type: 'output', content });
  });
  // ... execution logic
};

This keeps responsibilities separate. The interpreter knows nothing about the DOM. The interface knows nothing about parsing. They talk through a simple message protocol.

A terminal you can drag

The terminal was a fun challenge. I wanted it to feel like a real terminal window. Users can drag it around, maximize it, minimize it or close it.

I used dnd-kit for drag and drop. It handles touch events, keyboard accessibility and smooth animations. The terminal floats above the editor as a modal dialog.

The terminal shows three kinds of content: output lines, input prompts and errors. Each has its own style. Errors are red. Input prompts show a blinking cursor. Output scrolls to stay at the bottom.

Users can use the terminal while the code runs. When the code asks for input, a text field appears. You type your answer and press enter. The worker receives it and keeps running.

Suggestions that know the scope

Autocomplete knows where you are in the code. At the top level, it suggests Proceso, Algoritmo or Funcion. Inside a function, it adds Retornar.

This comes from the Lezer syntax tree. The completion function finds the current node and walks up the tree to the enclosing scope. Each scope has its own set of suggestions.

const completionsMap = new Map([
  ['Script', structuresCompletions],
  ['ForStatement', [...genericCompletions, ...insideLoopCompletions]],
  ['Function', [...genericCompletions, ...insideFunctionsCompletions]],
]);

Completions are snippets, not only keywords. Select Funcion and you get a full function template with placeholders. Press Tab to move between them and fill in the name, parameters and body.

Themes and small comforts

The editor has light and dark themes and remembers your choice. The themes extend Atom’s One Light and One Dark color schemes, which are easy on the eyes during long sessions.

Users can zoom with Ctrl and the scroll wheel, and the font size stays the same next time. These are small details, but they make the editor feel finished.

Inside a code editor

StepCode Editor taught me a lot about how editors work inside. CodeMirror’s architecture is elegant. Its immutable state model makes complex features possible.

Lezer grammars are powerful but tricky. Getting incremental parsing right took several tries. It was worth it, because the editor stays fast even with complex code.

Web Workers are a must for running untrusted code. The separate thread keeps users safe, and passing messages keeps the code clean.

Try it

StepCode Editor is live at stepcode.letsbuildsolutions.com. Open it in your browser and start writing pseudocode. No signup. No installation.

Here is a small program you can run right on this page. Press the play button to count down to liftoff, then change the numbers and run it again.

The source code is on GitHub if you want to see how it works. You are welcome to open issues or contribute improvements.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build