Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add electric dedent to lua mode #5424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add electric dedent to lua mode #5424

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented May 23, 2018

The lua mode currently adds a level of indentation when opening up a new
scope, but closing the scope doesn't remove a level. This change gives
opening scope indents a respective dedent.

The lua mode currently adds a level of indentation when opening up a new
scope, but closing the scope doesn't remove a level. This change gives
opening scope indents a respective dedent.
@marijnh
Copy link
Member

marijnh commented May 24, 2018

This breaks indentation on lines after else or elseif. I don't think there's any reason to touch dedentTokens, just adding the appropriate regexp as electricInput should be enough.

@ghost
Copy link
Author

ghost commented May 25, 2018

This is the behaviour I'm going for: https://webmshare.com/play/q5gmb

So I think the dedent keywords need separating out to differentiate between the indentation of the next scope and that of the line being typed.

I hacked together the .webm by using 'then' for the if/elseif indentation and then treating 'else' as a special case. Maybe there's a nicer way?

--- lua_old.js	2018-05-25 08:16:59.411017519 +0100
+++ lua_new.js	2018-05-25 08:24:47.534394133 +0100
@@ -63,9 +63,10 @@
                          "true","function", "end", "if", "then", "else", "do",
                          "while", "repeat", "until", "for", "in", "local" ]);
 
-  var indentTokens = wordRE(["function", "if","repeat","do", "\\(", "{"]);
-  var dedentTokens = wordRE(["end", "until", "\\)", "}"]);
-  var dedentPartial = prefixRE(["end", "until", "\\)", "}", "else", "elseif"]);
+  var indentTokens = wordRE(["function", "then","repeat","do", "\\(", "{"]);
+  var dedentKeywords = ["end", "until", "\\)", "}", "else", "elseif"];
+  var dedentTokens = wordRE(dedentKeywords);
+  var dedentPartial = prefixRE(dedentKeywords);
 
   function readBracket(stream) {
     var level = 0;
@@ -138,7 +139,7 @@
       }
       if ((style != "comment") && (style != "string")){
         if (indentTokens.test(word)) ++state.indentDepth;
-        else if (dedentTokens.test(word)) --state.indentDepth;
+        else if (dedentTokens.test(word) && word !== "else") --state.indentDepth;
       }
       return style;
     },
@@ -148,6 +149,7 @@
       return state.basecol + indentUnit * (state.indentDepth - (closing ? 1 : 0));
     },
 
+    electricInput: new RegExp("\\b" + dedentKeywords.join("|") + "\\b", "g"),
     lineComment: "--",
     blockCommentStart: "--[[",
     blockCommentEnd: "]]"

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