LuaWriter. I finally finished coloring Strings and Comments correctly.

This commit is contained in:
rolanmen1 2012-08-11 05:15:56 +00:00
parent 9cb825418f
commit c5577b7850
1 changed files with 167 additions and 148 deletions

View File

@ -86,12 +86,10 @@ namespace BizHawk.MultiClient
else else
LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular); LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular);
AddCommentsAndStrings();
AddKeyWords(); AddKeyWords();
AddLibraries(); AddLibraries();
AddSymbols(); AddSymbols();
AddComments();
AddStrings();
AddLongStrings();
ColorText(); ColorText();
@ -101,56 +99,78 @@ namespace BizHawk.MultiClient
LuaText.Refresh(); LuaText.Refresh();
} }
private void AddLongStrings() private void AddCommentsAndStrings()
{ {
string temp = LuaText.Text; string temp = LuaText.Text;
int firstBracket = temp.IndexOf("["), secondBracket, ending; int comment, longcomment, quote, apos, longstring, position = 0;
while (firstBracket >= 0)
while (position <= temp.Length)
{ {
ending = 0; comment = temp.IndexOf("--", position);
if (firstBracket > 1 && temp.Substring(firstBracket - 2, 2) == "--") longcomment = temp.IndexOf("--[[", position);
{ quote = temp.IndexOf('"', position);
firstBracket = temp.IndexOf("[", firstBracket + 1, temp.Length - firstBracket - 1); apos = temp.IndexOf('\'', position);
if (firstBracket == temp.Length - 1) longstring = temp.IndexOf("[", position);
break;
}
else if (firstBracket != temp.Length - 1)
{
secondBracket = temp.IndexOf("[", firstBracket + 1, temp.Length - firstBracket - 1);
if (secondBracket >= 0 && IsLongString(temp.Substring(firstBracket, secondBracket - firstBracket + 1)))
{
if (secondBracket + 1 == temp.Length)
ending = temp.Length - 1;
else
{
string tempBracket = GetLongStringClosingBracket(temp.Substring(firstBracket, secondBracket - firstBracket + 1));
ending = temp.IndexOf(tempBracket, secondBracket + 1);
if (ending < 0)
ending = temp.Length;
else
ending += tempBracket.Length - 1;
}
AddPosition(firstBracket, ending - firstBracket + 1, Global.Config.LuaStringColor, Global.Config.LuaStringBold); int secondBracket = temp.IndexOf('[', longstring + 1);
if (secondBracket >= 0)
if (ending < temp.Length - 1) while (longstring >= 0 && secondBracket >= 0 && longstring != longcomment - 2 && !IsLongString(temp.Substring(longstring, secondBracket - longstring)))
firstBracket = temp.IndexOf("[", ending + 1, temp.Length - ending - 1);
else
break;
}
else
{ {
if (secondBracket >= 0 && secondBracket != temp.Length - 1) longstring = temp.IndexOf('[', longstring + 1);
firstBracket = temp.IndexOf("[", secondBracket, temp.Length - secondBracket - 1); if (longstring >= 0)
else secondBracket = temp.IndexOf('[', longstring + 1);
break; if (secondBracket == -1)
longstring = -1;
} }
}
else else
break; longstring = -1;
if (comment >= 0 && (comment < quote || quote == -1) && (comment < apos || apos == -1) && (comment < longstring || longstring == -1))
{
if (comment < longcomment || longcomment == -1)
{
position = AddComment(comment);
}
else if (comment >= longcomment && longcomment >= 0)
{
position = AddMultiLineComment(longcomment);
}
}
else if (quote >= 0 && (quote < apos || apos == -1) && (quote < longstring || longstring == -1))
{
position = AddString(quote, '"');
}
else if (apos >= 0 && (apos < longstring || longstring == -1))
{
position = AddString(apos, '\'');
}
else if (longstring >= 0)
{
position = AddLongString(longstring, secondBracket);
}
position++;
} }
} }
private int AddLongString(int startPos, int secondBracket)
{
int ending = 0;
if (startPos != LuaText.Text.Length - 1)
{
string tempBracket = GetLongStringClosingBracket(LuaText.Text.Substring(startPos, secondBracket - startPos + 1));
ending = LuaText.Text.IndexOf(tempBracket, secondBracket + 1);
if (ending < 0)
ending = LuaText.Text.Length;
else
ending += tempBracket.Length - 1;
}
AddPosition(startPos, ending - startPos + 1, Global.Config.LuaStringColor, Global.Config.LuaStringBold, 1);
return ending;
}
private string GetLongStringClosingBracket(string openingBracket) private string GetLongStringClosingBracket(string openingBracket)
{ {
string closingBrackets = "]"; string closingBrackets = "]";
@ -192,8 +212,8 @@ namespace BizHawk.MultiClient
while (selection >= 0) while (selection >= 0)
{ {
//Validate if such text is not part of a string or comment if(!IsThisPartOfStringOrComment(selection))
AddPosition(selection, 1, Global.Config.LuaSymbolColor, Global.Config.LuaSymbolBold); AddPosition(selection, 1, Global.Config.LuaSymbolColor, Global.Config.LuaSymbolBold, 0);
currPos = selection + 1; currPos = selection + 1;
selection = temp.IndexOf(mark, currPos); selection = temp.IndexOf(mark, currPos);
@ -204,71 +224,49 @@ namespace BizHawk.MultiClient
} }
} }
private void AddStrings() private int AddString(int startPos, char mark)
{ {
string temp = LuaText.Text; int ending, endLine;
int firstMark, opening, ending, endLine;
char[] chars = { '"', '\'' }; if (LuaText.GetLineFromCharIndex(startPos) + 1 == LuaText.Lines.Count())
foreach (char mark in chars) endLine = LuaText.Text.Length - 1;
else
endLine = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(startPos) + 1) - 1;
ending = 0;
if (startPos != LuaText.Text.Length - 1)
{ {
firstMark = temp.IndexOf(mark.ToString()); if (startPos + 1 != endLine)
while (firstMark >= 0)
{ {
if (LuaText.SelectionColor.ToArgb() != Global.Config.LuaCommentColor) ending = LuaText.Text.IndexOf(mark, startPos + 1, endLine - startPos + 1);
if (ending > 0)
{ {
opening = firstMark; while (ending > 0)
if (LuaText.GetLineFromCharIndex(opening) + 1 == LuaText.Lines.Count())
endLine = temp.Length - 1;
else
endLine = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(opening) + 1) - 1;
ending = 0;
if (opening != temp.Length - 1)
{ {
if (opening + 1 != endLine) if (!IsThisPartOfTheString(LuaText.Text.Substring(startPos, ending - startPos + 1)))
{
ending = temp.IndexOf(mark, opening + 1, endLine - opening + 1);
if (ending > 0)
{
while (ending > 0)
{
if (!IsThisPartOfTheString(temp.Substring(opening, ending - opening + 1)))
break;
else
ending++;
ending = temp.IndexOf(mark, ending, endLine - opening + 1);
}
}
else
ending = endLine;
}
else
ending = endLine;
}
else
ending = endLine;
if (opening != temp.Length)
{
AddPosition(opening, ending - opening + 1, Global.Config.LuaStringColor, Global.Config.LuaStringBold);
if (ending >= temp.Length)
ending++;
else
break; break;
else
ending++;
firstMark = temp.IndexOf(mark, ending + 1); ending = LuaText.Text.IndexOf(mark, ending, endLine - startPos + 1);
} }
else
break;
} }
else else
firstMark = temp.IndexOf(mark, firstMark + 1); ending = endLine;
} }
else
ending = endLine;
} }
else
ending = endLine;
if (startPos != LuaText.Text.Length)
{
AddPosition(startPos, ending - startPos + 1, Global.Config.LuaStringColor, Global.Config.LuaStringBold, 1);
}
return ending;
} }
private bool IsThisPartOfTheString(string wholestring) private bool IsThisPartOfTheString(string wholestring)
@ -285,58 +283,77 @@ namespace BizHawk.MultiClient
return !(ammount % 2 == 0); return !(ammount % 2 == 0);
} }
private void AddComments() private int AddComment(int startPos)
{ {
string temp = LuaText.Text; int endComment;
foreach (Match match in new Regex("--").Matches(temp))
{
int selection, endComment;
if (match.Index + 4 < temp.Length && temp.Substring(match.Index, 4) == "--[[") if (LuaText.GetLineFromCharIndex(startPos) + 1 == LuaText.Lines.Count())
{ endComment = LuaText.Text.Length - startPos;
selection = temp.IndexOf("]]"); else
if (selection > 0) endComment = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(startPos) + 1) - 1;
endComment = selection - match.Index + 2;
else
endComment = temp.Length;
AddPosition(match.Index, endComment, Global.Config.LuaCommentColor, Global.Config.LuaCommentBold); AddPosition(startPos, endComment - startPos, Global.Config.LuaCommentColor, Global.Config.LuaCommentBold, 1);
}
else
{
if (LuaText.GetLineFromCharIndex(match.Index) + 1 == LuaText.Lines.Count())
endComment = temp.Length - match.Index;
else
endComment = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(match.Index) + 1) - match.Index;
AddPosition(match.Index, endComment, Global.Config.LuaCommentColor, Global.Config.LuaCommentBold); return endComment;
} }
}
private int AddMultiLineComment(int startPos)
{
int selection, endComment;
selection = LuaText.Text.IndexOf("]]");
if (selection > 0)
endComment = selection - startPos + 2;
else
endComment = LuaText.Text.Length;
AddPosition(startPos, endComment, Global.Config.LuaCommentColor, Global.Config.LuaCommentBold, 1);
return endComment;
} }
private void AddKeyWords() private void AddKeyWords()
{ {
foreach (Match match in keyWords.Matches(LuaText.Text)) foreach (Match match in keyWords.Matches(LuaText.Text))
{ {
char before = ' ', after = ' '; if (!IsThisPartOfStringOrComment(match.Index))
if (match.Index > 0)
if (match.Index > 5 && match.Value != "if" && LuaText.Text.Substring(match.Index - 4, 4) != "else")
before = LuaText.Text[match.Index - 1];
if (match.Index + match.Length != LuaText.Text.Length)
if (match.Value != "else" && LuaText.Text.Substring(match.Index, 2) != "if")
after = LuaText.Text[match.Index + match.Length];
if (!char.IsLetterOrDigit(before) && !char.IsLetterOrDigit(after))
{ {
//Validate if such text is not part of a string or comment char before = ' ', after = ' ';
AddPosition(match.Index, match.Length, Global.Config.LuaKeyWordColor, Global.Config.LuaKeyWordBold);
if (match.Index > 0)
if (match.Index > 5 && match.Value != "if" && LuaText.Text.Substring(match.Index - 4, 4) != "else")
before = LuaText.Text[match.Index - 1];
if (match.Index + match.Length != LuaText.Text.Length)
if (match.Value != "else" && LuaText.Text.Substring(match.Index, 2) != "if")
after = LuaText.Text[match.Index + match.Length];
if (!char.IsLetterOrDigit(before) && !char.IsLetterOrDigit(after))
{
AddPosition(match.Index, match.Length, Global.Config.LuaKeyWordColor, Global.Config.LuaKeyWordBold, 1);
}
} }
} }
} }
private void AddPosition(int start, int lenght, int color, bool bold) private bool IsThisPartOfStringOrComment(int startPos)
{
bool Validated = false;
foreach (int[] position in pos)
{
if (position[4] == 1)
if (position[0] <= startPos && position[0] + position[1] > startPos)
{
Validated = true;
break;
}
}
return Validated;
}
private void AddPosition(int start, int lenght, int color, bool bold, int iscommentorstring)
{ {
int IsBold = 0, IndexToAdd = 0; int IsBold = 0, IndexToAdd = 0;
if (bold) if (bold)
@ -353,7 +370,7 @@ namespace BizHawk.MultiClient
IndexToAdd = x + 1; IndexToAdd = x + 1;
} }
pos.Insert(IndexToAdd, new int[] { start, lenght, color, IsBold }); pos.Insert(IndexToAdd, new int[] { start, lenght, color, IsBold, iscommentorstring });
} }
private void ColorText() private void ColorText()
@ -375,22 +392,24 @@ namespace BizHawk.MultiClient
{ {
foreach (Match match in libraryWords.Matches(LuaText.Text)) foreach (Match match in libraryWords.Matches(LuaText.Text))
{ {
if (match.Index >= 0) if (!IsThisPartOfStringOrComment(match.Index))
{ {
char before = ' ', after = ' '; if (match.Index >= 0)
if (match.Index > 0)
before = LuaText.Text[match.Index - 1];
if (match.Index + match.Length != LuaText.Text.Length)
after = LuaText.Text[match.Index + match.Length];
if (!char.IsLetterOrDigit(before))
{ {
if (after == '.') char before = ' ', after = ' ';
if (match.Index > 0)
before = LuaText.Text[match.Index - 1];
if (match.Index + match.Length != LuaText.Text.Length)
after = LuaText.Text[match.Index + match.Length];
if (!char.IsLetterOrDigit(before))
{ {
//Validate if such text is not part of a string or comment if (after == '.')
AddPosition(match.Index, match.Length, Global.Config.LuaLibraryColor, Global.Config.LuaLibraryBold); {
AddPosition(match.Index, match.Length, Global.Config.LuaLibraryColor, Global.Config.LuaLibraryBold, 0);
}
} }
} }
} }