C# Highlight words that end with colon
I am using ScintillaNet, a wrapper for the Scintilla control in my C# application. I am dynamically adding all words that end with : (let's just call this keyword from now). I did this by using a regex the string of keywords separated by a whitespace every time the user presses the colon key. Here is my code (in the CharAdded event):
if (e.Ch == ':')
{
string wp = string.Empty;
Regex r = new Regex(@"\b\w+[:\b]");
MatchCollection m = r.Matches(Scintilla.Text);
for (int i = 0; i < m.Count; i++)
{
wp += " " + m[i].Value.Substring(0, m[i].Value.Length - 1); // Remove the colon
}
wp = 开发者_开发百科wp.ToLower();
Scintilla.Lexing.Keywords[3] = wp;
}
Now the problem is, every time a new keyword is typed and the user presses the colon, instead of just highlighting the keyword, it will unnecessarily highlight every single keyword in the document again. So although my coding works, it's pretty bad coding, and I am wondering how I can make my code faster by only highlighting the last keyword typed. Any help and/or ideas would be appreciated.
It is not unnecessary actually, You may only check for the last word but what if user pastes a long text? You really have to check every word one by one unless you are sure that the user won't paste a text into the control or edit the middle of the text..
If your text is really long and you want to improve your processing time, then what about trying to insert a hidden tag that flags the words as "highlighted" and ignoring these in your regular expression ?
精彩评论