Skip to content

Add MATLAB syntax highlighting - #679

Open
MingqiZhang0 wants to merge 1 commit into
focus-editor:mainfrom
MingqiZhang0:feat/matlab-syntax-highlighting
Open

Add MATLAB syntax highlighting#679
MingqiZhang0 wants to merge 1 commit into
focus-editor:mainfrom
MingqiZhang0:feat/matlab-syntax-highlighting

Conversation

@MingqiZhang0

Copy link
Copy Markdown

Summary

Adds MATLAB syntax highlighting support to Focus.

This introduces a dedicated MATLAB tokenizer and wires it into Focus's language selection, comment commands, and color preview.

Highlights

The tokenizer supports:

  • MATLAB reserved keywords
  • true / false values
  • numbers, including decimal, scientific notation, and i / j suffixes
  • % line comments and %% section comments
  • %{ ... %} block comments with MATLAB's standalone-line delimiter rules
  • double-quoted strings
  • single-quoted character vectors with doubled quote escaping
  • MATLAB operators and punctuation
  • ... line continuation
  • function-call highlighting
  • function declaration highlighting, including:
    • function foo
    • function foo(x)
    • function y = foo
    • function y = foo(x)
    • function [a,b] = foo
    • function [a,b] = foo(x)

Special handling is included for MATLAB's ' ambiguity between character vectors and conjugate transpose.

A quote directly following an expression is treated as transpose, while a quote preceded by whitespace starts a character vector.

Examples:

B = A';
C = A.';
seq = [seq 'def'];
cell_data = {A 'text'};
combo = [A' 'text'];

.m file association

Focus currently uses .m for the existing Objective-C highlighting path, so this change intentionally does not globally remap .m files to MATLAB.

MATLAB projects can opt in explicitly:

*.m : matlab

A commented example has been added to the default config.

Comment commands

  • Toggle comment uses %.
  • The generic block-comment command falls back to line comments for MATLAB because %{ and %} have standalone-line requirements.

Validation

A fresh Jai compilation was not performed, because a Jai compiler was not available locally.

Instead, the MATLAB tokenizer logic was ported rule-for-rule to a standalone Python test harness for behavioral validation.

The Python tokenizer and a small Tkinter editor shared the same tokenizer implementation, allowing both token-stream regression tests and visual/live-edit behavior to be checked without using a third-party MATLAB lexer.

Validation performed with the Python surrogate:

  • 89 token-stream regression assertions: PASS
  • quote / transpose ambiguity tests: PASS
  • function declaration highlighting tests: PASS
  • block comment and ellipsis tests: PASS
  • identifier edge-case tests: PASS
  • live editing stress tests: PASS
  • visual syntax-highlighting inspection: PASS
  • sample MATLAB syntax additionally verified with MATLAB R2025b

The visual/live-edit tests included cases such as:

  • A'
  • A.'
  • A''
  • [seq 'def']
  • {A 'text'}
  • [A' 'text']
  • true'
  • false'
  • temporarily removing and restoring string delimiters
  • changing A' to A ' and back
  • removing and restoring %} in a block comment
  • rapid quote insertion/removal during live highlighting

The Python test harness is only a behavioral surrogate for the tokenizer.

It does not prove that the Jai source compiles, and it is not an actual Focus runtime test.

Current verification status:

  • Python tokenizer surrogate: PASS
  • Visual/live-edit surrogate: PASS
  • MATLAB syntax probes with MATLAB R2025b: PASS
  • Fresh Jai compilation: NOT PERFORMED
  • Actual patched Focus runtime: NOT PERFORMED

Scope

This PR only adds MATLAB syntax highlighting.

It does not add:

  • MATLAB execution/build integration
  • LSP support
  • completion
  • formatting
  • smart indentation
  • builtin function databases
  • .mlx support
  • automatic .m content detection
  • Octave-specific syntax

Screenshot

This screenshot comes from a standalone Python editor that ports the MATLAB tokenizer rules for visual demonstration only. It is not a Focus runtime screenshot. Because a Jai compiler was not available locally, the Jai implementation itself could not be compiled or tested.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant