@@ -61,6 +61,8 @@ public partial class EditorElement : UserControl
6161 private bool SelectionIsHighlited ;
6262 private bool WantFoldingUpdate ;
6363
64+ private static DateTime lastParsing ;
65+
6466 public EditorElement ( )
6567 {
6668 InitializeComponent ( ) ;
@@ -522,7 +524,7 @@ public void UpdateFontSize(double size, bool updateLineHeight = true)
522524 public void ToggleCommentOnLine ( )
523525 {
524526 var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
525- var lineText = editor . Document . GetText ( line . Offset , line . Length ) ;
527+ var lineText = editor . Document . GetText ( line ) ;
526528 var leadingWhiteSpaces = 0 ;
527529 foreach ( var l in lineText )
528530 if ( char . IsWhiteSpace ( l ) )
@@ -547,7 +549,7 @@ public void ToggleCommentOnLine()
547549 private void DuplicateLine ( bool down )
548550 {
549551 var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
550- var lineText = editor . Document . GetText ( line . Offset , line . Length ) ;
552+ var lineText = editor . Document . GetText ( line ) ;
551553 editor . Document . Insert ( line . Offset , lineText + Environment . NewLine ) ;
552554 if ( down ) editor . CaretOffset -= line . Length + 1 ;
553555 }
@@ -563,7 +565,7 @@ private void MoveLine(bool down)
563565 }
564566 else
565567 {
566- var lineText = editor . Document . GetText ( line . NextLine . Offset , line . NextLine . Length ) ;
568+ var lineText = editor . Document . GetText ( line . NextLine ) ;
567569 editor . Document . Remove ( line . NextLine . Offset , line . NextLine . TotalLength ) ;
568570 editor . Document . Insert ( line . Offset , lineText + Environment . NewLine ) ;
569571 }
@@ -578,7 +580,7 @@ private void MoveLine(bool down)
578580 {
579581 var insertOffset = line . PreviousLine . Offset ;
580582 var relativeCaretOffset = editor . CaretOffset - line . Offset ;
581- var lineText = editor . Document . GetText ( line . Offset , line . Length ) ;
583+ var lineText = editor . Document . GetText ( line ) ;
582584 editor . Document . Remove ( line . Offset , line . TotalLength ) ;
583585 editor . Document . Insert ( insertOffset , lineText + Environment . NewLine ) ;
584586 editor . CaretOffset = insertOffset + relativeCaretOffset ;
@@ -636,9 +638,17 @@ private void Caret_PositionChanged(object sender, EventArgs e)
636638 var result = bracketSearcher . SearchBracket ( editor . Document , editor . CaretOffset ) ;
637639 bracketHighlightRenderer . SetHighlight ( result ) ;
638640
639-
641+
640642 if ( ! Program . OptionsObject . Program_DynamicISAC || Program . MainWindow == null ) return ;
643+ /*
644+ Debug.WriteLineIf(lastParsing != null, $"Diffrence: {DateTime.Now - lastParsing}");
645+ if (lastParsing != null && DateTime.Now - lastParsing < new TimeSpan(0, 0, 2))
646+ return;
647+
648+ lastParsing = DateTime.Now;
641649
650+ Debug.WriteLine("Reparsing");
651+ */
642652 var ee = Program . MainWindow . GetAllEditorElements ( ) ;
643653 var ce = Program . MainWindow . GetCurrentEditorElement ( ) ;
644654
@@ -675,6 +685,7 @@ private void Caret_PositionChanged(object sender, EventArgs e)
675685 var acNodes = smDef . ProduceACNodes ( ) ;
676686 var isNodes = smDef . ProduceISNodes ( ) ;
677687
688+ // Lags the hell out when typing a lot.
678689 ce . editor . SyntaxHighlighting = new AeonEditorHighlighting ( smDef ) ;
679690 foreach ( var el in ee )
680691 {
@@ -726,58 +737,45 @@ private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
726737 // editor.TextArea.IndentationStrategy.IndentLine(editor.Document, editor.Document.GetLineByOffset(editor.CaretOffset));
727738 foldingStrategy . UpdateFoldings ( foldingManager , editor . Document ) ;
728739 break ;
740+ case "(" :
741+ case "[" :
729742 case "{" :
730- {
731743 if ( Program . OptionsObject . Editor_AutoCloseBrackets )
732744 {
733- editor . Document . Insert ( editor . CaretOffset , "}" ) ;
745+ var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
746+ var lineText = editor . Document . GetText ( line ) ;
747+
748+ // Don't auto close brackets when the user is in a comment or in a string.
749+ if ( ( lineText [ 0 ] == '/' && lineText [ 1 ] == '/' ) ||
750+ editor . Document . GetText ( line . Offset , editor . CaretOffset - line . Offset ) . Count ( c => c == '\" ' ) % 2 == 1 ||
751+ editor . Document . GetText ( line . Offset - 3 , 1 ) == "\\ " )
752+ break ;
753+
754+ // Getting the char ascii code with int cast and the string pos 0 (the char it self),
755+ // if it's a ( i need to add 1 to get the ascii code for closing bracket
756+ // for [ and { i need to add 2 to get the closing bracket ascii code
757+ char closingBracket = ( char ) ( ( int ) e . Text [ 0 ] + ( e . Text == "(" ? 1 : 2 ) ) ;
758+ editor . Document . Insert ( editor . CaretOffset , closingBracket . ToString ( ) ) ;
734759 editor . CaretOffset -= 1 ;
735- }
736760
737- foldingStrategy . UpdateFoldings ( foldingManager , editor . Document ) ;
738- break ;
739- }
740- default :
741- {
742- if ( Program . OptionsObject . Editor_AutoCloseBrackets )
743- {
744- if ( e . Text == "(" )
745- {
746- editor . Document . Insert ( editor . CaretOffset , ")" ) ;
747- editor . CaretOffset -= 1 ;
748- }
749- else if ( e . Text == "[" )
750- {
751- editor . Document . Insert ( editor . CaretOffset , "]" ) ;
752- editor . CaretOffset -= 1 ;
753- }
761+ // If it's a code block bracket we need to update the folding
762+ if ( e . Text == "{" )
763+ foldingStrategy . UpdateFoldings ( foldingManager , editor . Document ) ;
754764 }
755765
756766 break ;
757- }
758767 }
759768
760769 if ( Program . OptionsObject . Editor_AutoCloseStringChars )
761770 {
762- if ( e . Text == "\" " )
763- {
764- var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
765- var lineText = editor . Document . GetText ( line . Offset , editor . CaretOffset - line . Offset ) ;
766- if ( lineText . Length > 0 )
767- if ( lineText [ Math . Max ( lineText . Length - 2 , 0 ) ] != '\\ ' )
768- {
769- editor . Document . Insert ( editor . CaretOffset , "\" " ) ;
770- editor . CaretOffset -= 1 ;
771- }
772- }
773- else if ( e . Text == "'" )
771+ if ( e . Text == "\" " || e . Text == "'" )
774772 {
775773 var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
776774 var lineText = editor . Document . GetText ( line . Offset , editor . CaretOffset - line . Offset ) ;
777775 if ( lineText . Length > 0 )
778776 if ( lineText [ Math . Max ( lineText . Length - 2 , 0 ) ] != '\\ ' )
779777 {
780- editor . Document . Insert ( editor . CaretOffset , "'" ) ;
778+ editor . Document . Insert ( editor . CaretOffset , e . Text ) ;
781779 editor . CaretOffset -= 1 ;
782780 }
783781 }
0 commit comments