- SyntaxHighlighter.cs
- Syntax.cs
SyntaxHighlighter.cs calls Syntax.cs, so here they are
SyntaxHighlighter.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
namespace RLM.SnippetManager
{
public class SyntaxHighlighter
{
public bool populating = true;
private Syntax syntaxReader;
private Color commentColor = Color.Green;
private string _syntaxFile;
public SyntaxHighlighter()
{
syntaxReader = new Syntax();
syntaxReader.SyntaxFile = "SyntaxFiles/C#.syntax";
syntaxReader.OpenSyntaxFile();
}
public SyntaxHighlighter(string file)
{
_syntaxFile = file;
syntaxReader = new Syntax();
syntaxReader.SyntaxFile = _syntaxFile;
syntaxReader.OpenSyntaxFile();
}
public void OpenSyntaxFile()
{
syntaxReader.SyntaxFile = _syntaxFile;
syntaxReader.OpenSyntaxFile();
}
public string SyntaxFile
{
get { return _syntaxFile; }
set { _syntaxFile = value; }
}
public void ColorCurrentLine(RichTextBox rtb)
{
int selStart = rtb.Selectionstart;
int selLength = rtb.SelectionLength;
// find start of line
int pos = selStart;
while ((pos > 0) && (rtb.Text[pos - 1] != '\n')) pos--;
int pos2 = selStart;
while ((pos2 < rtb.Text.Length) && (rtb.Text[pos2] != '\n')) pos2++;
string str = rtb.Text.Substring(pos, pos2 - pos);
if (FindComment(str) == true)
{
rtb.Select(pos, pos2 - pos);
rtb.SelectionColor = commentColor;
}
else
{
string previousWord = "";
int count = ParseLine(str);
for (int i = 0; i < count; i++)
{
WordPosition wp = buffer[*];
// check for comment
if (wp.word == "/" && previousWord == "/")
{
// color until end of line
int commentStart = wp.pos - 1;
int commentEnd = pos2;
while (wp.word != "\n" && i < count)
{
wp = buffer[*];
i++;
}
i--;
commentEnd = pos2;
rtb.Select(commentStart + pos, commentEnd - (commentStart + pos));
rtb.SelectionColor = commentColor;
}
else
{
Color color = DetermineColor(wp.word);
rtb.Select(wp.pos + pos, wp.len);
rtb.SelectionColor = color;
}
previousWord = wp.word;
}
}
if (selStart >= 0)
rtb.Select(selStart, selLength);
}
public void ColorAllText(string s, RichTextBox rtb)
{
populating = true;
int selStart = rtb.Selectionstart;
int selLength = rtb.SelectionLength;
int count = ParseLine(s);
string previousWord = "";
for (int i = 0; i < count; i++)
{
WordPosition wp = buffer[*];
// check for comment
if (wp.word == "/" && previousWord == "/")
{
// color until end of line
int posCommentStart = wp.pos - 1;
int posCommentEnd = i;
while (wp.word != "\n" && i < count)
{
wp = buffer[*];
i++;
}
i--;
posCommentEnd = wp.pos;
rtb.Select(posCommentStart, posCommentEnd - posCommentStart);
rtb.SelectionColor = commentColor;
}
else
{
Color c = DetermineColor(wp.word);
rtb.Select(wp.pos, wp.len);
rtb.SelectionColor = c;
}
previousWord = wp.word;
// Console.WriteLine(wp.ToString());
}
if (selStart >= 0)
rtb.Select(selStart, selLength);
populating = false;
}
private struct WordPosition
{
public string word;
public int pos;
public int len;
public override string ToString()
{
string str = "Word = " + word + ", Position = " + pos + ", Length = " + len + "\n";
return str;
}
}
private WordPosition[] buffer = new WordPosition[4000];
public bool FindComment(string s)
{
string testString = s.Trim();
if ((testString.Length >= 2) && (testString[0] == '/') && (testString[1] == '/'))
return true;
return false;
}
public int ParseLine(string s)
{
buffer.Initialize();
int count = 0;
Regex reg = new Regex(@"\w+|[^A-Za-z0-9_ \f\t\v]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match match;
for (match = reg.Match(s); match.Success; match = match.NextMatch())
{
buffer[count].word = match.Value;
buffer[count].pos = match.Index;
buffer[count].len = match.Length;
count++;
}
return count;
}
public Color DetermineColor(string s)
{
Color theColor = Color.Black;
if (syntaxReader.IsOperator(s))
{
theColor = Color.DarkRed;
}
if (syntaxReader.IsFunction(s))
{
theColor = Color.Blue;
}
if (syntaxReader.IsKeyword(s))
{
theColor = Color.Blue;
}
if (syntaxReader.IsDirective(s))
{
theColor = Color.Gray;
}
if (syntaxReader.IsDataTypes(s))
{
theColor = Color.Navy;
}
return theColor;
}
}
}
Syntax.cs
using System;
using System.Collections;
using System.IO;
namespace RLM.SnippetManager
{
class Syntax
{
private string _syntaxFile;
private ArrayList _functions = new ArrayList();
private ArrayList _keywords = new ArrayList();
private ArrayList _directives = new ArrayList();
private ArrayList _dataTypes = new ArrayList();
private ArrayList _operators = new ArrayList();
private ArrayList _comments = new ArrayList();
public string SyntaxFile
{
get { return _syntaxFile; }
set { _syntaxFile = value; }
}
public Syntax()
{
//
// TODO: Add constructor logic here
//
}
public void OpenSyntaxFile()
{
FileStream stream = new FileStream(_syntaxFile, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(stream);
_syntaxFile = reader.ReadToEnd();
reader.Close();
stream.Close();
Fill();
}
public void Fill()
{
StringReader reader = new StringReader(_syntaxFile);
string next = reader.ReadLine();
next = next.Trim();
//find the keywords header
while (next != null)
{
if (next == "[KEYWORDS]")
{
//read all keywords into our ArrayList
next = reader.ReadLine();
if (next != null)
next = next.Trim();
while (next != null && next[0] != '[')
{
_keywords.Add(next);
next = "";
while (next != null && next == "")
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
}
//find the functions header
if (next == "[FUNCTIONS]")
{
//read all directives into our ArrayList
next = reader.ReadLine();
if (next != null)
next = next.Trim();
while (next != null && next[0] != '[')
{
_functions.Add(next);
next = "";
while (next != null && next == "")
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
}
//find the directives header
if (next == "[DIRECTIVES]")
{
//read all directives into our ArrayList
next = reader.ReadLine();
if (next != null)
next = next.Trim();
while (next != null && next[0] != '[')
{
_directives.Add(next);
next = "";
while (next != null && next == "")
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
}
//find the operators header
if (next == "[OPERATORS]")
{
//read all operators into our ArrayList
next = reader.ReadLine();
if (next != null)
next = next.Trim();
while (next != null && next[0] != '[')
{
_operators.Add(next);
next = "";
while (next != null && next == "")
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
}
//find the data types header
if (next == "[DATATYPES]")
{
//read all comments into our ArrayList
next = reader.ReadLine();
if (next != null)
next = next.Trim();
while (next != null && next[0] != '[')
{
_dataTypes.Add(next);
next = "";
while (next != null && next == "")
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
}
//find the comments header
if (next == "[COMMENTS]")
{
//read all comments into our ArrayList
next = reader.ReadLine();
if (next != null)
next = next.Trim();
while (next != null && next[0] != '[')
{
_comments.Add(next);
next = "";
while (next != null && next == "")
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
}
if (next != null && next.Length > 0 && next[0] == '[')
{
}
else
{
next = reader.ReadLine();
if (next != null)
next = next.Trim();
}
}
//sort our ArrayList's
_functions.Sort();
_keywords.Sort();
_directives.Sort();
_dataTypes.Sort();
_operators.Sort();
_comments.Sort();
}
public bool IsKeyword(string s)
{
int index = _keywords.BinarySearch(s);
if (index >= 0) return true;
return false;
}
public bool IsFunction(string s)
{
int index = _functions.BinarySearch(s);
if (index >= 0) return true;
return false;
}
public bool IsDirective(string s)
{
int index = _directives.BinarySearch(s);
if (index >= 0) return true;
return false;
}
public bool IsOperator(string s)
{
int index = _operators.BinarySearch(s);
if (index >= 0) return true;
return false;
}
public bool IsDataTypes(string s)
{
int index = _dataTypes.BinarySearch(s);
if (index >= 0) return true;
return false;
}
public bool IsComment(string s)
{
int index = _comments.BinarySearch(s);
if (index >= 0)
return true;
return false;
}
}
}
To prevent the RichTextBox from flickering when the syntax is highlighted (as you type) I eat the Pain message in my custom RichTextBox control with this
const short WM_PAINT = 0x00f;
public static bool doPaint = true;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
//eat the Paint message to prevent seeing the flickering
//when we select the text to set the color
if (m.Msg == WM_PAINT)
{
if (_Paint)
base.WndProc(ref m);
else
m.Result = IntPtr.Zero;
}
else
base.WndProc (ref m);
}
Ill post more teasers as I go along
EDIT: Where you see buffer[*] is supposed to be buffer[i], but it was messing with the formatting of the post

New Topic/Question
Reply



MultiQuote











|