Add electric dedent to lua mode#5424
Open
Conversation
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.
Member
|
This breaks indentation on lines after |
Author
|
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: "]]" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.