abrochard / mermaid-mode

Emacs major mode for working with mermaid graphs https://mermaidjs.github.io/
GNU General Public License v3.0
189 stars 23 forks source link

[RFE] Stop parsing more than previous line for indentation #45

Open Hi-Angel opened 3 days ago

Hi-Angel commented 3 days ago

Starting up a discussion before I'd send the change to make sure the maintainer isn't against.

Problem: currently mermaid--calculate-desired-indentation parses way more than just the previous line. This is typically undesirable (in general, disregarding major-mode), because a user often may want custom indentation at current line. So whenever previous line lacks a keyword that would influence the next line indentation, the expected behavior is to continue same indentation.

Here's a problematic example:

block-beta
   block
       Hello
       world
   end

here, if you try to indent the world line, it will go back by 4 spaces, even though there's clearly a block of text with its own indentation.

(the fact that block-beta syntax isn't currently supported is only tangentially related, in a way that whenever such "new syntaxes" appears, indentation becomes completely unusable).

Solution:

Stop looking further than at the previous line. Besides being more convenient, this should also simplify the code. Also, this is the behavior that Emacs bulit-in major modes adhere to. This behavior is also mentioned in the paper SMIE: Weakness is power by one of the Emacs' maintainers.

Hi-Angel commented 3 days ago

Thinking of it, should probably correct it to "don't parse more than previous non-empty line", to make sure empty lines won't break indentation.

abrochard commented 3 days ago

Hi @Hi-Angel , It's been a really long time since I looked at the code, and even longer since I wrote it, but I think I ran into issues specific to the mermaid syntax that made me write this convoluted logic. If you have a way to simplify it, I'll be happy to review it. But I also strongly encourage to test it as thoroughly as possible on the newest version of mermaid.