|
-
May 1st, 2013, 01:48 PM
#1
Syntax Highlighter
I've been around the forums now for quite a while, and something I see come up quite regularly is someone wanting to develop an IDE. Well an IDE is very hard work, take visual studio's for example. Visual Studio's has intellisense, syntax highlighting, compiler... the list goes on! Well I've taken it upon my self to at least knock out one portion for those wanting to take on the challenge and that's the syntax highlighter.
The pro's:
- Highlights syntax in a pure managed .Net way!
The con's:
- It's not complete, I will constantly work on it
- Sort of a memory hog!
The way I do it is first set up a textfile and have it in a Token{delimiter}Color format. So in my example I post the text file I use looks like this:
<html>,Goldenrod
</html>,Goldenrod
<head>,Goldenrod
</head>,Goldenrod
<body>,Goldenrod
</body>,Goldenrod
<title>,CornflowerBlue
</title>,CornflowerBlue
<h1>,CornflowerBlue
</h1>,CornflowerBlue
<h2>,CornflowerBlue
</h2>,CornflowerBlue
<h3>,CornflowerBlue
</h3>,CornflowerBlue
<h4>,CornflowerBlue
</h4>,CornflowerBlue
<h5>,CornflowerBlue
</h5>,CornflowerBlue
<h6>,CornflowerBlue
</h6>,CornflowerBlue
<p>,CornflowerBlue
</p>,CornflowerBlue
<br/>,BlueViolet
<hr/>,BlueViolet
<div>,CornflowerBlue
</div>,CornflowerBlue
<ol>,CornflowerBlue
</ol>,CornflowerBlue
<li>,CornflowerBlue
</li>,CornflowerBlue
What I do next is declare a Dictionary(Of String, Color). Then fill that dictionary using this function:
Code:
Private Function txt_to_dictionary(ByVal content As String, ByVal delimiter As String) As Dictionary(Of String, Color)
'Declare a new instance of a dictionary
Dim dic As New Dictionary(Of String, Color)
'Split up the content by the newline b/c in our textfile we have:
'value,color
'value,color
'etc.
Dim str() As String = content.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
'Return nothing if there's nothing in the content
If str.Count = -1 Then
Return Nothing
End If
'Iterate through each row, or line
For rows As Integer = 0 To str.Count - 1
'Declare the token and color
'We get the token by getting the substring from 0 to where the delimiter is
'We get the color by getting the substring from where the delimiter + 1 is to the end
Dim token As String = str(rows).Substring(0, str(rows).IndexOf(delimiter))
Dim color As Color = color.FromName(str(rows).Substring(str(rows).IndexOf(delimiter) + 1))
'Add it to our dictionary
dic.Add(token, color)
Next
Return dic
End Function
The reason I have ByVal content As String, instead of say ByVal filename As String and use a streamreader to read the contents of the text file is because when I use textfiles, I generally add them in My.Resources and that's simply not necessary. Finally in the text_changed event of a RichTextBox I add:
Code:
'Get the current location of the cursor
Dim cursor_location As Integer = RichTextBox1.SelectionStart
'Get the line we're editing
Dim current_line As Integer = RichTextBox1.GetLineFromCharIndex(cursor_location)
'Iterate through each token
For Each token As String In token_dictionary.Keys
'If the line contains the token
If RichTextBox1.Lines(current_line).Contains(token) Then
'Select and color it
RichTextBox1.Select(RichTextBox1.Find(token), token.Length)
RichTextBox1.SelectionColor = token_dictionary(token)
End If
Next
'Finally set the selection color back to black and set the cursor wher it was
RichTextBox1.SelectionStart = cursor_location
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionColor = Color.Black
All in all, the final code looks like this:
Code:
'Come on now, everyone needs Option Strict/Explicit ON!
Option Strict On
Option Explicit On
Public Class Form1
Private token_dictionary As Dictionary(Of String, Color)
Private Function txt_to_dictionary(ByVal content As String, ByVal delimiter As String) As Dictionary(Of String, Color)
'Declare a new instance of a dictionary
Dim dic As New Dictionary(Of String, Color)
'Split up the content by the newline b/c in our textfile we have:
'value,color
'value,color
'etc.
Dim str() As String = content.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
'Return nothing if there's nothing in the content
If str.Count = -1 Then
Return Nothing
End If
'Iterate through each row, or line
For rows As Integer = 0 To str.Count - 1
'Declare the token and color
'We get the token by getting the substring from 0 to where the delimiter is
'We get the color by getting the substring from where the delimiter + 1 is to the end
Dim token As String = str(rows).Substring(0, str(rows).IndexOf(delimiter))
Dim color As Color = color.FromName(str(rows).Substring(str(rows).IndexOf(delimiter) + 1))
'Add it to our dictionary
dic.Add(token, color)
Next
Return dic
End Function
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
token_dictionary = txt_to_dictionary(My.Resources.Tokens, ",")
End Sub
Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
'Get the current location of the cursor
Dim cursor_location As Integer = RichTextBox1.SelectionStart
'Get the line we're editing
Dim current_line As Integer = RichTextBox1.GetLineFromCharIndex(cursor_location)
'Iterate through each token
For Each token As String In token_dictionary.Keys
'If the line contains the token
If RichTextBox1.Lines(current_line).Contains(token) Then
'Select and color it
RichTextBox1.Select(RichTextBox1.Find(token), token.Length)
RichTextBox1.SelectionColor = token_dictionary(token)
End If
Next
'Finally set the selection color back to black and set the cursor wher it was
RichTextBox1.SelectionStart = cursor_location
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionColor = Color.Black
End Sub
End Class
To go back to the con's, one thing I haven't figured out yet is for strings. In most cases strings are defined by the text inside double quotation marks: "This is a string!". The problem with my method is that I haven't figured out how to highlight both the double quote and everything inside of it yet. But like I said this is a work in progress, but a good jumping point. Here is an image of the work in progress:
Last edited by dday9; May 23rd, 2013 at 11:18 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|