diff --git a/EditorContext/JSONEditorContext.cs b/EditorContext/JSONEditorContext.cs index ef0a2db..a89bbef 100644 --- a/EditorContext/JSONEditorContext.cs +++ b/EditorContext/JSONEditorContext.cs @@ -10,7 +10,7 @@ namespace psedit { public class JSONEditorContext : EditorContext { - private Dictionary tokens; + private List parsedTokens; private List parsedErrors = new List(); public JSONEditorContext(int TabWidth) { @@ -62,9 +62,9 @@ public Terminal.Gui.Color GetColor(JsonToken token) return textColor; } - public Dictionary ParseJsonToken(string text, List> Runes) + public List ParseJsonToken(string text, List> Runes) { - List resultList = new List(); + List returnValue = new List(); Dictionary returnList = new Dictionary(); JsonTextReader reader = new JsonTextReader(new StringReader(text)); @@ -90,7 +90,7 @@ public Dictionary ParseJsonToken(string text, List> Run var endIndex = reader.LinePosition; var color = GetColor(reader.TokenType); var result = new ParseResult { StartIndex = startIndex, EndIndex = endIndex, Color = color, LineNumber = lineNumber }; - resultList.Add(result); + returnValue.Add(result); oldPos = endIndex; } catch (JsonReaderException ex) @@ -121,34 +121,7 @@ public Dictionary ParseJsonToken(string text, List> Run oldPos = endIndex; } } - - for (int idxRow = 0; idxRow < Runes.Count; idxRow++) - { - var line = EditorExtensions.GetLine(Runes, idxRow); - var tokenCol = 1; - int lineRuneCount = line.Count; - var rowTokens = resultList.Where(p => (p.LineNumber == idxRow + 1)); - for (int idxCol = 0; idxCol < lineRuneCount; idxCol++) - { - - var rune = idxCol >= lineRuneCount ? ' ' : line[idxCol]; - var cols = Rune.ColumnWidth(rune); - - var match = rowTokens.Where(p => (tokenCol >= p.StartIndex && tokenCol <= p.EndIndex)).FirstOrDefault(); - var color = Color.Green; - if (match != null) - { - color = match.Color; - } - - tokenCol++; - var point = new Point(idxCol + 1, idxRow + 1); - returnList.Add(point, color); - - } - } - - return returnList; + return returnValue; } public override void ParseText(int height, int topRow, int left, int right, string text, List> Runes) { @@ -164,7 +137,7 @@ public override void ParseText(int height, int topRow, int left, int right, stri parsedErrors.Clear(); Errors.Clear(); ColumnErrors.Clear(); - tokens = ParseJsonToken(text, Runes); + parsedTokens = ParseJsonToken(text, Runes); } Dictionary returnDict = new Dictionary(); @@ -174,6 +147,7 @@ public override void ParseText(int height, int topRow, int left, int right, stri _lastParseRightColumn = right; long count = 0; var row = 0; + for (int idxRow = topRow; idxRow < Runes.Count; idxRow++) { if (row > bottom) @@ -184,7 +158,7 @@ public override void ParseText(int height, int topRow, int left, int right, stri int lineRuneCount = line.Count; var col = left; var tokenCol = 1 + left; - + var rowTokens = parsedTokens.Where(p => (p.LineNumber == idxRow + 1)); var rowErrors = parsedErrors.Where(e => e.LineNumber == idxRow + 1); for (int idxCol = left; idxCol < lineRuneCount; idxCol++) @@ -199,14 +173,18 @@ public override void ParseText(int height, int topRow, int left, int right, stri { ColumnErrors.TryAdd(new Point(idxCol, idxRow), colError.ErrorMessage); } - - var jsonParseMatch = tokens[new Point(tokenCol, idxRow + 1)]; + var jsonParseMatch = rowTokens.Where(p => (tokenCol >= p.StartIndex && tokenCol <= p.EndIndex)).FirstOrDefault(); + var color = Color.Green; + if (jsonParseMatch != null) + { + color = jsonParseMatch.Color; + } count++; var rune = idxCol >= lineRuneCount ? ' ' : line[idxCol]; var cols = Rune.ColumnWidth(rune); var point = new Point(idxCol, row); - returnDict.Add(point, jsonParseMatch); + returnDict.Add(point, color); tokenCol++; if (!EditorExtensions.SetCol(ref col, right, cols)) diff --git a/EditorContext/YamlEditorContext.cs b/EditorContext/YamlEditorContext.cs index 4178193..0996d93 100644 --- a/EditorContext/YamlEditorContext.cs +++ b/EditorContext/YamlEditorContext.cs @@ -12,7 +12,7 @@ namespace psedit { public class YamlEditorContext : EditorContext { - private Dictionary tokens; + private List parsedTokens; private List parsedErrors = new List(); public YamlEditorContext(int TabWidth) { @@ -20,7 +20,7 @@ public YamlEditorContext(int TabWidth) CanFormat = true; CanSyntaxHighlight = true; } - public Terminal.Gui.Color GetColor(ParsingEvent token) + public Terminal.Gui.Color GetColor(ParsingEvent token) { var theme = ThemeService.Instance; switch (token) @@ -49,10 +49,9 @@ public Terminal.Gui.Color GetColor(ParsingEvent token) return theme.GetColor("Foreground"); } } - public Dictionary ParseYamlToken(string text, List> Runes) + public List ParseYamlToken(string text, List> Runes) { - List resultList = new List(); - Dictionary returnList = new Dictionary(); + List returnValue = new List(); var reader = new StringReader(text); var parser = new YamlDotNet.Core.Parser(reader); int oldPos = 1; @@ -80,11 +79,10 @@ public Dictionary ParseYamlToken(string text, List> Run oldPos = 1; } var startIndex = parser.Current != null ? (int)parser.Current.Start.Column : oldPos + 1; - var endIndex = parser.Current != null ? (int)parser.Current.End.Column -1 : oldPos + 1; + var endIndex = parser.Current != null ? (int)parser.Current.End.Column - 1 : oldPos + 1; var color = GetColor(token); var result = new ParseResult { StartIndex = startIndex, EndIndex = endIndex, Color = color, LineNumber = lineNumber }; - Debug.WriteLine($"YAML Token: {token} Line: {lineNumber} StartIndex: {startIndex} EndIndex: {endIndex} Color: {color}"); - resultList.Add(result); + returnValue.Add(result); oldPos = endIndex; } catch (YamlDotNet.Core.YamlException ex) @@ -113,28 +111,7 @@ public Dictionary ParseYamlToken(string text, List> Run oldPos = (int)endIndex; } } - for (int idxRow = 0; idxRow < Runes.Count; idxRow++) - { - var line = EditorExtensions.GetLine(Runes, idxRow); - var tokenCol = 1; - int lineRuneCount = line.Count; - var rowTokens = resultList.Where(p => (p.LineNumber == idxRow + 1)); - for (int idxCol = 0; idxCol < lineRuneCount; idxCol++) - { - var rune = idxCol >= lineRuneCount ? ' ' : line[idxCol]; - var cols = Rune.ColumnWidth(rune); - var match = rowTokens.Where(p => (tokenCol >= p.StartIndex && tokenCol <= p.EndIndex)).FirstOrDefault(); - var color = Color.Green; - if (match != null) - { - color = match.Color; - } - tokenCol++; - var point = new Point(idxCol + 1, idxRow + 1); - returnList.Add(point, color); - } - } - return returnList; + return returnValue; } public override void ParseText(int height, int topRow, int left, int right, string text, List> Runes) { @@ -147,7 +124,7 @@ public override void ParseText(int height, int topRow, int left, int right, stri parsedErrors.Clear(); Errors.Clear(); ColumnErrors.Clear(); - tokens = ParseYamlToken(text, Runes); + parsedTokens = ParseYamlToken(text, Runes); } Dictionary returnDict = new Dictionary(); int bottom = topRow + height; @@ -166,6 +143,7 @@ public override void ParseText(int height, int topRow, int left, int right, stri int lineRuneCount = line.Count; var col = left; var tokenCol = 1 + left; + var rowTokens = parsedTokens.Where(p => (p.LineNumber == idxRow + 1)); var rowErrors = parsedErrors.Where(e => e.LineNumber == idxRow + 1); for (int idxCol = left; idxCol < lineRuneCount; idxCol++) { @@ -178,12 +156,17 @@ public override void ParseText(int height, int topRow, int left, int right, stri { ColumnErrors.TryAdd(new Point(idxCol, idxRow), colError.ErrorMessage); } - var yamlParseMatch = tokens[new Point(tokenCol, idxRow + 1)]; + var yamlParseMatch = rowTokens.Where(p => (tokenCol >= p.StartIndex && tokenCol <= p.EndIndex)).FirstOrDefault(); + var color = Color.Green; + if (yamlParseMatch != null) + { + color = yamlParseMatch.Color; + } count++; var rune = idxCol >= lineRuneCount ? ' ' : line[idxCol]; var cols = Rune.ColumnWidth(rune); var point = new Point(idxCol, row); - returnDict.Add(point, yamlParseMatch); + returnDict.Add(point, color); tokenCol++; if (!EditorExtensions.SetCol(ref col, right, cols)) {