-
Notifications
You must be signed in to change notification settings - Fork 2
Add configuration-based variable expansion for snippets #124
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 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d738341
Initial plan
Copilot 0bcb2df
Add snippet variables feature with configuration-based variable expan…
Copilot cde2a89
Apply PR #124 review feedback: WPF wiring, README clarification, Appl…
Copilot 64254de
Parse snippet text once via ISnippetTemplateCompiler
maraf 5c1acc5
Match GeneralConfiguration formatting for VariablesConfiguration.Example
maraf a6707b2
Make Example ShellExt platform-specific (cmd on Windows, sh elsewhere)
maraf 568f09c
Fix WPF reload wiring so Variables changes take effect without restart
maraf daffaba
Add expansion correctness tests for corner cases
maraf 963e046
Align README Variables example with the generated default (cmd/sh)
maraf 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| using System.Text.Json.Serialization; | ||
| using Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| namespace Neptuo.Productivity.SnippetManager; | ||
|
|
||
| public class Configuration | ||
| { | ||
| public GeneralConfiguration? General { get; set; } | ||
|
|
||
| public VariablesConfiguration? Variables { get; set; } | ||
|
|
||
| [JsonIgnore] | ||
| public Dictionary<string, object> Providers { get; } = new(); | ||
| } |
1 change: 1 addition & 0 deletions
1
src/Neptuo.Productivity.SnippetManager/Neptuo.Productivity.SnippetManager.csproj
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
16 changes: 16 additions & 0 deletions
16
src/Neptuo.Productivity.SnippetManager/Variables/CompositeVariableValueResolver.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,16 @@ | ||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public class CompositeVariableValueResolver(IEnumerable<IVariableValueResolver> resolvers) : IVariableValueResolver | ||
| { | ||
| public bool TryGetValue(string name, out string? value) | ||
| { | ||
| foreach (var resolver in resolvers) | ||
| { | ||
| if (resolver.TryGetValue(name, out value)) | ||
| return true; | ||
| } | ||
|
|
||
| value = null; | ||
| return false; | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/Neptuo.Productivity.SnippetManager/Variables/ConfigurationVariableValueResolver.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,13 @@ | ||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public class ConfigurationVariableValueResolver(VariablesConfiguration? configuration) : IVariableValueResolver | ||
| { | ||
| public bool TryGetValue(string name, out string? value) | ||
| { | ||
| if (configuration != null && configuration.TryGetValue(name, out value)) | ||
| return true; | ||
|
|
||
| value = null; | ||
| return false; | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/Neptuo.Productivity.SnippetManager/Variables/ISnippetTextExpander.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,6 @@ | ||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public interface ISnippetTextExpander | ||
| { | ||
| string Expand(string text, IReadOnlyDictionary<string, string?> values); | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/Neptuo.Productivity.SnippetManager/Variables/ISnippetVariableScanner.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,6 @@ | ||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public interface ISnippetVariableScanner | ||
| { | ||
| IReadOnlyList<VariableReference> Scan(string text); | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/Neptuo.Productivity.SnippetManager/Variables/IVariableValueResolver.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,6 @@ | ||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public interface IVariableValueResolver | ||
| { | ||
| bool TryGetValue(string name, out string? value); | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/Neptuo.Productivity.SnippetManager/Variables/SnippetExpansionPipeline.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,23 @@ | ||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public class SnippetExpansionPipeline( | ||
| ISnippetVariableScanner scanner, | ||
| IVariableValueResolver resolver, | ||
| ISnippetTextExpander expander) | ||
| { | ||
| public string Apply(string text) | ||
| { | ||
| var references = scanner.Scan(text); | ||
| if (references.Count == 0) | ||
| return text; | ||
|
|
||
| var values = new Dictionary<string, string?>(); | ||
| foreach (var reference in references) | ||
| { | ||
| resolver.TryGetValue(reference.Name, out var value); | ||
| values[reference.Name] = value; | ||
| } | ||
|
|
||
| return expander.Expand(text, values); | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
src/Neptuo.Productivity.SnippetManager/Variables/TokenSnippetTextExpander.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,47 @@ | ||
| using System.Text; | ||
| using Neptuo.Text.Tokens; | ||
|
|
||
| namespace Neptuo.Productivity.SnippetManager.Variables; | ||
|
|
||
| public class TokenSnippetTextExpander : ISnippetTextExpander | ||
| { | ||
| public string Expand(string text, IReadOnlyDictionary<string, string?> values) | ||
| { | ||
| var parser = CreateParser(); | ||
| var parsedTokens = new List<TokenEventArgs>(); | ||
| parser.OnParsedToken += (sender, e) => parsedTokens.Add(e); | ||
|
|
||
| if (!parser.Parse(text) || parsedTokens.Count == 0) | ||
| return text; | ||
|
|
||
| var sb = new StringBuilder(); | ||
| int lastEnd = 0; | ||
|
|
||
| foreach (var tokenEvent in parsedTokens) | ||
| { | ||
| sb.Append(text, lastEnd, tokenEvent.StartPosition - lastEnd); | ||
|
|
||
| if (values.TryGetValue(tokenEvent.Token.Fullname, out var value) && value != null) | ||
| sb.Append(value); | ||
| else | ||
| sb.Append(text, tokenEvent.StartPosition, tokenEvent.EndPosition - tokenEvent.StartPosition); | ||
|
|
||
| lastEnd = tokenEvent.EndPosition; | ||
| } | ||
|
|
||
| sb.Append(text, lastEnd, text.Length - lastEnd); | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
|
|
||
| private static TokenParser CreateParser() | ||
| { | ||
| var parser = new TokenParser(); | ||
| parser.Configuration.AllowTextContent = true; | ||
| parser.Configuration.AllowMultipleTokens = true; | ||
| parser.Configuration.AllowEscapeSequence = true; | ||
| parser.Configuration.AllowAttributes = false; | ||
| parser.Configuration.AllowDefaultAttributes = false; | ||
| return parser; | ||
| } | ||
| } |
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.