-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
168 lines (150 loc) · 7.37 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using DevExpress.CodeParser;
using DevExpress.Xpf.RichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Services;
using System.Windows;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.Xpf.Core;
using System.IO;
using System;
using DevExpress.XtraRichEdit;
namespace SyntaxHighlightSimple
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : ThemedWindow
{
public MainWindow()
{
InitializeComponent();
}
private void richEditControl1_Loaded(object sender, RoutedEventArgs e)
{
// Use service substitution to register a custom service that implements highlighting.
richEditControl1.ReplaceService<ISyntaxHighlightService>(new MySyntaxHighlightService(richEditControl1));
string path = "MainWindow.xaml.cs";
richEditControl1.LoadDocument(path, DocumentFormat.PlainText);
}
}
public class MySyntaxHighlightService : ISyntaxHighlightService
{
readonly RichEditControl syntaxEditor;
SyntaxColors syntaxColors;
SyntaxHighlightProperties commentProperties;
SyntaxHighlightProperties keywordProperties;
SyntaxHighlightProperties stringProperties;
SyntaxHighlightProperties xmlCommentProperties;
SyntaxHighlightProperties textProperties;
public MySyntaxHighlightService(RichEditControl syntaxEditor)
{
this.syntaxEditor = syntaxEditor;
syntaxColors = new SyntaxColors(UserLookAndFeel.Default);
}
void HighlightSyntax(TokenCollection tokens)
{
commentProperties = new SyntaxHighlightProperties();
commentProperties.ForeColor = syntaxColors.CommentColor;
keywordProperties = new SyntaxHighlightProperties();
keywordProperties.ForeColor = syntaxColors.KeywordColor;
stringProperties = new SyntaxHighlightProperties();
stringProperties.ForeColor = syntaxColors.StringColor;
xmlCommentProperties = new SyntaxHighlightProperties();
xmlCommentProperties.ForeColor = syntaxColors.XmlCommentColor;
textProperties = new SyntaxHighlightProperties();
textProperties.ForeColor = syntaxColors.TextColor;
if (tokens == null || tokens.Count == 0)
return;
Document document = syntaxEditor.Document;
CharacterProperties cp = document.BeginUpdateCharacters(0, 1);
List<SyntaxHighlightToken> syntaxTokens = new List<SyntaxHighlightToken>(tokens.Count);
foreach (Token token in tokens)
{
HighlightCategorizedToken((CategorizedToken)token, syntaxTokens);
}
document.ApplySyntaxHighlight(syntaxTokens);
document.EndUpdateCharacters(cp);
}
void HighlightCategorizedToken(CategorizedToken token, List<SyntaxHighlightToken> syntaxTokens)
{
Color backColor = syntaxEditor.ActiveView.BackColor;
TokenCategory category = token.Category;
if (category == TokenCategory.Comment)
syntaxTokens.Add(SetTokenColor(token, commentProperties, backColor));
else if (category == TokenCategory.Keyword)
syntaxTokens.Add(SetTokenColor(token, keywordProperties, backColor));
else if (category == TokenCategory.String)
syntaxTokens.Add(SetTokenColor(token, stringProperties, backColor));
else if (category == TokenCategory.XmlComment)
syntaxTokens.Add(SetTokenColor(token, xmlCommentProperties, backColor));
else
syntaxTokens.Add(SetTokenColor(token, textProperties, backColor));
}
SyntaxHighlightToken SetTokenColor(Token token, SyntaxHighlightProperties foreColor, Color backColor)
{
if (syntaxEditor.Document.Paragraphs.Count < token.Range.Start.Line)
return null;
int paragraphStart = DocumentHelper.GetParagraphStart(syntaxEditor.Document.Paragraphs[token.Range.Start.Line - 1]);
int tokenStart = paragraphStart + token.Range.Start.Offset - 1;
if (token.Range.End.Line != token.Range.Start.Line)
paragraphStart = DocumentHelper.GetParagraphStart(syntaxEditor.Document.Paragraphs[token.Range.End.Line - 1]);
int tokenEnd = paragraphStart + token.Range.End.Offset - 1;
Debug.Assert(tokenEnd > tokenStart);
return new SyntaxHighlightToken(tokenStart, tokenEnd - tokenStart, foreColor);
}
#region #ISyntaxHighlightServiceMembers
public void Execute()
{
string newText = syntaxEditor.Text;
// Determine language by file extension.
string ext = Path.GetExtension(syntaxEditor.Options.DocumentSaveOptions.CurrentFileName);
ParserLanguageID lang_ID = ParserLanguage.FromFileExtension(ext);
// Do not parse HTML or XML.
if (lang_ID == ParserLanguageID.Html ||
lang_ID == ParserLanguageID.Xml ||
lang_ID == ParserLanguageID.None) return;
// Use DevExpress.CodeParser to parse text into tokens.
ITokenCategoryHelper tokenHelper = TokenCategoryHelperFactory.CreateHelper(lang_ID);
TokenCollection highlightTokens;
highlightTokens = tokenHelper.GetTokens(newText);
HighlightSyntax(highlightTokens);
}
public void ForceExecute()
{
Execute();
}
#endregion #ISyntaxHighlightServiceMembers
}
/// <summary>
/// This class provides colors to highlight the tokens.
/// </summary>
public class SyntaxColors
{
static Color DefaultCommentColor { get { return Color.Green; } }
static Color DefaultKeywordColor { get { return Color.Blue; } }
static Color DefaultStringColor { get { return Color.Brown; } }
static Color DefaultXmlCommentColor { get { return Color.Gray; } }
static Color DefaultTextColor { get { return Color.Black; } }
UserLookAndFeel lookAndFeel;
public Color CommentColor { get { return GetCommonColorByName(CommonSkins.SkinInformationColor, DefaultCommentColor); } }
public Color KeywordColor { get { return GetCommonColorByName(CommonSkins.SkinQuestionColor, DefaultKeywordColor); } }
public Color TextColor { get { return GetCommonColorByName(CommonColors.WindowText, DefaultTextColor); } }
public Color XmlCommentColor { get { return GetCommonColorByName(CommonColors.DisabledText, DefaultXmlCommentColor); } }
public Color StringColor { get { return GetCommonColorByName(CommonSkins.SkinWarningColor, DefaultStringColor); } }
public SyntaxColors(UserLookAndFeel lookAndFeel)
{
this.lookAndFeel = lookAndFeel;
}
Color GetCommonColorByName(string colorName, Color defaultColor)
{
Skin skin = CommonSkins.GetSkin(lookAndFeel);
if (skin == null)
return defaultColor;
return skin.Colors[colorName];
}
}
}