BizHawk/BizHawk.MultiClient/tools/LuaWriter.cs

212 lines
6.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace BizHawk.MultiClient
{
public partial class LuaWriter : Form
{
2012-07-21 04:17:49 +00:00
public Regex keyWords = new Regex("and|break|do|else|if|end|false|for|function|in|local|nil|not|or|repeat|return|then|true|until|while|elseif");
public LuaWriter()
{
InitializeComponent();
}
private void timer_Tick(object sender, EventArgs e)
{
int selPos = LuaText.SelectionStart;
int selChars = LuaText.SelectedText.Length;
2012-07-21 04:17:49 +00:00
LuaText.SelectAll();
LuaText.SelectionColor = Color.Black;
ColorReservedWords();
ColorComments();
ColorStrings();
2012-07-21 04:17:49 +00:00
ColorCharacters();
LuaText.Select(selPos, selChars);
}
2012-07-21 04:17:49 +00:00
private void ColorCharacters()
{
int firstApostrophe = LuaText.Find("'", 0);
while (firstApostrophe >= 0)
{
if (LuaText.SelectionColor != Color.FromArgb(Global.Config.LuaCommentColor))
2012-07-21 04:17:49 +00:00
{
int opening = firstApostrophe;
int endLine;
if (LuaText.Lines[LuaText.GetLineFromCharIndex(LuaText.GetFirstCharIndexOfCurrentLine())] == LuaText.Lines[LuaText.Lines.Length - 1])
endLine = LuaText.Text.Length;
else
endLine = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(opening) + 1) - 1;
int ending = LuaText.Find("'", opening + 1, endLine, RichTextBoxFinds.None);
if (ending > 0)
while (ending > 0)
{
if (LuaText.Text[ending - 1] == '\\')
ending++;
else
break;
if (ending > endLine)
{
ending = endLine;
2012-07-21 04:17:49 +00:00
break;
}
ending = LuaText.Find("'", ending, endLine, RichTextBoxFinds.None);
}
else
ending = endLine;
2012-07-21 04:17:49 +00:00
if (opening != ending)
{
LuaText.Select(opening, ending - opening + 1);
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaStringColor);
2012-07-21 04:17:49 +00:00
if (ending != LuaText.Text.Length - 1)
firstApostrophe = LuaText.Find("'", ending + 1, RichTextBoxFinds.None);
else
break;
}
else
if (endLine != LuaText.Text.Length)
firstApostrophe = LuaText.Find("'", endLine + 1, RichTextBoxFinds.None);
else
break;
}
else
{
if (LuaText.SelectionStart == LuaText.Text.Length - 1)
break;
else
firstApostrophe = LuaText.Find("'", LuaText.SelectionStart + 1, RichTextBoxFinds.None);
}
}
}
private void ColorStrings()
{
2012-07-21 04:17:49 +00:00
int firstQuotation = LuaText.Find("\"", 0);
while (firstQuotation >= 0)
{
if (LuaText.SelectionColor != Color.FromArgb(Global.Config.LuaCommentColor))
2012-07-21 04:17:49 +00:00
{
int opening = firstQuotation;
int endLine;
if (LuaText.Lines[LuaText.GetLineFromCharIndex(LuaText.GetFirstCharIndexOfCurrentLine())] == LuaText.Lines[LuaText.Lines.Length - 1])
endLine = LuaText.Text.Length;
else
endLine = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(opening) + 1) - 1;
int ending = LuaText.Find("\"", opening + 1, endLine, RichTextBoxFinds.None);
if (ending > 0)
while (ending > 0)
{
if (LuaText.Text[ending - 1] == '\\')
ending++;
else
break;
if (ending > endLine)
{
ending = endLine;
2012-07-21 04:17:49 +00:00
break;
}
ending = LuaText.Find("\"", ending, endLine, RichTextBoxFinds.None);
}
else
ending = endLine;
2012-07-21 04:17:49 +00:00
if (opening != ending)
{
LuaText.Select(opening, ending - opening + 1);
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaStringColor);
2012-07-21 04:17:49 +00:00
if (ending != LuaText.Text.Length - 1)
firstQuotation = LuaText.Find("\"", ending + 1, RichTextBoxFinds.None);
else
break;
}
else
if (endLine != LuaText.Text.Length)
firstQuotation = LuaText.Find("\"", endLine + 1, RichTextBoxFinds.None);
else
break;
}
else
{
if (LuaText.SelectionStart == LuaText.Text.Length - 1)
break;
else
firstQuotation = LuaText.Find("\"", LuaText.SelectionStart + 1, RichTextBoxFinds.None);
}
}
}
private void ColorComments()
{
foreach (Match CommentMatch in new Regex("--").Matches(LuaText.Text))
{
int endComment;
if (LuaText.Text.Substring(CommentMatch.Index, 4) == "--[[")
{
if (LuaText.Find("]]", RichTextBoxFinds.MatchCase) > 0)
endComment = LuaText.SelectionStart - CommentMatch.Index + 2;
else
endComment = LuaText.Text.Length;
LuaText.Select(CommentMatch.Index, endComment);
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaCommentColor);
}
else
{
if (LuaText.GetLineFromCharIndex(CommentMatch.Index) + 1 == LuaText.Lines.Count())
endComment = LuaText.Text.Length - CommentMatch.Index;
else
endComment = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(CommentMatch.Index) + 1) - CommentMatch.Index;
LuaText.Select(CommentMatch.Index, endComment);
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaCommentColor);
}
}
}
private void ColorReservedWords()
{
foreach (Match keyWordMatch in keyWords.Matches(LuaText.Text))
{
char before = ' ', after = ' ';
if (keyWordMatch.Index > 0)
if(keyWordMatch.Value != "if" && LuaText.Text.Substring(keyWordMatch.Index - 4, 4) != "else")
before = LuaText.Text[keyWordMatch.Index - 1];
if (keyWordMatch.Index + keyWordMatch.Length != LuaText.Text.Length)
if (keyWordMatch.Value != "else" && LuaText.Text.Substring(keyWordMatch.Index, 2) != "if")
after = LuaText.Text[keyWordMatch.Index + keyWordMatch.Length];
if (!char.IsLetterOrDigit(before) && !char.IsLetterOrDigit(after))
{
LuaText.Select(keyWordMatch.Index, keyWordMatch.Length);
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaKeyWordColor);
}
}
}
}
}