diff --git a/examples/Repl/JavaScriptHighlighter.cs b/examples/Repl/JavaScriptHighlighter.cs index e8df029..d57996d 100644 --- a/examples/Repl/JavaScriptHighlighter.cs +++ b/examples/Repl/JavaScriptHighlighter.cs @@ -1,4 +1,6 @@ +using Esprima; using Spectre.Console; +using Spectre.Console.Rendering; namespace RadLine.Examples { @@ -41,10 +43,9 @@ private static WordHighlighter CreateHighlighter() return highlighter; } - - public Style Highlight(string token) + public IRenderable BuildHighlightedText(string text) { - return _highlighter.Highlight(token); + return _highlighter.BuildHighlightedText(text); } } } \ No newline at end of file diff --git a/src/RadLine/IHighlighter.cs b/src/RadLine/IHighlighter.cs index 25d7a33..703bab2 100644 --- a/src/RadLine/IHighlighter.cs +++ b/src/RadLine/IHighlighter.cs @@ -1,9 +1,9 @@ -using Spectre.Console; +using Spectre.Console.Rendering; namespace RadLine { public interface IHighlighter { - Style? Highlight(string token); + IRenderable BuildHighlightedText(string text); } } diff --git a/src/RadLine/Internal/LineEditorAnsiBuilder.cs b/src/RadLine/Internal/LineEditorAnsiBuilder.cs index daaadf8..9876c42 100644 --- a/src/RadLine/Internal/LineEditorAnsiBuilder.cs +++ b/src/RadLine/Internal/LineEditorAnsiBuilder.cs @@ -239,14 +239,7 @@ private IRenderable BuildHighlightedText(string text) return new Text(text); } - var paragraph = new Paragraph(); - foreach (var token in StringTokenizer.Tokenize(text)) - { - var style = string.IsNullOrWhiteSpace(token) ? null : highlighter.Highlight(token); - paragraph.Append(token, style); - } - - return paragraph; + return highlighter.BuildHighlightedText(text); } } } diff --git a/src/RadLine/WordHighlighter.cs b/src/RadLine/WordHighlighter.cs index cef5fd5..c1263ee 100644 --- a/src/RadLine/WordHighlighter.cs +++ b/src/RadLine/WordHighlighter.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using Spectre.Console; +using Spectre.Console.Rendering; +using static System.Net.Mime.MediaTypeNames; namespace RadLine { @@ -19,10 +21,22 @@ public WordHighlighter AddWord(string word, Style style) return this; } - Style? IHighlighter.Highlight(string token) + IRenderable IHighlighter.BuildHighlightedText(string text) { - _words.TryGetValue(token, out var style); - return style; + var paragraph = new Paragraph(); + foreach (var token in StringTokenizer.Tokenize(text)) + { + if (_words.TryGetValue(token, out var style)) + { + paragraph.Append(token, style); + } + else + { + paragraph.Append(token, null); + } + } + + return paragraph; } } }