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

Conversation

khalidabuhakmeh
Copy link
Contributor

@khalidabuhakmeh khalidabuhakmeh commented Apr 28, 2025

This update introduces the handling of expressive code metadata for code snippets, enabling enhanced representation and functionality. Changes include parsing, storing, and rendering expressive attributes in snippets and updates to tests and relevant components to validate the new feature. Additionally, a package dependency (Argon) is updated for compatibility.

Only works for comment snippets currently.

See https://expressive-code.com/ for more details. #714

TL;DR:

This...

<!--begin-snippet: CodeKey(title="Program.cs" {1-3})-->
Console.WriteLine("Hello World");
<!--end-snippet-->

Get converted into...

```csharp title="Program.cs" {1-3}
Console.WriteLine("Hello World");
```

This update introduces the handling of expressive code metadata for code snippets, enabling enhanced representation and functionality. Changes include parsing, storing, and rendering expressive attributes in snippets, along with updates to tests and relevant components to validate the new feature. Additionally, a package dependency (Argon) is updated for compatibility.

Only works for comment snippets currently.
@khalidabuhakmeh
Copy link
Contributor Author

Also addresses issue #537

@khalidabuhakmeh khalidabuhakmeh changed the title Add support for expressive code blocks in snippets Add support for expressive code in snippets Apr 28, 2025
@khalidabuhakmeh
Copy link
Contributor Author

@SimonCropp I can make the expressiveCode parameter optional if it reduces the number of changes in the pull request. I leaned away from doing so in case there's a new parameter in the future and it helped locate all the places I needed to update.

@SimonCropp
Copy link
Owner

can u add some docs

Introduced the `ExpressiveCode` class to centralize regex pattern handling, supporting both .NET 8.0+ generated regex and static regex for older versions. Updated `StartEndTester` to utilize the shared `ExpressiveCode.Instance.Pattern` for consistency and maintainability. Expanded documentation with examples of snippet metadata and its rendering.
@khalidabuhakmeh
Copy link
Contributor Author

@SimonCropp I've made the changes you requested. Please re-review and let me know if you'd like any more changes.

I did notice that #716 was a feature to add metadata at the target. That could be a follow up to this PR where I can make it also handle this metadata at write-time.

Reorganized Regex initialization using `#if NET8_0_OR_GREATER` to leverage `GeneratedRegex` for .NET 8+ and fallback to static initialization otherwise instance is null.
Simplified the logic for generating the language declaration by checking for empty `ExpressiveCode`. No more TrimEnd.
@SimonCropp SimonCropp added this to the 28.0.0 milestone May 3, 2025
@SimonCropp SimonCropp merged commit ad72fdf into SimonCropp:main May 3, 2025
2 checks passed
@khalidabuhakmeh
Copy link
Contributor Author

Thank you sir. You are a scholar and a gentlemen. 🧐

@SimonCropp
Copy link
Owner

lol. thanks. i think i still owe you more drinks than the other way around

i will deploy a beta now. have a play, if u r ok with it, i will deploy a stable when u give me the ok

@SimonCropp
Copy link
Owner

28.0.0-beta.1 deployed. give nuget 10 min to cache it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants