Skip to content

Commit 76fccca

Browse files
committed
Further work on syntax highlighting support.
1 parent 8559bbd commit 76fccca

File tree

6 files changed

+251
-48
lines changed

6 files changed

+251
-48
lines changed

GunWin/App.config

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<configuration>
3-
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5-
</startup>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
<appSettings>
7+
<add key="MaximumRealtimeRenderNodes" value="50"/>
8+
</appSettings>
69
</configuration>

GunWin/EditorSyntaxHighlighter.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#region BSD 3-Clause License
2+
3+
// <copyright file="EditorSyntaxHighlighter.cs" company="Edgerunner.org">
4+
// Copyright 2020
5+
// </copyright>
6+
//
7+
// BSD 3-Clause License
8+
//
9+
// Copyright (c) 2020,
10+
// All rights reserved.
11+
//
12+
// Redistribution and use in source and binary forms, with or without
13+
// modification, are permitted provided that the following conditions are met:
14+
//
15+
// 1. Redistributions of source code must retain the above copyright notice, this
16+
// list of conditions and the following disclaimer.
17+
//
18+
// 2. Redistributions in binary form must reproduce the above copyright notice,
19+
// this list of conditions and the following disclaimer in the documentation
20+
// and/or other materials provided with the distribution.
21+
//
22+
// 3. Neither the name of the copyright holder nor the names of its
23+
// contributors may be used to endorse or promote products derived from
24+
// this software without specific prior written permission.
25+
//
26+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36+
37+
#endregion
38+
39+
using System.Collections.Generic;
40+
using System.Threading;
41+
using System.Windows.Forms;
42+
43+
using FastColoredTextBoxNS;
44+
45+
using Org.Edgerunner.ANTLR4.Tools.Testing.Grammar;
46+
47+
namespace Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin
48+
{
49+
public class EditorSyntaxHighlighter
50+
{
51+
private int _TokenColoringInProgress;
52+
53+
public void ColorizeTokens(FastColoredTextBox editor, StyleRegistry registry, IList<TokenViewModel> tokens)
54+
{
55+
int coloring = Interlocked.Exchange(ref _TokenColoringInProgress, 1);
56+
if (coloring != 0)
57+
return;
58+
59+
editor.BeginInvoke(
60+
new MethodInvoker(() =>
61+
{
62+
editor.BeginUpdate();
63+
64+
try
65+
{
66+
foreach (var token in tokens)
67+
{
68+
var startingPlace = new Place(token.ActualToken.Column, token.ActualToken.Line - 1);
69+
var stoppingPlace = new Place(token.ActualToken.Column + token.Text.Length, token.ActualToken.Line - 1);
70+
var tokenRange = editor.GetRange(startingPlace, stoppingPlace);
71+
tokenRange.ClearStyle(StyleIndex.All);
72+
var style = registry.GetTokenStyle(token);
73+
tokenRange.SetStyle(style);
74+
}
75+
}
76+
finally
77+
{
78+
editor.EndUpdate();
79+
}
80+
81+
_TokenColoringInProgress = 0;
82+
}));
83+
}
84+
}
85+
}

GunWin/GunWin.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<HintPath>..\packages\ObjectListView.2.7.1.5\lib\SparkleLibrary.dll</HintPath>
6565
</Reference>
6666
<Reference Include="System" />
67+
<Reference Include="System.Configuration" />
6768
<Reference Include="System.Core" />
6869
<Reference Include="System.Xml.Linq" />
6970
<Reference Include="System.Data.DataSetExtensions" />
@@ -76,13 +77,15 @@
7677
<Reference Include="System.Xml" />
7778
</ItemGroup>
7879
<ItemGroup>
80+
<Compile Include="EditorSyntaxHighlighter.cs" />
7981
<Compile Include="GrammarSelector.cs">
8082
<SubType>Form</SubType>
8183
</Compile>
8284
<Compile Include="GrammarSelector.Designer.cs">
8385
<DependentUpon>GrammarSelector.cs</DependentUpon>
8486
</Compile>
8587
<Compile Include="Options.cs" />
88+
<Compile Include="StyleRegistry.cs" />
8689
<Compile Include="VisualAnalyzer.cs">
8790
<SubType>Form</SubType>
8891
</Compile>

GunWin/StyleRegistry.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#region BSD 3-Clause License
2+
// <copyright file="StyleRegistry.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 System;
38+
using System.Collections.Generic;
39+
40+
using FastColoredTextBoxNS;
41+
42+
using JetBrains.Annotations;
43+
44+
using Org.Edgerunner.ANTLR4.Tools.Common;
45+
using Org.Edgerunner.ANTLR4.Tools.Testing.Grammar;
46+
47+
namespace Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin
48+
{
49+
/// <summary>
50+
/// Class for managing the various Style configurations of different tokens and errors.
51+
/// </summary>
52+
public class StyleRegistry
53+
{
54+
private readonly IEditorGuide _EditorGuide;
55+
56+
private readonly Dictionary<string, Style> _TokenStyles;
57+
58+
private readonly Dictionary<string, Style> _UniqueStyles;
59+
60+
private Style _ErrorStyle;
61+
62+
public StyleRegistry([NotNull] IEditorGuide editorGuide)
63+
{
64+
_EditorGuide = editorGuide ?? throw new ArgumentNullException(nameof(editorGuide));
65+
_TokenStyles = new Dictionary<string, Style>();
66+
_UniqueStyles = new Dictionary<string, Style>();
67+
}
68+
69+
/// <summary>
70+
/// Gets the style for a given token.
71+
/// </summary>
72+
/// <param name="token">The token.</param>
73+
/// <returns>A <see cref="Style"/> instance.</returns>
74+
public Style GetTokenStyle(TokenViewModel token)
75+
{
76+
if (_TokenStyles.TryGetValue(token.Type, out var style))
77+
return style;
78+
79+
var foregroundBrush = _EditorGuide.GetTokenForegroundBrush(token.Type);
80+
var backgroundBrush = _EditorGuide.GetTokenBackgroundBrush(token.Type);
81+
var fontStyle = _EditorGuide.GetTokenFontStyle(token.Type);
82+
var key = foregroundBrush.GetHashCode().ToString() + backgroundBrush.GetHashCode() + fontStyle;
83+
if (_UniqueStyles.TryGetValue(key, out style))
84+
{
85+
_TokenStyles[token.Type] = style;
86+
return style;
87+
}
88+
89+
style = new TextStyle(foregroundBrush, backgroundBrush, fontStyle);
90+
_UniqueStyles[key] = style;
91+
_TokenStyles[token.Type] = style;
92+
return style;
93+
}
94+
95+
/// <summary>
96+
/// Gets the style for a parse error .
97+
/// </summary>
98+
/// <returns>A <see cref="Style"/> instance.</returns>
99+
public Style GetParseErrorStyle()
100+
{
101+
return _ErrorStyle ?? (_ErrorStyle = new WavyLineStyle(240, _EditorGuide.ErrorColor));
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)