我有一些解析编程语言源代码的应用程序。我正在使用System.Windows.Forms.RichTextBox
作为代码编辑器。我用以下算法突出显示语言的关键词:
- 每当文本更改时,返回到最后一个单词的开头,请调用此子字符串。
- 如果关键字包含单词,则将单词的颜色设置为蓝色
否则将字的颜色设置为黑色
我正在使用RichTextBox.SelectionStarts
,RichTextBox.Select(int, int)
和RichTextBox.SelectionColor
。这工作得很好。
然而,当我按下Enter键时,光标会回到该行的大部分开头。作为源代码编辑器,我希望它遵循最后一行乞讨。我将前一行中的填充空格字符放在字符串str
中,然后richTextBox.Text = richTextBox.Text.Insert(richTextBox.SelectionStarts, str)
。当我这样做时,所有文本突出显示都已损坏,所有文本都是蓝色。
任何人都可以提出如何在不破坏突出显示的情况下追加行填充空间的建议吗?
答案 0 :(得分:2)
您可能想尝试搜索现有的代码编辑器控件,而不是创建自己的代码编辑器控件。
答案 1 :(得分:2)
不知道代码的确切问题是什么。也许你应该看看既定的解决方案。例如,Scintilla.NET是众所周知的Scintilla控件的.NET包装器。它可以使用自定义词法分析器进行扩展,因此可以满足您的需求。
答案 2 :(得分:0)
啊,我明白了。这是一个好的开始:
将其放在表单的最上方:
set text item delimiters to ";"
tell application "Mail"
set email_accounts to {}
repeat with acc in accounts
set emailAdresses to (email addresses of acc) as rich text
if enabled of acc then
set end of email_accounts to (id of acc) & ":" & emailAdresses
end if
end repeat
email_accounts
end tell
然后将其放入using System.Text.RegularExpressions;
RichTextBox
:
TextChanged
这将为C#提供 // getting keywords/functions
string keywords = @"\b(abstract|as|base|break|case|catch|checked|continue|default|delegate|do|else|event|explicit|extern|false|finally|fixed|for|foreach|goto|if|implicit|in|interface|internal|is|lock|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|switch|this|throw|true|try|typeof|unchecked|unsafe|using|virtual|volatile|while)\b";
MatchCollection keywordMatches = Regex.Matches(codeRichTextBox.Text, keywords);
// getting types/classes from the text
string types = @"\b(Console)\b";
MatchCollection typeMatches = Regex.Matches(codeRichTextBox.Text, types);
// getting comments (inline or multiline)
string comments = @"(\/\/.+?$|\/\*.+?\*\/)";
MatchCollection commentMatches = Regex.Matches(codeRichTextBox.Text, comments, RegexOptions.Multiline);
// getting strings
string strings = "\".+?\"";
MatchCollection stringMatches = Regex.Matches(codeRichTextBox.Text, strings);
string stringz = "bool|byte|char|class|const|decimal|double|enum|float|int|long|sbyte|short|static|string|struct|uint|ulong|ushort|void";
MatchCollection stringzMatchez = Regex.Matches(codeRichTextBox.Text, stringz);
// saving the original caret position + forecolor
int originalIndex = codeRichTextBox.SelectionStart;
int originalLength = codeRichTextBox.SelectionLength;
Color originalColor = Color.Black;
// MANDATORY - focuses a label before highlighting (avoids blinking)
label1.Focus();
// removes any previous highlighting (so modified words won't remain highlighted)
codeRichTextBox.SelectionStart = 0;
codeRichTextBox.SelectionLength = codeRichTextBox.Text.Length;
codeRichTextBox.SelectionColor = originalColor;
// scanning...
foreach (Match m in keywordMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Blue;
}
foreach (Match m in typeMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.DarkCyan;
}
foreach (Match m in commentMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Green;
}
foreach (Match m in stringMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Brown;
}
foreach (Match m in stringzMatchez)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Color.Purple;
}
// restoring the original colors, for further writing
codeRichTextBox.SelectionStart = originalIndex;
codeRichTextBox.SelectionLength = originalLength;
codeRichTextBox.SelectionColor = originalColor;
// giving back the focus
codeRichTextBox.Focus();
编码语法
如果要为另一种语言编码语法,请用您选择的语言替换RichTextBox
keywords
的内容和string
stringz
的内容
要更改string
或Color
中的keywords
,请查看stringz
或foreach (Match m in keywordMatches)
如果您使用的是C#以外的其他语言,请删除foreach (Match m in stringzMatchez)
,然后编辑types
和strings
希望对您有所帮助:)