未执行函数之前:
执行后参数未加数据:
执行后参数加上数据:
源代码:
/// <summary>
/// 一次性记录关键字,注释,字符串值,运算符,括号配对的位置,并设置自定义数据颜色
/// </summary>
/// <param name="CustomData">自定义数据</param>
/// <returns></returns>
/// 创建时间: 2022-12-24 最后一次修改时间:2022-12-24
DList<TextColor> Syntax::GetTextColorPos_C(const DList<Pair<StringList, Color_>> &CustomData) const
{
DList<TextColor> dtResult;
if (_code.length() <= 0) return dtResult;
const _char* p = _code.c_str();
_string sWord(_t(""), 50); //50个_char缓冲
Color_ cKeyWordColor = GetColor(SyntaxType::关键字);
Color_ cExplanatoryNote = GetColor(SyntaxType::注释);
Color_ cStringValue = GetColor(SyntaxType::字符串值);
Color_ cOS = GetColor(SyntaxType::运算符号);
for (int i = 0; i < _code.length(); ++i)
{
if (p[i] == _t('/'))
{
++i;
if (i < _code.length())
{
if (p[i] == _t('/')) //是"//"注释,查找注释结束位置 \n ,跳过注释
{
int iStart = i - 1;
++i;
bool bFind = false; //查找下一个 "\n"
while (i < _code.length())
{
if (p[i] == '\n')
{
++i;
bFind = true;
break;
}
++i;
}
if (!bFind)
{
return dtResult;
}
else
{
dtResult.Add(TextColor(iStart, i - iStart, cExplanatoryNote));
}
}
else if (p[i] == _t('*')) //是"/*"注释,查找注释结束位置 */ ,跳过注释
{
int iStart = i - 1;
++i;
bool bFind = false;
while (i + 1 < _code.length()) //查找下一个 "*/"
{
if (p[i] == '*' && p[i + 1] == '/')
{
i += 2;
bFind = true;
break;
}
++i;
}
if (!bFind)
{
return dtResult;
}
else
{
dtResult.Add(TextColor(iStart, i - iStart, cExplanatoryNote));
}
}
}
else
{
return dtResult;
}
}
else if (p[i] == _t('\"')) { //字符开始
if (i - 1 > 0 && p[i - 1] != '\\')
{
int iStart = i;
++i;
bool bFind = false; //查找下一个 "\""
while (i < _code.length())
{
if (p[i] == '\"' && p[i - 1] != '\\')
{
++i;
bFind = true;
break;
}
else if (p[i] == '\n') //当前行已结束,还未找到引号配对
{
/*
SyntaxInfo si;
si.ErrorText = _t("当前行已结束,还未找到引号配对?");
si.ErrorText += _t("错误行号为第:");
si.ErrorLineNumber = _code.GetLineIndexForCharIndex(i - 1);
si.ErrorText += _tostr(si.ErrorLineNumber + 1);
si.ErrorText += _t("行。");
//输出错误信息
PrintSyntaxCheckError(si.ErrorText);
_siList.Add(si);
*/
++i;
bFind = true;
break;
}
++i;
}
if (!bFind)
{
return dtResult;
}
else
{
dtResult.Add(TextColor(iStart, i - iStart, cStringValue));
}
}
else
{
++i;
}
}
else if (p[i] == _t('\'')) { //字符开始
if (i - 1 > 0 && p[i - 1] != '\\')
{
int iStart = i;
++i;
bool bFind = false; //查找下一个 "\'"
while (i < _code.length())
{
if (p[i] == '\'' && p[i - 1] != '\\')
{
++i;
bFind = true;
break;
}
else if (p[i] == '\n') //当前行已结束,还未找到引号配对
{
++i;
bFind = true;
break;
}
++i;
}
if (!bFind)
{
return dtResult;
}
else
{
dtResult.Add(TextColor(iStart, i - iStart, cStringValue));
}
}
else
{
++i;
}
}
else if (Syntax::C_OperationalCharacter.IndexOf(p[i]) != -1)
{
dtResult.Add(TextColor(i, 1, cOS));
}
else if (p[i] == _t('{')) //大括号查找全部文本
{
int iFlag = 0;
for (int j = i + 1; j < _code.length(); ++j) //查找配对,直到遇到分号;
{
_char cRight = p[j];
if (cRight == _t('}'))
{
if (iFlag == 0)
{
Color_ c = Color_::RedRandom();
dtResult.Add(TextColor(i, 1, c));
dtResult.Add(TextColor(j, 1, c));
break;
}
--iFlag;
}
else if (cRight == _t('{'))
{
++iFlag;
}
}
}
else if (p[i] == _t('['))
{
int iFlag = 0;
for (int j = i + 1; j < _code.length(); ++j) //查找配对,直到遇到分号;
{
_char cRight = _code[j];
if (cRight == _t(']'))
{
if (iFlag == 0)
{
Color_ c = Color_::RedRandom();
dtResult.Add(TextColor(i, 1, c));
dtResult.Add(TextColor(j, 1, c));
break;
}
--iFlag;
}
else if (cRight == _t('['))
{
++iFlag;
}
else if (ga.c_IsPunctuation(cRight)) //遇到标点符号停止
{
break;
}
}
}
else if (p[i] == _t('('))
{
int iFlag = 0;
for (int j = i + 1; j < _code.length(); ++j) //查找配对,直到遇到分号;
{
_char cRight = p[j];
if (cRight == _t(')'))
{
if (iFlag == 0)
{
Color_ c = Color_::RedRandom();
dtResult.Add(TextColor(i, 1, c));
dtResult.Add(TextColor(j, 1, c));
break;
}
--iFlag;
}
else if (cRight == _t('('))
{
++iFlag;
}
else if (cRight == _t(';'))
{
break;
}
}
}
else if (p[i] == _t('<')) //这里究竟是小于号还是尖括号
{
int iFlag = 0;
for (int j = i + 1; j < _code.length(); ++j) //查找配对,直到遇到分号;
{
_char cRight = _code[j];
if (cRight == _t('>'))
{
if (iFlag == 0)
{
Color_ c = Color_::RedRandom();
dtResult.Add(TextColor(i, 1, c));
dtResult.Add(TextColor(j, 1, c));
break;
}
--iFlag;
}
else if (cRight == _t('<'))
{
++iFlag;
}
else if (cRight == _t(';'))
{
break;
}
}
}
//开始查找,关键字,自定义类、等等
if (gs.s_Syntax_IsWordSeparator(p[i]))
{
bool bFind = false;
//查找自定义数据
auto p = CustomData.First();
while (p != null)
{
if (p->Data.first.findNoteItem(sWord) != null)
{
dtResult.Add(TextColor(i - sWord.length(), sWord.length(), p->Data.second));
bFind = true;
break;
}
p = p->Next;
}
if(!bFind) //自定义类和函数不能是关键字
{
//关键字最小长度大于2,且全是小写字母
if (sWord.length() >= 2 && sWord.IsAllLowerCaseEnglishLetter())
{
if (C_Keyword.findNoteItem(sWord) != null) //是关键字,记录位置
{
dtResult.Add(TextColor(i - sWord.length(), sWord.length(), cKeyWordColor));
}
//log::d(_getc(sWord));
}
}
sWord.Clear(); //清空
}
else
{
sWord.add(p[i]);
}
}
return dtResult;
}
其中RichTextBox中:
void RtbSyntax::TestVisibleTextColor(const DList<TextColor> dt)
{
IsLockPaint = true;
int iVisibleStart = GetCharIndexFromPosition(Point(0, 0));
int iVisibleEnd = GetCharIndexFromPosition(Point(ClientSize.Width, ClientSize.Height - 1));
_string sVisibleCode = Text->Substring(iVisibleStart, iVisibleEnd - iVisibleStart);
int iOldStart = SelectionStart;
int iOldSelectLength = SelectionLength;
//把当前光标面前的字符设置为黑色
auto p = sVisibleCode.GetWordBorder(iOldStart);
if (p.second > p.first)
{
SelectionStart = iVisibleStart + p.first;
SelectionLength = p.second - p.first + 1;
SelectionColor = Color::Black;
SelectionFont = _f;
}
for (TextColor tc : dt)
{
SelectionStart = iVisibleStart + tc.Pos;
SelectionLength = tc.Length;
SelectionColor = tc.ForeColor;
}
auto dn = _code._siList.First();
while (dn != null)
{
int n = this->GetFirstCharIndexFromLine(dn->Data.ErrorLineNumber);
this->SelectionStart = n;
this->SelectionLength = Lines[dn->Data.ErrorLineNumber]->Length;
this->SelectionColor = Color::Red;
//this->SelectionFont = gcnew Drawing::Font(this->Font->FontFamily, this->Font->Size, FontStyle::Underline);
dn = dn->Next;
}
SelectionStart = iOldStart;
SelectionLength = iOldSelectLength;
SelectionColor = Color::Black; //每次重新设置为黑色
IsLockPaint = false;
}