@@ -159,14 +159,47 @@ protected override async Task<KeyPress> TransformKeyPressAsync(string text, int
159159 static int GetSmartIndentationLevel ( string text , int caret )
160160 {
161161 int openBraces = 0 ;
162- var end = Math . Min ( text . Length , caret ) ;
163- for ( int i = 0 ; i < end ; i ++ )
162+ bool inSingleLineComment = false ;
163+ bool inMultiLineComment = false ;
164+ bool inString = false ;
165+ bool inChar = false ;
166+ bool escape = false ;
167+
168+ for ( int i = 0 ; i < Math . Min ( text . Length , caret ) ; i ++ )
164169 {
165- var c = text [ i ] ;
166- if ( c == '{' ) ++ openBraces ;
167- if ( c == '}' ) -- openBraces ;
170+ char c = text [ i ] ;
171+ char prev = i > 0 ? text [ i - 1 ] : '\0 ' ;
172+
173+ if ( inSingleLineComment )
174+ {
175+ if ( c == '\n ' ) inSingleLineComment = false ;
176+ }
177+ else if ( inMultiLineComment )
178+ {
179+ if ( prev == '*' && c == '/' ) inMultiLineComment = false ;
180+ }
181+ else if ( inString )
182+ {
183+ if ( ! escape && c == '"' ) inString = false ;
184+ escape = c == '\\ ' && ! escape ;
185+ }
186+ else if ( inChar )
187+ {
188+ if ( ! escape && c == '\' ' ) inChar = false ;
189+ escape = c == '\\ ' && ! escape ;
190+ }
191+ else
192+ {
193+ if ( prev == '/' && c == '/' ) inSingleLineComment = true ;
194+ else if ( prev == '/' && c == '*' ) inMultiLineComment = true ;
195+ else if ( c == '"' ) inString = true ;
196+ else if ( c == '\' ' ) inChar = true ;
197+ else if ( c == '{' ) openBraces ++ ;
198+ else if ( c == '}' ) openBraces -- ;
199+ }
168200 }
169- return openBraces ;
201+
202+ return Math . Max ( 0 , openBraces ) ;
170203 }
171204
172205 static KeyPress NewLineWithIndentation ( int indentation ) =>
0 commit comments