A couple refactors for `RoslynUtils` and `DefaultSetterGenerator`
This commit is contained in:
parent
b4cb0b7612
commit
f656f07bfe
|
@ -6,12 +6,8 @@ using System.Threading;
|
|||
|
||||
public static class RoslynUtils
|
||||
{
|
||||
public static SyntaxNode? EnclosingTypeDeclarationSyntax(this CSharpSyntaxNode node)
|
||||
{
|
||||
var parent = node.Parent;
|
||||
while (parent is not (null or TypeDeclarationSyntax)) parent = parent.Parent;
|
||||
return parent;
|
||||
}
|
||||
public static TypeDeclarationSyntax? EnclosingTypeDeclarationSyntax(this CSharpSyntaxNode node)
|
||||
=> node.NearestAncestorOfType<TypeDeclarationSyntax>();
|
||||
|
||||
public static string GetMethodName(this ConversionOperatorDeclarationSyntax cods)
|
||||
=> cods.ImplicitOrExplicitKeyword.ToString() is "implicit"
|
||||
|
@ -132,9 +128,21 @@ public static class RoslynUtils
|
|||
SyntaxNodeAnalysisContext snac)
|
||||
=> list.Matching(targetAttrSym, snac.SemanticModel, snac.CancellationToken);
|
||||
|
||||
public static T? NearestAncestorOfType<T>(this CSharpSyntaxNode node)
|
||||
where T : CSharpSyntaxNode
|
||||
=> node.Parent?.FirstAncestorOrSelf<T>();
|
||||
|
||||
public static TextSpan Slice(this TextSpan span, int start)
|
||||
=> TextSpan.FromBounds(start: span.Start + start, end: span.End);
|
||||
|
||||
public static TextSpan Slice(this TextSpan span, int start, int length)
|
||||
=> new(start: span.Start + start, length: length);
|
||||
|
||||
public static string ToMetadataNameStr(this NameSyntax nameSyn)
|
||||
=> nameSyn switch
|
||||
{
|
||||
QualifiedNameSyntax qual => $"{qual.Left.ToMetadataNameStr()}.{qual.Right.ToMetadataNameStr()}",
|
||||
SimpleNameSyntax simple => simple.Identifier.ValueText,
|
||||
_ => throw new InvalidOperationException(),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using BizHawk.Analyzers;
|
||||
|
||||
[Generator]
|
||||
public sealed class ReflectionCacheGenerator : ISourceGenerator
|
||||
{
|
||||
|
@ -35,14 +37,8 @@ public sealed class ReflectionCacheGenerator : ISourceGenerator
|
|||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
static string Ser(NameSyntax nameSyn) => nameSyn switch
|
||||
{
|
||||
SimpleNameSyntax simple => simple.Identifier.ValueText,
|
||||
QualifiedNameSyntax qual => $"{Ser(qual.Left)}.{Ser(qual.Right)}",
|
||||
_ => throw new InvalidOperationException(),
|
||||
};
|
||||
if (_namespace != null || syntaxNode is not NamespaceDeclarationSyntax syn) return;
|
||||
var newNS = Ser(syn.Name);
|
||||
var newNS = syn.Name.ToMetadataNameStr();
|
||||
if (!newNS.StartsWith("BizHawk.", StringComparison.Ordinal)) return;
|
||||
_namespaces.Add(newNS);
|
||||
if (_namespaces.Count == SAMPLE_SIZE) _namespace = CalcNamespace();
|
||||
|
|
|
@ -5,12 +5,14 @@ using System.Collections.Immutable;
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using BizHawk.Analyzers;
|
||||
|
||||
[Generator]
|
||||
public class DefaultSetterGenerator : ISourceGenerator
|
||||
{
|
||||
public class SyntaxReceiver : ISyntaxContextReceiver
|
||||
{
|
||||
public readonly List<(ClassDeclarationSyntax, SemanticModel)> ClassDeclarations = new();
|
||||
public readonly List<(ClassDeclarationSyntax CDS, SemanticModel SemanticModel)> ClassDeclarations = new();
|
||||
|
||||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
||||
{
|
||||
|
@ -82,26 +84,26 @@ public class DefaultSetterGenerator : ISourceGenerator
|
|||
{
|
||||
return;
|
||||
}
|
||||
var consumerAttrSym = context.Compilation.GetTypeByMetadataName("BizHawk.Emulation.Common.CoreSettingsAttribute");
|
||||
if (consumerAttrSym is null) return;
|
||||
|
||||
// Generated source code
|
||||
var source = new StringBuilder(@"
|
||||
namespace BizHawk.Common
|
||||
namespace BizHawk.Emulation.Cores
|
||||
{
|
||||
public static partial class SettingsUtil
|
||||
{");
|
||||
|
||||
foreach (var (cds, semanticModel) in syntaxReceiver.ClassDeclarations)
|
||||
{
|
||||
if (cds.AttributeLists.SelectMany(e => e.Attributes)
|
||||
.Any(e => e.Name.NormalizeWhitespace().ToFullString() == "CoreSettings"))
|
||||
foreach (var (cds, semanticModel) in syntaxReceiver.ClassDeclarations
|
||||
.Where(tuple => tuple.CDS.AttributeLists.Matching(
|
||||
consumerAttrSym,
|
||||
tuple.SemanticModel,
|
||||
context.CancellationToken).Any()))
|
||||
{
|
||||
var symbol = semanticModel.GetDeclaredSymbol(cds, context.CancellationToken);
|
||||
if (symbol is not null) // probably never happens?
|
||||
{
|
||||
if (symbol is null) continue; // probably never happens?
|
||||
CreateDefaultSetter(source, symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source.Append(@"
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,6 +1,5 @@
|
|||
using System.ComponentModel;
|
||||
|
||||
using BizHawk.Common;
|
||||
using BizHawk.Emulation.Common;
|
||||
|
||||
namespace BizHawk.Emulation.Cores.Computers.Commodore64
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using System.ComponentModel;
|
||||
using BizHawk.Common;
|
||||
|
||||
using BizHawk.Emulation.Common;
|
||||
|
||||
namespace BizHawk.Emulation.Cores.Computers.MSX
|
||||
|
|
Loading…
Reference in New Issue