-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxHighlighting.vb
98 lines (83 loc) · 5.01 KB
/
SyntaxHighlighting.vb
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
Imports System.Collections.Generic
Imports DevExpress.XtraRichEdit.Services
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.CodeParser
Imports System.Drawing
Imports DevExpress.XtraRichEdit
Namespace WindowsFormsApplication1
Public Class HTMLSyntaxHighlightService
Implements ISyntaxHighlightService
Private ReadOnly syntaxEditor As RichEditControl
Private TokensMapping As Dictionary(Of TokenCategory, SyntaxHighlightProperties) = New Dictionary(Of TokenCategory, SyntaxHighlightProperties)()
Private textProperties As SyntaxHighlightProperties
Private Sub AddTokensMapping(ByVal category As TokenCategory, ByVal foreColor As Color)
Dim tokenProperties As SyntaxHighlightProperties = New SyntaxHighlightProperties()
tokenProperties.ForeColor = foreColor
TokensMapping.Add(category, tokenProperties)
End Sub
Private Function GetTokensMapping(ByVal category As TokenCategory) As SyntaxHighlightProperties
If TokensMapping.ContainsKey(category) Then
Return TokensMapping(category)
Else
Return textProperties
End If
End Function
Public Sub New(ByVal syntaxEditor As RichEditControl)
Me.syntaxEditor = syntaxEditor
End Sub
Private Sub HighlightSyntax(ByVal tokens As TokenCollection)
If TokensMapping.Count = 0 Then
AddTokensMapping(TokenCategory.HtmlAttributeName, Color.Red)
AddTokensMapping(TokenCategory.HtmlAttributeValue, Color.Blue)
AddTokensMapping(TokenCategory.HtmlComment, Color.Green)
AddTokensMapping(TokenCategory.HtmlElementName, Color.Brown)
AddTokensMapping(TokenCategory.HtmlEntity, Color.Green)
AddTokensMapping(TokenCategory.HtmlOperator, Color.Blue)
AddTokensMapping(TokenCategory.HtmlServerSideScript, Color.Gray)
AddTokensMapping(TokenCategory.HtmlString, Color.Black)
AddTokensMapping(TokenCategory.HtmlTagDelimiter, Color.Blue)
AddTokensMapping(TokenCategory.CssComment, Color.Green)
AddTokensMapping(TokenCategory.CssKeyword, Color.Red)
AddTokensMapping(TokenCategory.CssPropertyName, Color.Red)
AddTokensMapping(TokenCategory.CssPropertyValue, Color.Blue)
AddTokensMapping(TokenCategory.CssSelector, Color.Brown)
AddTokensMapping(TokenCategory.CssStringValue, Color.Blue)
textProperties = New SyntaxHighlightProperties()
textProperties.ForeColor = Color.Black
End If
If tokens Is Nothing OrElse tokens.Count = 0 Then Return
Dim document As Document = syntaxEditor.Document
Dim syntaxTokens As List(Of SyntaxHighlightToken) = New List(Of SyntaxHighlightToken)(tokens.Count)
For Each token As Token In tokens
HighlightCategorizedToken(CType(token, CategorizedToken), syntaxTokens)
Next
document.ApplySyntaxHighlight(syntaxTokens)
End Sub
Private Sub HighlightCategorizedToken(ByVal token As CategorizedToken, ByVal syntaxTokens As List(Of SyntaxHighlightToken))
Dim backColor As Color = syntaxEditor.ActiveView.BackColor
Dim category As TokenCategory = token.Category
syntaxTokens.Add(SetTokenColor(token, GetTokensMapping(category), backColor))
End Sub
Private Function SetTokenColor(ByVal token As Token, ByVal foreColor As SyntaxHighlightProperties, ByVal backColor As Color) As SyntaxHighlightToken
If syntaxEditor.Document.Paragraphs.Count < token.Range.Start.Line Then Return Nothing
Dim paragraphStart As Integer = DocumentHelper.GetParagraphStart(syntaxEditor.Document.Paragraphs(token.Range.Start.Line - 1))
Dim tokenStart As Integer = paragraphStart + token.Range.Start.Offset - 1
If token.Range.End.Line <> token.Range.Start.Line Then paragraphStart = DocumentHelper.GetParagraphStart(syntaxEditor.Document.Paragraphs(token.Range.End.Line - 1))
Dim tokenEnd As Integer = paragraphStart + token.Range.End.Offset - 1
Return New SyntaxHighlightToken(tokenStart, tokenEnd - tokenStart, foreColor)
End Function
'#Region "#ISyntaxHighlightServiceMembers"
Public Sub Execute() Implements ISyntaxHighlightService.Execute
Dim newText As String = syntaxEditor.Text
' Use DevExpress.CodeParser to parse text into tokens.
Dim tokenHelper As ITokenCategoryHelper = TokenCategoryHelperFactory.CreateHelper(ParserLanguageID.Html)
Dim highlightTokens As TokenCollection
highlightTokens = tokenHelper.GetTokens(newText)
HighlightSyntax(highlightTokens)
End Sub
Public Sub ForceExecute() Implements ISyntaxHighlightService.ForceExecute
Execute()
End Sub
'#End Region ' #ISyntaxHighlightServiceMembers
End Class
End Namespace