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

Commit b975c95

Browse files
committed
fix prototype
I created a check for brackets in Comment Blocks and Comment Lines - brackets won't get highlighted if inside any of those
1 parent d034f0c commit b975c95

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed

UI/Components/EditorElementBracketHighlighter.cs

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public class SPBracketSearcher : IBracketSearcher
8888
{
8989
private readonly string openingBrackets = "([{";
9090
private readonly string closingBrackets = ")]}";
91+
private bool isCommentBlockForward;
92+
private bool isCommentBlockBackward;
93+
private bool isCommentLine;
9194

9295
public BracketSearchResult SearchBracket(IDocument document, int offset)
9396
{
@@ -233,7 +236,10 @@ private int SearchBracketBackward(IDocument document, int offset, char openBrack
233236
}
234237

235238
var quickResult = QuickSearchBracketBackward(document, offset, openBracket, closingBracket);
236-
if (quickResult >= 0)
239+
isCommentBlockBackward = CheckForCommentBackward(document, offset, openBracket, closingBracket);
240+
isCommentLine = CheckForCommentLine(document, offset);
241+
242+
if (quickResult >= 0 && !isCommentBlockBackward && !isCommentLine)
237243
{
238244
return quickResult;
239245
}
@@ -328,7 +334,7 @@ private int SearchBracketBackward(IDocument document, int offset, char openBrack
328334
default:
329335
if (ch == openBracket)
330336
{
331-
if (!(inString || inChar || lineComment || blockComment))
337+
if (!(inString || inChar || lineComment || blockComment || isCommentBlockBackward || isCommentLine))
332338
{
333339
bracketStack.Push(i);
334340
}
@@ -371,7 +377,10 @@ private int SearchBracketForward(IDocument document, int offset, char openBracke
371377
}
372378

373379
var quickResult = QuickSearchBracketForward(document, offset, openBracket, closingBracket);
374-
if (quickResult >= 0)
380+
isCommentBlockForward = CheckForCommentBlockForward(document, offset);
381+
isCommentLine = CheckForCommentLine(document, offset);
382+
383+
if (quickResult >= 0 && !isCommentBlockForward && !isCommentLine)
375384
{
376385
return quickResult;
377386
}
@@ -466,7 +475,7 @@ private int SearchBracketForward(IDocument document, int offset, char openBracke
466475
}
467476
else if (ch == closingBracket)
468477
{
469-
if (!(inString || inChar || lineComment || blockComment))
478+
if (!(inString || inChar || lineComment || blockComment || isCommentBlockForward || isCommentLine))
470479
{
471480
--brackets;
472481
if (brackets == 0)
@@ -569,5 +578,76 @@ private int QuickSearchBracketForward(IDocument document, int offset, char openB
569578
}
570579
return -1;
571580
}
581+
582+
private bool CheckForCommentBlockForward(IDocument document, int offset)
583+
{
584+
for (var i = offset; i < document.TextLength; ++i)
585+
{
586+
var ch = document.GetCharAt(i);
587+
588+
// If we find the characters ' */ ' together, it means a comment block is finishing
589+
// therefore, we've been inside a code block this whole time:
590+
// this bracket should be ignored by the highlighter
591+
592+
if (ch == '*' && document.GetCharAt(i + 1) == '/')
593+
{
594+
return true;
595+
}
596+
597+
// If we find, however, ' /* ', a code block is starting - I'll try and return false on this one
598+
599+
if (ch == '/' && document.GetCharAt(i + 1) == '*')
600+
{
601+
return false;
602+
}
603+
604+
}
605+
return false;
606+
}
607+
608+
private bool CheckForCommentBackward(IDocument document, int offset, char openBracket, char closingBracket)
609+
{
610+
for (var i = offset; i >= 0; --i)
611+
{
612+
var ch = document.GetCharAt(i);
613+
614+
// If we find the characters ' /* ' together (backwards speaking), it means a comment block is starting
615+
// therefore, we've been inside a code block this whole time:
616+
// this bracket should be ignored by the highlighter
617+
618+
if (ch == '*' && document.GetCharAt(i - 1) == '/')
619+
{
620+
return true;
621+
}
622+
623+
// If we find, however, ' /* ', a code block is finishing - I'll try and return false on this one
624+
625+
if (ch == '/' && document.GetCharAt(i - 1) == '*')
626+
{
627+
return false;
628+
}
629+
630+
}
631+
return false;
632+
}
633+
634+
private bool CheckForCommentLine(IDocument document, int offset)
635+
{
636+
for (var i = offset; i >= 0; --i)
637+
{
638+
var ch = document.GetCharAt(i);
639+
640+
if (ch == '/' && document.GetCharAt(i - 1) == '/')
641+
{
642+
return true;
643+
}
644+
645+
if (ch == '\n')
646+
{
647+
break;
648+
}
649+
}
650+
return false;
651+
}
572652
}
573653
}

0 commit comments

Comments
 (0)