diff --git a/CSharpRepl/CSharpReplPromptCallbacks.cs b/CSharpRepl/CSharpReplPromptCallbacks.cs index cf3ff56..7b9a08f 100644 --- a/CSharpRepl/CSharpReplPromptCallbacks.cs +++ b/CSharpRepl/CSharpReplPromptCallbacks.cs @@ -159,14 +159,47 @@ protected override async Task TransformKeyPressAsync(string text, int static int GetSmartIndentationLevel(string text, int caret) { int openBraces = 0; - var end = Math.Min(text.Length, caret); - for (int i = 0; i < end; i++) + bool inSingleLineComment = false; + bool inMultiLineComment = false; + bool inString = false; + bool inChar = false; + bool escape = false; + + for (int i = 0; i < Math.Min(text.Length, caret); i++) { - var c = text[i]; - if (c == '{') ++openBraces; - if (c == '}') --openBraces; + char c = text[i]; + char prev = i > 0 ? text[i - 1] : '\0'; + + if (inSingleLineComment) + { + if (c == '\n') inSingleLineComment = false; + } + else if (inMultiLineComment) + { + if (prev == '*' && c == '/') inMultiLineComment = false; + } + else if (inString) + { + if (!escape && c == '"') inString = false; + escape = c == '\\' && !escape; + } + else if (inChar) + { + if (!escape && c == '\'') inChar = false; + escape = c == '\\' && !escape; + } + else + { + if (prev == '/' && c == '/') inSingleLineComment = true; + else if (prev == '/' && c == '*') inMultiLineComment = true; + else if (c == '"') inString = true; + else if (c == '\'') inChar = true; + else if (c == '{') openBraces++; + else if (c == '}') openBraces--; + } } - return openBraces; + + return Math.Max(0, openBraces); } static KeyPress NewLineWithIndentation(int indentation) =>