mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-05 03:08:13 +00:00
Highlighted code blocks inside the app (#1153)
This commit is contained in:
1 parent
0b42c0288e
commit
f4a86c2fb8
60 files changed
+2550
-41
No files matched your search
@@ -0,0 +1,27 @@
|
||||
using System.Text;
|
||||
|
||||
using AIStudio.Tools.CodeHighlighting;
|
||||
|
||||
namespace AIStudio.Tests.Tools.CodeHighlighting;
|
||||
|
||||
/// <summary>
|
||||
/// Writes code with its highlighted parts marked, such as {keyword:def}, so that a test reads like the
|
||||
/// code it checks.
|
||||
/// </summary>
|
||||
internal static class CodeMarkup
|
||||
{
|
||||
public static string Of(string code, CodeLanguageRules rules)
|
||||
{
|
||||
var markup = new StringBuilder();
|
||||
var position = 0;
|
||||
foreach (var token in CodeTokenizer.Tokenize(code, rules))
|
||||
{
|
||||
markup.Append(code, position, token.Start - position);
|
||||
markup.Append('{').Append(token.Kind.ToString().ToLowerInvariant()).Append(':').Append(code, token.Start, token.Length).Append('}');
|
||||
position = token.Start + token.Length;
|
||||
}
|
||||
|
||||
markup.Append(code, position, code.Length - position);
|
||||
return markup.ToString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user