-
Notifications
You must be signed in to change notification settings - Fork 0
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
wip: create generator project #1
Draft
ttd2089
wants to merge
1
commit into
main
Choose a base branch
from
feat/generate-default-value-parsers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 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,3 @@ | ||
.vs/ | ||
[Bb]in/ | ||
[Oo]bj/ |
This file contains 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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference | ||
Include="..\Geis.ValueParserGenerators\Geis.ValueParserGenerators.csproj" | ||
OutputItemType="Analyzer"/> | ||
<ProjectReference Include="..\Geis\Geis.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,5 @@ | ||
using Geis; | ||
|
||
var collection = new ValueParserCollection(); | ||
collection.AddDefaultParser<int>(); | ||
collection.AddDefaultParser<bool>(); |
49 changes: 49 additions & 0 deletions
49
src/Geis.ValueParserGenerators/DefaultValueParserGenerator.cs
This file contains 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,49 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System.Collections.Immutable; | ||
|
||
namespace Geis.ValueParserGenerators; | ||
|
||
[Generator] | ||
public class DefaultValueParserGenerator : IIncrementalGenerator | ||
{ | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
//System.Diagnostics.Debugger.Launch(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to put this behind a flag and conditional compile it in |
||
|
||
var provider = context | ||
.SyntaxProvider | ||
.CreateSyntaxProvider( | ||
predicate: (c, _) => c is GenericNameSyntax n, | ||
transform: (n, _) => (GenericNameSyntax)n.Node) | ||
.Where(x => x is not null); | ||
|
||
var compilation = context.CompilationProvider.Combine(provider.Collect()); | ||
|
||
context.RegisterSourceOutput( | ||
compilation, | ||
(spc, source) => Execute(spc, source.Left, source.Right)); | ||
} | ||
|
||
private void Execute( | ||
SourceProductionContext context, | ||
Compilation compilation, | ||
ImmutableArray<GenericNameSyntax> genericNames) | ||
{ | ||
foreach (var name in genericNames) | ||
{ | ||
// todo: add explicitly constructor value parsers that reference the TryParse or new(string) member of T. | ||
|
||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
new DiagnosticDescriptor( | ||
id: "GVPG0001", | ||
title: "Possible Reference To AddDefaultParser", | ||
messageFormat: "Found a possible reference to the AddDefaultParser method.", | ||
category: "Accomplishment", | ||
defaultSeverity: DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true), | ||
name.GetLocation())); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Geis.ValueParserGenerators/Geis.ValueParserGenerators.csproj
This file contains 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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,37 @@ | ||
| ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geis.ValueParserGenerators", "Geis.ValueParserGenerators\Geis.ValueParserGenerators.csproj", "{388C1697-D0ED-4340-AE58-22CF614FD50D}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geis", "Geis\Geis.csproj", "{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geis.ScratchApp", "Geis.ScratchApp\Geis.ScratchApp.csproj", "{435D5879-9BB7-4106-BB3A-2B22750C08EF}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {3043616A-5042-4512-816E-3345E1FA7AB3} | ||
EndGlobalSection | ||
EndGlobal |
This file contains 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,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains 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,17 @@ | ||
namespace Geis; | ||
|
||
public class ValueParserCollection | ||
{ | ||
private readonly Dictionary<Type, Func<string, object?>> _parsers = new(); | ||
|
||
public ValueParserCollection SetParser<T>(Func<string, T?> parser) where T : notnull | ||
{ | ||
_parsers[typeof(T)] = s => parser(s); | ||
return this; | ||
} | ||
} | ||
|
||
public static class ValueParserCollectionExtensions | ||
{ | ||
public static ValueParserCollection AddDefaultParser<T>(this ValueParserCollection collection) => collection; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to be the old man "No, the kids are wrong" but I'd prefer not using top level statements