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 class RoslynUtils
|
||||||
{
|
{
|
||||||
public static SyntaxNode? EnclosingTypeDeclarationSyntax(this CSharpSyntaxNode node)
|
public static TypeDeclarationSyntax? EnclosingTypeDeclarationSyntax(this CSharpSyntaxNode node)
|
||||||
{
|
=> node.NearestAncestorOfType<TypeDeclarationSyntax>();
|
||||||
var parent = node.Parent;
|
|
||||||
while (parent is not (null or TypeDeclarationSyntax)) parent = parent.Parent;
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GetMethodName(this ConversionOperatorDeclarationSyntax cods)
|
public static string GetMethodName(this ConversionOperatorDeclarationSyntax cods)
|
||||||
=> cods.ImplicitOrExplicitKeyword.ToString() is "implicit"
|
=> cods.ImplicitOrExplicitKeyword.ToString() is "implicit"
|
||||||
|
@ -132,9 +128,21 @@ public static class RoslynUtils
|
||||||
SyntaxNodeAnalysisContext snac)
|
SyntaxNodeAnalysisContext snac)
|
||||||
=> list.Matching(targetAttrSym, snac.SemanticModel, snac.CancellationToken);
|
=> 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)
|
public static TextSpan Slice(this TextSpan span, int start)
|
||||||
=> TextSpan.FromBounds(start: span.Start + start, end: span.End);
|
=> TextSpan.FromBounds(start: span.Start + start, end: span.End);
|
||||||
|
|
||||||
public static TextSpan Slice(this TextSpan span, int start, int length)
|
public static TextSpan Slice(this TextSpan span, int start, int length)
|
||||||
=> new(start: span.Start + start, length: 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.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
using BizHawk.Analyzers;
|
||||||
|
|
||||||
[Generator]
|
[Generator]
|
||||||
public sealed class ReflectionCacheGenerator : ISourceGenerator
|
public sealed class ReflectionCacheGenerator : ISourceGenerator
|
||||||
{
|
{
|
||||||
|
@ -35,14 +37,8 @@ public sealed class ReflectionCacheGenerator : ISourceGenerator
|
||||||
|
|
||||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
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;
|
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;
|
if (!newNS.StartsWith("BizHawk.", StringComparison.Ordinal)) return;
|
||||||
_namespaces.Add(newNS);
|
_namespaces.Add(newNS);
|
||||||
if (_namespaces.Count == SAMPLE_SIZE) _namespace = CalcNamespace();
|
if (_namespaces.Count == SAMPLE_SIZE) _namespace = CalcNamespace();
|
||||||
|
|
|
@ -5,12 +5,14 @@ using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
using BizHawk.Analyzers;
|
||||||
|
|
||||||
[Generator]
|
[Generator]
|
||||||
public class DefaultSetterGenerator : ISourceGenerator
|
public class DefaultSetterGenerator : ISourceGenerator
|
||||||
{
|
{
|
||||||
public class SyntaxReceiver : ISyntaxContextReceiver
|
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)
|
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
||||||
{
|
{
|
||||||
|
@ -82,25 +84,25 @@ public class DefaultSetterGenerator : ISourceGenerator
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
var consumerAttrSym = context.Compilation.GetTypeByMetadataName("BizHawk.Emulation.Common.CoreSettingsAttribute");
|
||||||
|
if (consumerAttrSym is null) return;
|
||||||
|
|
||||||
// Generated source code
|
// Generated source code
|
||||||
var source = new StringBuilder(@"
|
var source = new StringBuilder(@"
|
||||||
namespace BizHawk.Common
|
namespace BizHawk.Emulation.Cores
|
||||||
{
|
{
|
||||||
public static partial class SettingsUtil
|
public static partial class SettingsUtil
|
||||||
{");
|
{");
|
||||||
|
|
||||||
foreach (var (cds, semanticModel) in syntaxReceiver.ClassDeclarations)
|
foreach (var (cds, semanticModel) in syntaxReceiver.ClassDeclarations
|
||||||
|
.Where(tuple => tuple.CDS.AttributeLists.Matching(
|
||||||
|
consumerAttrSym,
|
||||||
|
tuple.SemanticModel,
|
||||||
|
context.CancellationToken).Any()))
|
||||||
{
|
{
|
||||||
if (cds.AttributeLists.SelectMany(e => e.Attributes)
|
var symbol = semanticModel.GetDeclaredSymbol(cds, context.CancellationToken);
|
||||||
.Any(e => e.Name.NormalizeWhitespace().ToFullString() == "CoreSettings"))
|
if (symbol is null) continue; // probably never happens?
|
||||||
{
|
CreateDefaultSetter(source, symbol);
|
||||||
var symbol = semanticModel.GetDeclaredSymbol(cds, context.CancellationToken);
|
|
||||||
if (symbol is not null) // probably never happens?
|
|
||||||
{
|
|
||||||
CreateDefaultSetter(source, symbol);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
source.Append(@"
|
source.Append(@"
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,6 +1,5 @@
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
using BizHawk.Common;
|
|
||||||
using BizHawk.Emulation.Common;
|
using BizHawk.Emulation.Common;
|
||||||
|
|
||||||
namespace BizHawk.Emulation.Cores.Computers.Commodore64
|
namespace BizHawk.Emulation.Cores.Computers.Commodore64
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using BizHawk.Common;
|
|
||||||
using BizHawk.Emulation.Common;
|
using BizHawk.Emulation.Common;
|
||||||
|
|
||||||
namespace BizHawk.Emulation.Cores.Computers.MSX
|
namespace BizHawk.Emulation.Cores.Computers.MSX
|
||||||
|
|
Loading…
Reference in New Issue