Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit ff909b9

Browse files
committed
fix #82 - small refactoring
1 parent 20c9ab4 commit ff909b9

File tree

4 files changed

+300
-277
lines changed

4 files changed

+300
-277
lines changed

Spcode.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
<Compile Include="UI\Windows\AboutWindow.xaml.cs">
205205
<DependentUpon>AboutWindow.xaml</DependentUpon>
206206
</Compile>
207+
<Compile Include="Utils\BracketHighlightHelpers.cs" />
207208
<Compile Include="Utils\Constants.cs" />
208209
<Compile Include="Utils\JavaUtils.cs" />
209210
<Compile Include="Utils\Paths.cs" />

UI/Components/EditorElement.xaml.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using SPCode.Utils.SPSyntaxTidy;
2525
using Xceed.Wpf.AvalonDock.Layout;
2626
using Timer = System.Timers.Timer;
27+
using SPCode.Utils;
2728

2829
namespace SPCode.UI.Components
2930
{
@@ -59,6 +60,8 @@ public partial class EditorElement : UserControl
5960
private bool SelectionIsHighlited;
6061
private bool WantFoldingUpdate;
6162

63+
public BracketHighlightHelpers bracketHelper = new BracketHighlightHelpers();
64+
6265
public EditorElement()
6366
{
6467
InitializeComponent();
@@ -889,7 +892,7 @@ private void ParseIncludes(object sender, EventArgs e)
889892
});
890893
}
891894

892-
private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
895+
public void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
893896
{
894897
if (Program.OptionsObject.Editor_ReformatLineAfterSemicolon)
895898
{
@@ -935,14 +938,15 @@ private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
935938
case "{":
936939
if (Program.OptionsObject.Editor_AutoCloseBrackets)
937940
{
938-
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
939-
var lineText = editor.Document.GetText(line);
940-
941-
// Don't auto close brackets when the user is in a comment or in a string or a text is selected.
942-
if ((editor.SelectionLength == 0 &&
943-
lineText[0] == '/' && lineText[1] == '/') ||
944-
editor.Document.GetText(line.Offset, editor.CaretOffset - line.Offset).Count(c => c == '\"') % 2 == 1 ||
945-
(line.LineNumber != 1 && editor.Document.GetText(line.Offset - 3, 1) == "\\"))
941+
var document = editor.Document;
942+
var offset = editor.TextArea.Caret.Offset;
943+
var a = bracketHelper.CheckForCommentLine(document, offset);
944+
if (editor.SelectionLength == 0 &&
945+
(bracketHelper.CheckForCommentBlockForward(document, offset) ||
946+
bracketHelper.CheckForCommentBlockBackward(document, offset) ||
947+
bracketHelper.CheckForCommentLine(document, offset) ||
948+
bracketHelper.CheckForString(document, offset) ||
949+
bracketHelper.CheckForChar(document, offset)))
946950
{
947951
break;
948952
}

UI/Components/EditorElementBracketHighlighter.cs

+14-268
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Windows.Media;
44
using ICSharpCode.AvalonEdit.Document;
55
using ICSharpCode.AvalonEdit.Rendering;
6+
using SPCode.Utils;
67

78
namespace SPCode.UI.Components
89
{
@@ -95,6 +96,8 @@ public class SPBracketSearcher : IBracketSearcher
9596
private bool isCommentLine;
9697
private bool isString;
9798
private bool isChar;
99+
public BracketHighlightHelpers bracketHelper = new BracketHighlightHelpers();
100+
98101
#endregion
99102

100103
public BracketSearchResult SearchBracket(IDocument document, int offset)
@@ -238,7 +241,6 @@ private int SearchBracketForward(IDocument document, int offset, char openBracke
238241
var inString = false;
239242
var inChar = false;
240243
var verbatim = false;
241-
242244
var lineComment = false;
243245
var blockComment = false;
244246

@@ -250,11 +252,11 @@ private int SearchBracketForward(IDocument document, int offset, char openBracke
250252
// After searching for quick result, we check for
251253
// comment blocks, comment lines, string and chars
252254

253-
var quickResult = QuickSearchBracketForward(document, offset, openBracket, closingBracket);
254-
isCommentBlockForward = CheckForCommentBlockForward(document, offset);
255-
isCommentLine = CheckForCommentLine(document, offset);
256-
isString = CheckForString(document, offset);
257-
isChar = CheckForChar(document, offset);
255+
var quickResult = bracketHelper.QuickSearchBracketForward(document, offset, openBracket, closingBracket);
256+
isCommentBlockForward = bracketHelper.CheckForCommentBlockForward(document, offset);
257+
isCommentLine = bracketHelper.CheckForCommentLine(document, offset);
258+
isString = bracketHelper.CheckForString(document, offset);
259+
isChar = bracketHelper.CheckForChar(document, offset);
258260

259261
// If the Quick Search result is valid, only return it if we determined the bracket
260262
// is valid (is not inside a comment block, comment line, string or char)
@@ -382,11 +384,11 @@ private int SearchBracketBackward(IDocument document, int offset, char openBrack
382384
// After searching for quick result, we check for
383385
// comment blocks, comment lines, string and chars
384386

385-
var quickResult = QuickSearchBracketBackward(document, offset, openBracket, closingBracket);
386-
isCommentBlockBackward = CheckForCommentBackward(document, offset, openBracket, closingBracket);
387-
isCommentLine = CheckForCommentLine(document, offset);
388-
isString = CheckForString(document, offset);
389-
isChar = CheckForChar(document, offset);
387+
var quickResult = bracketHelper.QuickSearchBracketBackward(document, offset, openBracket, closingBracket);
388+
isCommentBlockBackward = bracketHelper.CheckForCommentBlockBackward(document, offset);
389+
isCommentLine = bracketHelper.CheckForCommentLine(document, offset);
390+
isString = bracketHelper.CheckForString(document, offset);
391+
isChar = bracketHelper.CheckForChar(document, offset);
390392

391393
// If the Quick Search result is valid, only return it if we determined the bracket
392394
// is valid (is not inside a comment block, comment line, string or char)
@@ -515,262 +517,6 @@ private int SearchBracketBackward(IDocument document, int offset, char openBrack
515517
}
516518
#endregion
517519

518-
#region Quick Search Helpers
519-
private int QuickSearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket)
520-
{
521-
var brackets = -1;
522-
for (var i = offset; i >= 0; --i)
523-
{
524-
var ch = document.GetCharAt(i);
525-
if (ch == openBracket)
526-
{
527-
++brackets;
528-
if (brackets == 0)
529-
{
530-
return i;
531-
}
532-
}
533-
else if (ch == closingBracket)
534-
{
535-
--brackets;
536-
}
537-
else if (ch == '"')
538-
{
539-
break;
540-
}
541-
else if (ch == '\'')
542-
{
543-
break;
544-
}
545-
else if (ch == '/' && i > 0)
546-
{
547-
if (document.GetCharAt(i - 1) == '/')
548-
{
549-
break;
550-
}
551-
552-
if (document.GetCharAt(i - 1) == '*')
553-
{
554-
break;
555-
}
556-
}
557-
}
558-
return -1;
559-
}
560-
561-
private int QuickSearchBracketForward(IDocument document, int offset, char openBracket, char closingBracket)
562-
{
563-
var brackets = 1;
564-
// try "quick find" - find the matching bracket if there is no string/comment in the way
565-
for (var i = offset; i < document.TextLength; ++i)
566-
{
567-
var ch = document.GetCharAt(i);
568-
if (ch == openBracket)
569-
{
570-
++brackets;
571-
}
572-
else if (ch == closingBracket)
573-
{
574-
--brackets;
575-
if (brackets == 0)
576-
{
577-
return i;
578-
}
579-
}
580-
else if (ch == '"')
581-
{
582-
break;
583-
}
584-
else if (ch == '\'')
585-
{
586-
break;
587-
}
588-
else if (ch == '/' && i > 0)
589-
{
590-
if (document.GetCharAt(i - 1) == '/')
591-
{
592-
break;
593-
}
594-
}
595-
else if (ch == '*' && i > 0)
596-
{
597-
if (document.GetCharAt(i - 1) == '/')
598-
{
599-
break;
600-
}
601-
}
602-
}
603-
return -1;
604-
}
605-
606-
private bool CheckForCommentBlockForward(IDocument document, int offset)
607-
{
608-
for (var i = offset; i < document.TextLength; ++i)
609-
{
610-
var ch = document.GetCharAt(i);
611-
612-
// If we find the characters ' */ ' together scanning forward,
613-
// it means a comment block is finishing
614-
// therefore, we've been inside a code block this whole time:
615-
// this bracket should be ignored by the highlighter
616-
617-
if (ch == '*' && document.GetCharAt(i + 1) == '/')
618-
{
619-
return true;
620-
}
621-
622-
// If we find, however, ' /* ', a code block is starting:
623-
// not possible that we've been in a comment block
624-
// that's a nested comment compiling error
625-
626-
if (ch == '/' && document.GetCharAt(i + 1) == '*')
627-
{
628-
return false;
629-
}
630-
631-
}
632-
return false;
633-
}
634-
635-
private bool CheckForCommentBackward(IDocument document, int offset, char openBracket, char closingBracket)
636-
{
637-
for (var i = offset; i >= 0; --i)
638-
{
639-
var ch = document.GetCharAt(i);
640-
641-
// If we find the characters ' /* ' together while scanning backwards,
642-
// it means a comment block is starting
643-
// therefore, we've been inside a code block this whole time:
644-
// this bracket should be ignored by the highlighter
645-
646-
if (ch == '*' && document.GetCharAt(i - 1) == '/')
647-
{
648-
return true;
649-
}
650-
651-
// If we find, however, ' /* ', a code block is finishing:
652-
// not possible that we've been in a comment block
653-
// that's a nested comment compiling error
654-
655-
if (ch == '/' && i > 0 && document.GetCharAt(i - 1) == '*')
656-
{
657-
return false;
658-
}
659-
660-
}
661-
return false;
662-
}
663-
664-
private bool CheckForCommentLine(IDocument document, int offset)
665-
{
666-
for (var i = offset; i >= 0; --i)
667-
{
668-
var ch = document.GetCharAt(i);
669-
670-
// If we find two ' // ' together as we scan backwards
671-
// we find we are in a comment line, and should ignore the bracket
672-
673-
if (ch == '/' && document.GetCharAt(i - 1) == '/')
674-
{
675-
return true;
676-
}
677-
678-
// If the next scanned character is a newline, cut the function off
679-
680-
if (ch == '\n')
681-
{
682-
break;
683-
}
684-
}
685-
return false;
686-
}
687-
688-
private bool CheckForString(IDocument document, int offset)
689-
{
690-
var quoteFound = false;
691-
for (var i = offset; i >= 0; --i)
692-
{
693-
var ch = document.GetCharAt(i);
694-
695-
// If we find a quote in the same line, set a flag.
696-
697-
if (ch == '"')
698-
{
699-
quoteFound = true;
700-
}
701-
702-
// Otherwise, keep looking for a line jump and a '\'
703-
// to cover the case of its usage to escape the newline
704-
705-
if (ch == '\n' && i > 0)
706-
{
707-
if (document.GetCharAt(i - 1) == '\r' && document.GetCharAt(i - 2) == '\\')
708-
{
709-
continue;
710-
}
711-
else
712-
{
713-
break;
714-
}
715-
}
716-
}
717-
718-
// If there was an opening quote, look forward for the closing quote
719-
// skipping the combination of a backslash and a newline
720-
721-
if (quoteFound)
722-
{
723-
for (var i = offset; i < document.TextLength; ++i)
724-
{
725-
var ch = document.GetCharAt(i);
726-
if (ch == '"')
727-
{
728-
return true;
729-
}
730-
731-
if (ch == '\\' && document.GetCharAt(i + 1) == '\r')
732-
{
733-
continue;
734-
}
735-
}
736-
}
737-
return false;
738-
}
739-
740-
private bool CheckForChar(IDocument document, int offset)
741-
{
742-
var apFound = false;
743-
for (var i = offset; i >= 0; --i)
744-
{
745-
var ch = document.GetCharAt(i);
746-
747-
// Scanning backwards, if we find the apostrophe, set the flag
748-
749-
if (ch == '\'')
750-
{
751-
apFound = true;
752-
}
753-
}
754-
if (apFound)
755-
{
756-
for (var i = offset; i < document.TextLength; ++i)
757-
{
758-
var ch = document.GetCharAt(i);
759-
760-
// If the flag is true, scan for the other one
761-
762-
if (ch == '\'')
763-
{
764-
return true;
765-
}
766-
if (ch == '\n')
767-
{
768-
break;
769-
}
770-
}
771-
}
772-
return false;
773-
}
774-
#endregion
520+
775521
}
776522
}

0 commit comments

Comments
 (0)