Skip to content

Commit 70f8bef

Browse files
committed
Redesigned syntax highlight logic to fix error highlighting bug.
1 parent b4bd4fd commit 70f8bef

File tree

7 files changed

+84
-94
lines changed

7 files changed

+84
-94
lines changed

GunWin/Editor/FastColoredTextBoxExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static void SelectSource([NotNull] this FastColoredTextBox codeEditor, Pa
6969
var startingPlace = new Place(context.Start.Column, context.start.Line - 1);
7070
var stopToken = context.Stop ?? context.Start;
7171
var spot = stopToken.GetEndPlace();
72-
var stoppingPlace = new Place(spot.Position + 1, spot.Line - 1);
72+
var stoppingPlace = new Place(spot.Position, spot.Line - 1);
7373

7474
codeEditor.Selection = new Range(codeEditor, startingPlace, stoppingPlace);
7575
codeEditor.DoCaretVisible();
@@ -111,7 +111,7 @@ public static void SelectSource([NotNull] this FastColoredTextBox codeEditor, Er
111111
if (node.Symbol.StartIndex != -1)
112112
{
113113
var spot = node.Symbol.GetEndPlace();
114-
stoppingPlace = new Place(spot.Position + 1, spot.Line - 1);
114+
stoppingPlace = new Place(spot.Position, spot.Line - 1);
115115
}
116116
else
117117
stoppingPlace = startingPlace;
@@ -136,7 +136,7 @@ public static void SelectSource([NotNull] this FastColoredTextBox codeEditor, IT
136136

137137
var startingPlace = new Place(token.Column, token.Line - 1);
138138
var spot = token.GetEndPlace();
139-
var stoppingPlace = new Place(spot.Position + 1, spot.Line - 1);
139+
var stoppingPlace = new Place(spot.Position, spot.Line - 1);
140140

141141
codeEditor.Selection = new Range(codeEditor, startingPlace, stoppingPlace);
142142
codeEditor.DoCaretVisible();

GunWin/Editor/SyntaxHighlighting/EditorSyntaxHighlighter.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@
4141
using System.Threading;
4242
using System.Windows.Forms;
4343

44+
using Antlr4.Runtime;
45+
4446
using FastColoredTextBoxNS;
4547

48+
using Org.Edgerunner.ANTLR4.Tools.Testing.Extensions;
4649
using Org.Edgerunner.ANTLR4.Tools.Testing.Grammar;
4750
using Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin.Dialogs;
51+
using Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin.Extensions;
4852
using Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin.Properties;
4953

5054
using Place = FastColoredTextBoxNS.Place;
@@ -64,7 +68,8 @@ public class EditorSyntaxHighlighter
6468
/// <param name="editor">The editor.</param>
6569
/// <param name="registry">The registry.</param>
6670
/// <param name="tokens">The tokens.</param>
67-
public void ColorizeTokens(FastColoredTextBox editor, IStyleRegistry registry, IList<SyntaxToken> tokens)
71+
/// <param name="errorTokens">The error tokens.</param>
72+
public void ColorizeTokens(FastColoredTextBox editor, IStyleRegistry registry, IList<SyntaxToken> tokens, IList<IToken> errorTokens)
6873
{
6974
int coloring = Interlocked.Exchange(ref _TokenColoringInProgress, 1);
7075
if (coloring != 0)
@@ -86,6 +91,13 @@ public void ColorizeTokens(FastColoredTextBox editor, IStyleRegistry registry, I
8691
var style = registry.GetTokenStyle(token);
8792
tokenRange.SetStyle(style);
8893
}
94+
foreach (var token in errorTokens)
95+
{
96+
var startingPlace = new Place(token.Column, token.Line - 1);
97+
var stoppingPlace = token.GetEndPlace().ConvertToFctbPlace();
98+
var tokenRange = editor.GetRange(startingPlace, stoppingPlace);
99+
tokenRange.SetStyle(registry.GetParseErrorStyle());
100+
}
89101
}
90102
// ReSharper disable once CatchAllClause
91103
catch (Exception ex)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#region BSD 3-Clause License
2+
// <copyright file="PlaceExtensions.cs" company="Edgerunner.org">
3+
// Copyright 2020
4+
// </copyright>
5+
//
6+
// BSD 3-Clause License
7+
//
8+
// Copyright (c) 2020,
9+
// All rights reserved.
10+
//
11+
// Redistribution and use in source and binary forms, with or without
12+
// modification, are permitted provided that the following conditions are met:
13+
//
14+
// 1. Redistributions of source code must retain the above copyright notice, this
15+
// list of conditions and the following disclaimer.
16+
//
17+
// 2. Redistributions in binary form must reproduce the above copyright notice,
18+
// this list of conditions and the following disclaimer in the documentation
19+
// and/or other materials provided with the distribution.
20+
//
21+
// 3. Neither the name of the copyright holder nor the names of its
22+
// contributors may be used to endorse or promote products derived from
23+
// this software without specific prior written permission.
24+
//
25+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
#endregion
36+
37+
using Org.Edgerunner.ANTLR4.Tools.Testing.Grammar;
38+
39+
namespace Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin.Extensions
40+
{
41+
public static class PlaceExtensions
42+
{
43+
/// <summary>
44+
/// Converts to a token <see cref="Org.Edgerunner.ANTLR4.Tools.Testing.Grammar.Place"/> to a <see cref="FastColoredTextBoxNS.Place"/> place.
45+
/// </summary>
46+
/// <param name="place">The place to convert.</param>
47+
/// <returns>A new <see cref="FastColoredTextBoxNS.Place"/>.</returns>
48+
/// <remarks>The Fast Colored Text Box placement begins line indexes at index 0 so we must reduce the grammar placement by 1.</remarks>
49+
// ReSharper disable once IdentifierTypo
50+
public static FastColoredTextBoxNS.Place ConvertToFctbPlace(this Place place)
51+
{
52+
return new FastColoredTextBoxNS.Place(place.Position, place.Line - 1);
53+
}
54+
}
55+
}

GunWin/GunWin.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="Dialogs\GrammarSelector.Designer.cs">
101101
<DependentUpon>GrammarSelector.cs</DependentUpon>
102102
</Compile>
103+
<Compile Include="Extensions\PlaceExtensions.cs" />
103104
<Compile Include="Graphing\GraphingResult.cs" />
104105
<Compile Include="Graphing\GraphingWorkItem.cs" />
105106
<Compile Include="Graphing\GraphWorker.cs" />
@@ -180,5 +181,6 @@
180181
<ItemGroup>
181182
<Content Include="grun.net.ico" />
182183
</ItemGroup>
184+
<ItemGroup />
183185
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
184186
</Project>

GunWin/VisualAnalyzer.cs

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,7 @@ private void GuideAssemblyChanged(object sender, EditorGuideReference e)
321321
{
322322
_SyntaxGuide = guide;
323323
_Registry = new StyleRegistry(_SyntaxGuide);
324-
//DeColorExistingErrors();
325324
ColorizeTokens(null);
326-
ColorizeErrors();
327325
}
328326
}
329327

@@ -332,9 +330,7 @@ private void GrammarAssemblyChanged(object sender, GrammarReference e)
332330
var grammar = FetchGrammarInternal(e.AssemblyPath, e.GrammarName);
333331
SetGrammar(grammar);
334332
ParseSource();
335-
//DeColorExistingErrors();
336333
ColorizeTokens(null);
337-
ColorizeErrors();
338334
}
339335

340336
/// <summary>
@@ -472,87 +468,18 @@ private void CodeEditor_DragEnter(object sender, DragEventArgs e)
472468
e.Effect = DragDropEffects.None;
473469
}
474470

475-
private void DeColorExistingErrors()
471+
private List<IToken> GetErrorTokens()
476472
{
477-
if (_Registry == null)
478-
return;
479-
480-
CodeEditor.BeginUpdate();
481-
var lastLineNumber = CodeEditor.LinesCount;
482-
var lastColumn = CodeEditor.Lines[lastLineNumber - 1].Length;
483-
try
484-
{
485-
var errorStyle = _Registry.GetParseErrorStyle();
486-
var index = CodeEditor.GetStyleIndex(errorStyle);
487-
var styleIndex = (StyleIndex)index + 1;
488-
foreach (var token in _HighlightedErrors)
489-
{
490-
var startingPlace = new Place(token.Column, token.Line - 1);
491-
var stoppingPlace = new Place(token.Column + token.Text.Length, token.Line - 1);
473+
var result = new List<IToken>();
492474

493-
//if (stoppingPlace.iLine >= lastLineNumber)
494-
// break;
495-
496-
//if (stoppingPlace.iChar >= lastColumn)
497-
// break;
498-
499-
var tokenRange = CodeEditor.GetRange(startingPlace, stoppingPlace);
500-
tokenRange.ClearStyle(styleIndex);
501-
}
502-
503-
_HighlightedErrors.Clear();
504-
}
505-
catch (Exception ex)
506-
{
507-
var errorDisplay = new ErrorDisplay
508-
{
509-
Text = Resources.SyntaxErrorColoringErrorTitle,
510-
ErrorMessage = ex.Message,
511-
ErrorStackTrace = ex.StackTrace
512-
};
513-
errorDisplay.ShowDialog();
514-
}
515-
finally
516-
{
517-
CodeEditor.EndUpdate();
518-
}
519-
520-
}
475+
if (_ParseErrors == null)
476+
return result;
521477

522-
// ReSharper disable once TooManyDeclarations
523-
private void ColorizeErrors()
524-
{
525-
if (_Registry == null)
526-
return;
478+
foreach (var error in _ParseErrors)
479+
if (error.Token != null)
480+
result.Add(error.Token);
527481

528-
CodeEditor.BeginUpdate();
529-
try
530-
{
531-
var errorStyle = _Registry.GetParseErrorStyle();
532-
foreach (var error in _ParseErrors)
533-
{
534-
var token = error.Token;
535-
var startingPlace = new Place(token.Column, token.Line - 1);
536-
var stoppingPlace = new Place(token.Column + token.Text.Length, token.Line - 1);
537-
var tokenRange = CodeEditor.GetRange(startingPlace, stoppingPlace);
538-
tokenRange.SetStyle(errorStyle);
539-
_HighlightedErrors.Add(token);
540-
}
541-
}
542-
catch (Exception ex)
543-
{
544-
var errorDisplay = new ErrorDisplay
545-
{
546-
Text = Resources.SyntaxErrorColoringErrorTitle,
547-
ErrorMessage = ex.Message,
548-
ErrorStackTrace = ex.StackTrace
549-
};
550-
errorDisplay.ShowDialog();
551-
}
552-
finally
553-
{
554-
CodeEditor.EndUpdate();
555-
}
482+
return result;
556483
}
557484

558485
private void ColorizeTokens(Range range)
@@ -561,7 +488,7 @@ private void ColorizeTokens(Range range)
561488
return;
562489

563490
var tokensToColor = range == null ? _Tokens : FindTokensInRange(_Tokens, range);
564-
_Highlighter.ColorizeTokens(CodeEditor, _Registry, tokensToColor);
491+
_Highlighter.ColorizeTokens(CodeEditor, _Registry, tokensToColor, GetErrorTokens());
565492
}
566493

567494
private void ConfigureGraphWorker()
@@ -623,9 +550,7 @@ private void HeuristicHighlightingToolStripMenuItem_Click(object sender, EventAr
623550
{
624551
if (HeuristicHighlightingToolStripMenuItem.Checked)
625552
{
626-
//DeColorExistingErrors();
627553
ColorizeTokens(null);
628-
ColorizeErrors();
629554
}
630555
else
631556
{
@@ -1033,9 +958,7 @@ private void VisualAnalyzer_Load(object sender, EventArgs e)
1033958

1034959
// Handle initial parse and coloring on load
1035960
ParseSource();
1036-
// No reason to de-color existing errors since there should be none
1037961
ColorizeTokens(null);
1038-
ColorizeErrors();
1039962
}
1040963

1041964
private void ConfigureEditorSettings()
@@ -1079,9 +1002,7 @@ private void CodeEditor_TextChangedDelayed(object sender, TextChangedEventArgs e
10791002
else
10801003
{
10811004
ParseSource();
1082-
//DeColorExistingErrors();
10831005
ColorizeTokens(null);
1084-
ColorizeErrors();
10851006
}
10861007
}
10871008
}

TestRig/Extensions/TokenInterfaceExtensionMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static Place GetEndPlace([NotNull] this IToken token)
8484
}
8585
}
8686

87-
return new Place(token.Line + lineShift, (lineShift == 0) ? token.Column + positionShift - 1 : positionShift);
87+
return new Place(token.Line + lineShift, (lineShift == 0) ? token.Column + positionShift : positionShift);
8888
}
8989
}
9090
}

TestRig/Grammar/SyntaxToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public SyntaxToken(Lexer lexer, IToken parserToken)
6666

6767
var spot = parserToken.GetEndPlace();
6868
EndingLineNumber = spot.Line;
69-
EndingColumnPosition = spot.Position + 1;
69+
EndingColumnPosition = spot.Position;
7070
}
7171

7272
/// <summary>

0 commit comments

Comments
 (0)