-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Repeat pattern method argument when used with no argu…
…ments and add replacePattern support on method
- Loading branch information
1 parent
5e8e6d7
commit 1992316
Showing
30 changed files
with
688 additions
and
112 deletions.
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
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
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
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
116 changes: 116 additions & 0 deletions
116
src/libs/SoloX.GeneratorTools.Core.CSharp/Generator/Impl/AInstanceResolver.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,116 @@ | ||
// ---------------------------------------------------------------------- | ||
// <copyright file="AInstanceResolver.cs" company="Xavier Solau"> | ||
// Copyright © 2021 Xavier Solau. | ||
// Licensed under the MIT license. | ||
// See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
// ---------------------------------------------------------------------- | ||
|
||
using Microsoft.CodeAnalysis; | ||
using SoloX.GeneratorTools.Core.CSharp.Model.Use; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace SoloX.GeneratorTools.Core.CSharp.Generator.Impl | ||
{ | ||
/// <summary> | ||
/// Base type resolver to find a Type from a IDeclarationUse | ||
/// </summary> | ||
public abstract class AInstanceResolver | ||
{ | ||
private readonly Dictionary<string, Type> typesByFullName; | ||
private readonly Dictionary<string, Type> typesByName; | ||
|
||
/// <summary> | ||
/// Setup instance with the given types to be resolved. | ||
/// </summary> | ||
/// <param name="types">Types to be resolved in selectors.</param> | ||
protected AInstanceResolver(IEnumerable<Type> defaultTypes, params Type[] types) | ||
{ | ||
var allTypes = types.Concat(defaultTypes).ToArray(); | ||
|
||
this.typesByFullName = allTypes.ToDictionary(t => t.FullName); | ||
this.typesByName = allTypes.ToDictionary(t => t.Name); | ||
} | ||
|
||
/// <summary> | ||
/// Create a Type instance from the given declarationUse | ||
/// </summary> | ||
/// <typeparam name="TInstance"></typeparam> | ||
/// <param name="declarationUse"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
protected TInstance? CreateInstance<TInstance>(IDeclarationUse<SyntaxNode> declarationUse) | ||
{ | ||
if (declarationUse == null) | ||
{ | ||
throw new ArgumentNullException(nameof(declarationUse)); | ||
} | ||
|
||
var instanceType = ResolveAsType(declarationUse); | ||
|
||
if (instanceType != null) | ||
{ | ||
return (TInstance)Activator.CreateInstance(instanceType); | ||
} | ||
|
||
return default; | ||
} | ||
|
||
private Type ResolveAsType(IDeclarationUse<SyntaxNode> declarationUse) | ||
{ | ||
if (declarationUse is IGenericDeclarationUse genericDeclarationUse) | ||
{ | ||
var fullName = declarationUse.Declaration.FullName; | ||
var genericCount = genericDeclarationUse.GenericParameters.Count; | ||
|
||
Type type; | ||
if (genericCount > 0) | ||
{ | ||
fullName = fullName + '`' + genericCount; | ||
|
||
type = GetTypeFromName(fullName); | ||
|
||
var genericParameters = new List<Type>(); | ||
|
||
foreach (var genericParameter in genericDeclarationUse.GenericParameters) | ||
{ | ||
var genericParameterType = ResolveAsType(genericParameter); | ||
|
||
genericParameters.Add(genericParameterType); | ||
} | ||
|
||
type = type.MakeGenericType(genericParameters.ToArray()); | ||
} | ||
else | ||
{ | ||
type = GetTypeFromName(fullName); | ||
} | ||
|
||
return type; | ||
} | ||
|
||
return GetTypeFromName(declarationUse.Declaration.Name); | ||
} | ||
|
||
/// <summary> | ||
/// Get the type matching the given name. | ||
/// </summary> | ||
/// <param name="name">Name of the type to resolve.</param> | ||
/// <returns>The resolver Type object or null.</returns> | ||
protected Type GetTypeFromName(string name) | ||
{ | ||
if (this.typesByFullName.TryGetValue(name, out var type)) | ||
{ | ||
return type; | ||
} | ||
else if (this.typesByName.TryGetValue(name, out type)) | ||
{ | ||
return type; | ||
} | ||
|
||
throw new NotSupportedException($"Unable to resolve type from name: {name}"); | ||
} | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
src/libs/SoloX.GeneratorTools.Core.CSharp/Generator/Impl/DefaultReplacePatternResolver.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,42 @@ | ||
// ---------------------------------------------------------------------- | ||
// <copyright file="DefaultReplacePatternResolver.cs" company="Xavier Solau"> | ||
// Copyright © 2021 Xavier Solau. | ||
// Licensed under the MIT license. | ||
// See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
// ---------------------------------------------------------------------- | ||
|
||
using Microsoft.CodeAnalysis; | ||
using SoloX.GeneratorTools.Core.CSharp.Model.Use; | ||
using System.Collections.Generic; | ||
using System; | ||
using SoloX.GeneratorTools.Core.CSharp.Generator.ReplacePattern; | ||
|
||
namespace SoloX.GeneratorTools.Core.CSharp.Generator.Impl | ||
{ | ||
/// <summary> | ||
/// Default resolver for replace pattern handler factory. | ||
/// </summary> | ||
public class DefaultReplacePatternResolver : AInstanceResolver, IReplacePatternResolver | ||
{ | ||
private static readonly IEnumerable<Type> DefaultTypes = new[] | ||
{ | ||
typeof(TaskValueTypeReplaceHandler), | ||
}; | ||
|
||
/// <summary> | ||
/// Setup instance with the given types to be resolved. | ||
/// </summary> | ||
/// <param name="types">Types to be resolved in selectors.</param> | ||
public DefaultReplacePatternResolver(params Type[] types) | ||
: base(DefaultTypes, types) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IReplacePatternHandlerFactory GetHandlerFactory(IDeclarationUse<SyntaxNode> replacePatternHandlerTypeUse) | ||
{ | ||
return CreateInstance<IReplacePatternHandlerFactory>(replacePatternHandlerTypeUse); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.