From 38723f87860d521db745a83775daeaf80692b879 Mon Sep 17 00:00:00 2001 From: rolanmen1 Date: Mon, 6 Aug 2012 07:12:25 +0000 Subject: [PATCH] LuaWriter. This is half of a lot of changes im making. 1)Made a list of int arrays, these will have a start position, length, color and a number that will tell if said text is gonna be bold or not. This was made to color the text only once per edit. I also made it so these values are added in order of the starting position. This will select the text from top to bottom and not randomly like it used to be. 2)Made ColorSymbols (wich is now called AddSymbols) will no longer search symbols in the textbox, instead on a copy of it that is saved in a string variable. This was made because the RichTextBox.Find method selects text in the textbox, making the whole process slower. This will be done to the ColorComments and ColorStrings aswell. --- BizHawk.MultiClient/tools/LuaWriter.cs | 112 ++++++++++++++++--------- 1 file changed, 73 insertions(+), 39 deletions(-) diff --git a/BizHawk.MultiClient/tools/LuaWriter.cs b/BizHawk.MultiClient/tools/LuaWriter.cs index f91ea4cd36..9048c94b0c 100644 --- a/BizHawk.MultiClient/tools/LuaWriter.cs +++ b/BizHawk.MultiClient/tools/LuaWriter.cs @@ -39,6 +39,8 @@ namespace BizHawk.MultiClient char[] Symbols = { '+', '-', '*', '/', '%', '^', '#', '=', '<', '>', '(', ')', '{', '}', '[', ']', ';', ':', ',', '.' }; public Regex libraryWords; + List pos = new List(); + public LuaWriter() { InitializeComponent(); @@ -84,12 +86,15 @@ namespace BizHawk.MultiClient else LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular); - ColorReservedWords(); - ColorLibraries(); - ColorSymbols(); + AddKeyWords(); + AddLibraries(); + AddSymbols(); ColorComments(); ColorStrings(); ColorLongStrings(); + + ColorText(); + LuaText.Select(selPos, selChars); ProcessingText = false; } @@ -177,22 +182,24 @@ namespace BizHawk.MultiClient return Validated; } - private void ColorSymbols() + private void AddSymbols() { + string temp = LuaText.Text; + int selection; + foreach (char mark in Symbols) { int currPos = 0; - while (LuaText.Find(mark.ToString(), currPos, RichTextBoxFinds.None) >= 0) + selection = temp.IndexOf(mark, currPos); + + while (selection >= 0) { - LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaSymbolColor); - if (Global.Config.LuaSymbolBold) - LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Bold); - else - LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular); + AddPosition(selection, 1, Global.Config.LuaSymbolColor, Global.Config.LuaSymbolBold); - currPos = LuaText.SelectionStart + 1; + currPos = selection + 1; + selection = temp.IndexOf(mark, currPos); - if (currPos == LuaText.Text.Length) + if (currPos == temp.Length) break; } } @@ -320,56 +327,83 @@ namespace BizHawk.MultiClient } } - private void ColorReservedWords() + private void AddKeyWords() { - foreach (Match keyWordMatch in keyWords.Matches(LuaText.Text)) + foreach (Match match in keyWords.Matches(LuaText.Text)) { char before = ' ', after = ' '; - if (keyWordMatch.Index > 0) - if (keyWordMatch.Index > 5 && keyWordMatch.Value != "if" && LuaText.Text.Substring(keyWordMatch.Index - 4, 4) != "else") - before = LuaText.Text[keyWordMatch.Index - 1]; + 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 (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 (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)) { - LuaText.Select(keyWordMatch.Index, keyWordMatch.Length); - LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaKeyWordColor); - if (Global.Config.LuaKeyWordBold) - LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Bold); - else - LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular); + //Validate if such text is not part of a string or comment + AddPosition(match.Index, match.Length, Global.Config.LuaKeyWordColor, Global.Config.LuaKeyWordBold); } } } - private void ColorLibraries() + private void AddPosition(int start, int lenght, int color, bool bold) { - foreach (Match libraryWordMatch in libraryWords.Matches(LuaText.Text)) + int IsBold = 0, IndexToAdd = 0; + if (bold) + IsBold = 1; + + for(int x = 0; x < pos.Count; x++) { - if (libraryWordMatch.Index >= 0) + if (start < pos[x][0]) + { + IndexToAdd = x; + break; + } + if (x == pos.Count - 1) + IndexToAdd = x + 1; + } + + pos.Insert(IndexToAdd, new int[] { start, lenght, color, IsBold }); + } + + private void ColorText() + { + foreach (int[] positions in pos) + { + LuaText.Select(positions[0], positions[1]); + LuaText.SelectionColor = Color.FromArgb(positions[2]); + if (positions[3] == 1) + LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Bold); + else + LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular); + } + + pos = new List(); + } + + private void AddLibraries() + { + foreach (Match match in libraryWords.Matches(LuaText.Text)) + { + if (match.Index >= 0) { char before = ' ', after = ' '; - if (libraryWordMatch.Index > 0) - before = LuaText.Text[libraryWordMatch.Index - 1]; + if (match.Index > 0) + before = LuaText.Text[match.Index - 1]; - if (libraryWordMatch.Index + libraryWordMatch.Length != LuaText.Text.Length) - after = LuaText.Text[libraryWordMatch.Index + libraryWordMatch.Length]; + if (match.Index + match.Length != LuaText.Text.Length) + after = LuaText.Text[match.Index + match.Length]; if (!char.IsLetterOrDigit(before)) { if (after == '.') { - LuaText.Select(libraryWordMatch.Index, libraryWordMatch.Length); - LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaLibraryColor); - if (Global.Config.LuaLibraryBold) - LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Bold); - else - LuaText.SelectionFont = new Font(LuaText.SelectionFont, FontStyle.Regular); + //Validate if such text is not part of a string or comment + AddPosition(match.Index, match.Length, Global.Config.LuaLibraryColor, Global.Config.LuaLibraryBold); } } }