-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add file-based template attribute and utility method for whitespace handling #6
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # File Templates | ||
|
|
||
| The templates can also be stored in external files. | ||
|
|
||
| To get started, create a '.editorconfig' file in your project. | ||
|
|
||
| In this, connect static files to valid methods in your project. | ||
|
|
||
| For example, if you have a template called 'my-template.txt', | ||
| you can connect it to the 'MyTemplate' method. | ||
|
|
||
| First, add the template to the project. | ||
|
|
||
| ```xml | ||
| <Project> | ||
| <ItemGroup> | ||
| <AdditionalFiles Include="my-template.txt" /> | ||
| </ItemGroup> | ||
| </Project> | ||
| ``` | ||
|
|
||
| Create the template method. | ||
|
|
||
| ```c# | ||
| using My.Project; | ||
|
|
||
| public static partial class Templates | ||
| { | ||
| [FileTemplate] // required | ||
| public static partial void MyTemplate(this StringBuilder sb, string name); | ||
| } | ||
| ``` | ||
|
|
||
| Then link the 2 together in the '.editorconfig' file. | ||
|
|
||
| ```editorconfig | ||
| [my-template.txt] | ||
| template_method = My.Project.Templates.MyTemplate | ||
| ``` | ||
|
|
||
| Now you can externalize your templates and set your editor of choice up to | ||
| enable syntax highlighting and other features. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [SampleTemplate.cs.liquid] | ||
| template_method = Cutout.Sample.Examples.Test4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Some.Test; | ||
|
|
||
| // Some example code | ||
| public class {{parameter}} | ||
| { | ||
| public string Hello(string name) | ||
| { | ||
| return $"Hello {name} from {{parameter}}"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
Cutout.Tests/SharedTests.Case1#FileTemplateAttribute.g.verified.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //HintName: FileTemplateAttribute.g.cs | ||
| #if !CUTOUT_EXCLUDE_ATTRIBUTES | ||
|
|
||
| // <auto-generated/> | ||
| #nullable enable | ||
|
|
||
| namespace Cutout; | ||
|
|
||
| /// <summary> | ||
| /// Marks a method as a template generator based on an external file, this method will be implemented to write | ||
| /// into a string builder like object, based on a template defined in an external file | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Method)] | ||
| internal sealed class FileTemplateAttribute : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="FileTemplateAttribute"/> class | ||
| /// </summary> | ||
| public FileTemplateAttribute() {} | ||
| } | ||
|
|
||
| #endif |
25 changes: 25 additions & 0 deletions
25
Cutout.Tests/SharedTests.Case1#RenderUtilities.g.verified.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //HintName: RenderUtilities.g.cs | ||
| // <auto-generated/> | ||
| #nullable enable | ||
|
|
||
| using System; | ||
|
|
||
| namespace Cutout; | ||
|
|
||
| /// <summary> | ||
| /// Utility methods for use in generated render methods | ||
| /// </summary> | ||
| internal static class RenderUtilities | ||
| { | ||
| internal static string? ApplyExtraWhitespace<T>(T value, string whitespace) | ||
| { | ||
| string? str = value switch | ||
| { | ||
| null => null, | ||
| string str1 => str1, | ||
| _ => value.ToString() | ||
| }; | ||
|
|
||
| return str is null ? null : str.Replace("\n", "\n" + whitespace); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.