Skip to content

Commit

Permalink
Highlighter now works on the whole line.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger authored and patriksvensson committed Jun 18, 2024
1 parent 51ff1df commit d538198
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
7 changes: 4 additions & 3 deletions examples/Repl/JavaScriptHighlighter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Esprima;
using Spectre.Console;
using Spectre.Console.Rendering;

namespace RadLine.Examples
{
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/RadLine/IHighlighter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Spectre.Console;
using Spectre.Console.Rendering;

namespace RadLine
{
public interface IHighlighter
{
Style? Highlight(string token);
IRenderable BuildHighlightedText(string text);
}
}
9 changes: 1 addition & 8 deletions src/RadLine/Internal/LineEditorAnsiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
20 changes: 17 additions & 3 deletions src/RadLine/WordHighlighter.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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;
}
}
}

0 comments on commit d538198

Please sign in to comment.