Skip to content

Rework indentation algo and add tests - #50

Merged
abrochard merged 4 commits into
abrochard:masterfrom
Hi-Angel:add-indentation-tests
Feb 17, 2026
Merged

Rework indentation algo and add tests#50
abrochard merged 4 commits into
abrochard:masterfrom
Hi-Angel:add-indentation-tests

Conversation

@Hi-Angel

@Hi-Angel Hi-Angel commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Rework indentation algorithm to prefer previous indentation

Parsing whole buffer to calculate the current indentation is wasteful
and error-prone. Mermaid evolves and adds new modes support for which
may be absent from the mode yet, so the user may want to apply custom
indentation and the plugin should account for it.

It is very typical in indentation engines for various programming
languages to account for previous indentation, because a user
frequently may have a reason to align previous indentation to
something only they aware of.

The rule of thumb: if a user wants to reindent code prior to the
current line, they can do that explicitly. So calculate current
indentation based on assumption that previous indentation is already
correct.

This also fixes a bug (see tests) where nested subgraphs were indented
linearly instead of increasing indentation; and another one with
negative numbers.

Fixes: #45


First commit adds indentation tests to make sure further indentation changes wouldn't break anything. At that point 3 tests are broken and are marked as such.

2nd commit reworks indentation algo per discussion in the issue, and that fixes the 3 tests. I also added more diagram-starter keywords that were missed in the older impl. such as flowchart. Besides being useful on its own this will also simplify 3rd commit.

3rd is a keywords deduplication.


Besides regression tests being added in the PR I also tested this algo on a large graph TD I have locally. Before this commit attempting to re-indent it results in broken indentation (due to nested subgraphs bug, see tests 2nd commit), after this commit the buffer remains unchanged as expected.

@Hi-Angel

Copy link
Copy Markdown
Contributor Author

@abrochard the tests are failing because of the pre-existing change 😊 The "package-requires" tag is set to 25.3, however compilation on 25.3 doesn't work because the code uses json-encode which only appeared in 28.1.

@Hi-Angel

Copy link
Copy Markdown
Contributor Author

The json-encode usage is introduced in e27930a by @mjburling

@Hi-Angel

Copy link
Copy Markdown
Contributor Author

upd: new commit — added basic support for stateDiagram-v2. There's still more to do, like adding support for state braces, but for now even that is better than nothing as a line following the stateDiagram-v2 now gets indented correctly (and also the whole keyword is now highlighted).

Btw, with this PR indentation support for state is better than before due re-using previous line indentation.

@Hi-Angel
Hi-Angel force-pushed the add-indentation-tests branch from 929fdf6 to 75c9a1c Compare February 12, 2026 10:11
@abrochard

Copy link
Copy Markdown
Owner

Hi @Hi-Angel , sorry for the delay and thank you for submitting this!
I will test it out locally in the coming week! Also fix CI if I have time to get to it

@Hi-Angel
Hi-Angel force-pushed the add-indentation-tests branch from 75c9a1c to 97e2019 Compare February 15, 2026 07:09
@Hi-Angel

Copy link
Copy Markdown
Contributor Author

Thanks!

upd: while reviewing my code I noticed I assumed we use indent-to which ignores negative offsets, but we use indent-line-to, which does not. So I fixed it and added a new test.

Fun fact btw, this test fails for the old (i.e. current master) behavior for exactly same reason — it lacked a check for negative numbers somewhere.

@Hi-Angel
Hi-Angel force-pushed the add-indentation-tests branch 2 times, most recently from b8d1a86 to f8c931f Compare February 15, 2026 07:15
Parsing whole buffer to calculate the current indentation is wasteful
and error-prone. Mermaid evolves and adds new modes support for which
may be absent from the mode yet, so the user may want to apply custom
indentation and the plugin should account for it.

It is very typical in indentation engines for various programming
languages to account for previous indentation, because a user
frequently may have a reason to align previous indentation to
something only they aware of.

The rule of thumb: if a user wants to reindent code prior to the
current line, they can do that explicitly. So calculate current
indentation based on assumption that previous indentation is already
correct.

This also fixes a bug (see tests) where nested subgraphs were indented
linearly instead of increasing indentation, and another one with
negative offsets.

Fixes: abrochard#45
@Hi-Angel
Hi-Angel force-pushed the add-indentation-tests branch from f8c931f to cffff69 Compare February 15, 2026 07:50
@abrochard

Copy link
Copy Markdown
Owner

I tested locally and seems fine for the diagram types we support (ie. the simple ones). I haven't kept up and was surprised by all the kind of diagrams that mermaid now supports!

@abrochard
abrochard merged commit 3b97216 into abrochard:master Feb 17, 2026
2 of 4 checks passed
@Hi-Angel

Copy link
Copy Markdown
Contributor Author

@abrochard why did you squash the commits though? 🤔

@abrochard

Copy link
Copy Markdown
Owner

@abrochard why did you squash the commits though? 🤔

Oh just force of habit

@Hi-Angel

Hi-Angel commented Feb 21, 2026

Copy link
Copy Markdown
Contributor Author

Do you often squash commits…?

@abrochard

Copy link
Copy Markdown
Owner

Do you often squash commits…?

Yeah all the time. I have a specific workflow at my job that requires it

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.

[RFE] Stop parsing more than previous line for indentation

2 participants