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