Skip to content

Add support for expressive code in snippets #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Argon" Version="0.27.0" />
<PackageVersion Include="Argon" Version="0.28.0" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
<PackageVersion Include="Microsoft.Build.Tasks.Core" Version="17.13.9" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
Expand Down
3 changes: 2 additions & 1 deletion src/MarkdownSnippets/Processing/MarkdownProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ Snippet FileToSnippet(string key, string file, string? path)
value: text,
key: key,
language: Path.GetExtension(file)[1..],
path: path);
path: path,
expressiveCode: null);
}

(string text, int lineCount) ReadNonStartEndLines(string file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void Append(string key, IEnumerable<Snippet> snippets, Action<stri

static void WriteSnippet(Action<string> appendLine, Snippet snippet)
{
appendLine($"```{snippet.Language}");
appendLine($"```{snippet.Language} {snippet.ExpressiveCode}".TrimEnd());
appendLine(snippet.Value);
appendLine("```");
}
Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownSnippets/Processing/SnippetMarkdownHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ string GetSupText(Snippet snippet, string anchor)

static void WriteSnippetValueAndLanguage(Action<string> appendLine, Snippet snippet)
{
appendLine($"```{snippet.Language}");
appendLine($"```{snippet.Language} {snippet.ExpressiveCode}".TrimEnd());
appendLine(snippet.Value);
appendLine("```");
}
Expand Down
11 changes: 6 additions & 5 deletions src/MarkdownSnippets/Reading/FileSnippetExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static async Task AppendUrlAsSnippet(ICollection<Snippet> snippets, strin
throw new SnippetException($"Unable to get UrlAsSnippet: {url}");
}

var snippet = Snippet.Build(1, content!.LineCount(), content!, key, GetLanguageFromPath(url), url);
var snippet = Snippet.Build(1, content!.LineCount(), content!, key, GetLanguageFromPath(url), url, null);
snippets.Add(snippet);

using var reader = new StringReader(content!);
Expand Down Expand Up @@ -71,7 +71,7 @@ public static void AppendFileAsSnippet(ICollection<Snippet> snippets, string fil
{
Guard.FileExists(filePath, nameof(filePath));
var text = File.ReadAllText(filePath);
var snippet = Snippet.Build(1, text.LineCount(), text, key, GetLanguageFromPath(filePath), filePath);
var snippet = Snippet.Build(1, text.LineCount(), text, key, GetLanguageFromPath(filePath), filePath, null);
snippets.Add(snippet);
}

Expand Down Expand Up @@ -153,9 +153,9 @@ static IEnumerable<Snippet> GetSnippets(TextReader stringReader, string path, in

var trimmedLine = line.Trim();

if (StartEndTester.IsStart(trimmedLine, path, out var key, out var endFunc))
if (StartEndTester.IsStart(trimmedLine, path, out var key, out var endFunc, out var expressive))
{
loopStack.Push(endFunc, key, index, maxWidth, newLine);
loopStack.Push(endFunc, key, index, maxWidth, newLine, expressive);
continue;
}

Expand Down Expand Up @@ -207,7 +207,8 @@ static Snippet BuildSnippet(string path, LoopStack loopStack, string language, i
key: loopState.Key,
value: value,
path: path,
language: language.ToLowerInvariant()
language: language.ToLowerInvariant(),
expressiveCode: loopState.ExpressiveCode
);
}
}
4 changes: 2 additions & 2 deletions src/MarkdownSnippets/Reading/LoopStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public void AppendLine(string line)

public void Pop() => stack.Pop();

public void Push(EndFunc endFunc, string key, int startLine, int maxWidth, string newLine)
public void Push(EndFunc endFunc, string key, int startLine, int maxWidth, string newLine, string? block)
{
var state = new LoopState(key, endFunc, startLine, maxWidth, newLine);
var state = new LoopState(key, endFunc, startLine, maxWidth, newLine, block);
stack.Push(state);
}

Expand Down
3 changes: 2 additions & 1 deletion src/MarkdownSnippets/Reading/LoopState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[DebuggerDisplay("Key={Key}")]
class LoopState(string key, EndFunc endFunc, int startLine, int maxWidth, string newLine)
class LoopState(string key, EndFunc endFunc, int startLine, int maxWidth, string newLine, string? expressiveCode = null)
{
public string GetLines()
{
Expand Down Expand Up @@ -85,4 +85,5 @@ void CheckWhiteSpace(string line, char whiteSpace)
public EndFunc EndFunc { get; } = endFunc;
public int StartLine { get; } = startLine;
int newlineCount;
public string? ExpressiveCode { get; } = expressiveCode;
}
11 changes: 10 additions & 1 deletion src/MarkdownSnippets/Reading/Snippet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static Snippet BuildError(string key, int lineNumberInError, string path,
/// <summary>
/// Initialise a new instance of <see cref="Snippet"/>.
/// </summary>
public static Snippet Build(int startLine, int endLine, string value, string key, string language, string? path)
public static Snippet Build(int startLine, int endLine, string value, string key, string language, string? path, string? expressiveCode)
{
Guard.AgainstNullAndEmpty(key, nameof(key));
Guard.AgainstEmpty(path, nameof(path));
Expand All @@ -46,6 +46,7 @@ public static Snippet Build(int startLine, int endLine, string value, string key
Key = key,
language = language,
Path = path,
ExpressiveCode = expressiveCode,
Error = ""
};
}
Expand All @@ -59,6 +60,12 @@ public static Snippet Build(int startLine, int endLine, string value, string key
/// </summary>
public string Key { get; private init; } = null!;

/// <summary>
/// An associated expressive code block with the snippet
/// See https://expressive-code.com/
/// </summary>
public string? ExpressiveCode { get; private init; }

/// <summary>
/// The language of the snippet, extracted from the file extension of the input file.
/// </summary>
Expand All @@ -70,6 +77,7 @@ public string Language
return language!;
}
}

string? language;

/// <summary>
Expand Down Expand Up @@ -98,6 +106,7 @@ public string? FileLocation
{
return null;
}

return $"{Path}({StartLine}-{EndLine})";
}
}
Expand Down
32 changes: 25 additions & 7 deletions src/MarkdownSnippets/Reading/StartEndTester.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
public delegate bool EndFunc(string line);

static class StartEndTester
static partial class StartEndTester
{
static readonly Regex keyPatternRegex = new(@"([a-zA-Z0-9\-_]+)(?:\((.*?)\))?");

internal static bool IsStartOrEnd(string trimmedLine) =>
IsBeginSnippet(trimmedLine) ||
IsEndSnippet(trimmedLine) ||
Expand All @@ -12,21 +14,28 @@ internal static bool IsStart(
string trimmedLine,
string path,
[NotNullWhen(true)] out string? currentKey,
[NotNullWhen(true)] out EndFunc? endFunc)
[NotNullWhen(true)] out EndFunc? endFunc,
out string? expressiveCode
)
{
if (IsBeginSnippet(trimmedLine, path, out currentKey))
if (IsBeginSnippet(trimmedLine, path, out currentKey, out var block))
{
endFunc = IsEndSnippet;
expressiveCode = block;
return true;
}

if (IsStartRegion(trimmedLine, out currentKey))
{
endFunc = IsEndRegion;
// not supported for regions
expressiveCode = null;
return true;
}

expressiveCode = null;
endFunc = throwFunc;

return false;
}

Expand Down Expand Up @@ -78,8 +87,11 @@ static bool IsBeginSnippet(string line)
internal static bool IsBeginSnippet(
string line,
string path,
[NotNullWhen(true)] out string? key)
[NotNullWhen(true)] out string? key,
[NotNullWhen(true)] out string? expressiveCode
)
{
expressiveCode = null;
var beginSnippetIndex = IndexOf(line, "begin-snippet: ");
if (beginSnippetIndex == -1)
{
Expand All @@ -90,8 +102,10 @@ internal static bool IsBeginSnippet(
var startIndex = beginSnippetIndex + 15;
var substring = line
.TrimBackCommentChars(startIndex);
var split = substring.SplitBySpace();
if (split.Length == 0)

var match = keyPatternRegex.Match(substring);

if (match.Length == 0)
{
throw new SnippetReadingException(
$"""
Expand All @@ -101,7 +115,8 @@ No Key could be derived.
""");
}

key = split[0];
var partOne = match.Groups[1].Value;
var split = partOne.SplitBySpace();
if (split.Length != 1)
{
throw new SnippetReadingException(
Expand All @@ -112,6 +127,9 @@ Too many parts.
""");
}

key = split[0];
expressiveCode = match.Groups[2].Value;

if (KeyValidator.IsValidKey(key.AsSpan()))
{
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/Tests/DirectoryMarkdownProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,5 +507,6 @@ static Snippet SnippetBuild(string key, string? path = null) =>
endLine: 2,
value: "the code from " + key,
key: key,
path: path);
path: path,
expressiveCode: null);
}
9 changes: 6 additions & 3 deletions src/Tests/MarkdownProcessor/MarkdownProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ public Task WithTwoLineSnippet()
Snippet
""",
key: "theKey",
path: "thePath")
path: "thePath",
expressiveCode: null),
]);
}

Expand Down Expand Up @@ -236,7 +237,8 @@ public Task WithMultiLineSnippet()
Snippet
""",
key: "theKey",
path: "thePath")
path: "thePath",
expressiveCode: null)
]);
}

Expand Down Expand Up @@ -669,5 +671,6 @@ static Snippet SnippetBuild(string language, string key) =>
endLine: 2,
value: "Snippet",
key: key,
path: "thePath");
path: "thePath",
expressiveCode: null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```cs title="HelloWorld.cs" {1}
Console.WriteLine("Hello World");
```
15 changes: 14 additions & 1 deletion src/Tests/SimpleSnippetMarkdownHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@
public Task Append()
{
var builder = new StringBuilder();
var snippets = new List<Snippet> {Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", "thePath")};
var snippets = new List<Snippet> {Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", "thePath", null)};
using (var writer = new StringWriter(builder))
{
SimpleSnippetMarkdownHandling.Append("key1", snippets, writer.WriteLine);
}

return Verify(builder.ToString());
}

[Fact]
public Task ExpressiveCode()
{
var builder = new StringBuilder();
var snippets = new List<Snippet> {Snippet.Build(1, 2, """Console.WriteLine("Hello World");""", "thekey", "cs", "thePath", """title="HelloWorld.cs" {1}""")};
using (var writer = new StringWriter(builder))
{
SimpleSnippetMarkdownHandling.Append("key1", snippets, writer.WriteLine);
Expand Down
3 changes: 2 additions & 1 deletion src/Tests/SnippetExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ static Snippet SnippetBuild(string key, string? path) =>
endLine: 2,
value: "Snippet",
key: key,
path: path);
path: path,
expressiveCode: null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
Key: CodeKey,
Language: cs,
Value: Console.WriteLine("Hello World");,
Error: ,
FileLocation: path.cs(1-3),
IsInError: false
}
]
12 changes: 12 additions & 0 deletions src/Tests/SnippetExtractor/SnippetExtractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,16 @@ the code
var snippets = FromText(input);
return Verify(snippets);
}

[Fact]
public Task CanExtractWithExpressiveCode()
{
var input = """
<!--begin-snippet: CodeKey(title="Program.cs" {1-3})-->
Console.WriteLine("Hello World");
<!--end-snippet-->
""";
var snippets = FromText(input);
return Verify(snippets);
}
}
2 changes: 1 addition & 1 deletion src/Tests/SnippetMarkdownHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ public Task AppendHashed()
}

static List<Snippet> Snippets() =>
[Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", Environment.CurrentDirectory)];
[Snippet.Build(1, 2, "theValue", "thekey", "thelanguage", Environment.CurrentDirectory, expressiveCode: null)];
}
Loading