mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-28 16:13:28 +08:00
refactor(generator): 重构上下文获取生成器的代码结构
- 将 ContextSymbols 的创建提取为独立方法 CreateContextSymbols - 将源码生成逻辑提取为独立方法 GenerateSources - 将绑定收集逻辑提取为独立方法 CollectBindings - 将显式绑定添加逻辑提取为独立方法 AddExplicitBindings - 将推断绑定添加逻辑提取为独立方法 AddInferredBindings - 将绑定推断检查逻辑提取为独立方法 CanInferBinding - 优化了代码组织结构和可读性
This commit is contained in:
parent
78af119f38
commit
ee2936e0a2
@ -171,11 +171,22 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
|
||||
|
||||
var descriptors = ResolveBindingDescriptors(compilation);
|
||||
var getAllAttribute = compilation.GetTypeByMetadataName(GetAllAttributeMetadataName);
|
||||
|
||||
if (descriptors.Length == 0 && getAllAttribute is null)
|
||||
return;
|
||||
|
||||
var symbols = new ContextSymbols(
|
||||
var symbols = CreateContextSymbols(compilation);
|
||||
var workItems = CollectWorkItems(
|
||||
fieldCandidates,
|
||||
typeCandidates,
|
||||
descriptors,
|
||||
getAllAttribute);
|
||||
|
||||
GenerateSources(context, descriptors, symbols, workItems);
|
||||
}
|
||||
|
||||
private static ContextSymbols CreateContextSymbols(Compilation compilation)
|
||||
{
|
||||
return new ContextSymbols(
|
||||
compilation.GetTypeByMetadataName(ContextAwareAttributeMetadataName),
|
||||
compilation.GetTypeByMetadataName(IContextAwareMetadataName),
|
||||
compilation.GetTypeByMetadataName(ContextAwareBaseMetadataName),
|
||||
@ -184,21 +195,51 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
|
||||
compilation.GetTypeByMetadataName(IUtilityMetadataName),
|
||||
compilation.GetTypeByMetadataName(IReadOnlyListMetadataName),
|
||||
compilation.GetTypeByMetadataName(GodotNodeMetadataName));
|
||||
}
|
||||
|
||||
var workItems = CollectWorkItems(
|
||||
fieldCandidates,
|
||||
typeCandidates,
|
||||
descriptors,
|
||||
getAllAttribute);
|
||||
|
||||
private static void GenerateSources(
|
||||
SourceProductionContext context,
|
||||
ImmutableArray<ResolvedBindingDescriptor> descriptors,
|
||||
ContextSymbols symbols,
|
||||
Dictionary<INamedTypeSymbol, TypeWorkItem> workItems)
|
||||
{
|
||||
foreach (var workItem in workItems.Values)
|
||||
{
|
||||
if (!CanGenerateForType(context, workItem, symbols))
|
||||
continue;
|
||||
|
||||
var bindings = CollectBindings(context, workItem, descriptors, symbols);
|
||||
if (bindings.Count == 0 && workItem.GetAllDeclaration is null)
|
||||
continue;
|
||||
|
||||
var source = GenerateSource(workItem.TypeSymbol, bindings);
|
||||
context.AddSource(GetHintName(workItem.TypeSymbol), source);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<BindingInfo> CollectBindings(
|
||||
SourceProductionContext context,
|
||||
TypeWorkItem workItem,
|
||||
ImmutableArray<ResolvedBindingDescriptor> descriptors,
|
||||
ContextSymbols symbols)
|
||||
{
|
||||
var bindings = new List<BindingInfo>();
|
||||
var explicitFields = new HashSet<IFieldSymbol>(SymbolEqualityComparer.Default);
|
||||
|
||||
AddExplicitBindings(context, workItem, descriptors, symbols, bindings, explicitFields);
|
||||
AddInferredBindings(context, workItem, symbols, bindings, explicitFields);
|
||||
|
||||
return bindings;
|
||||
}
|
||||
|
||||
private static void AddExplicitBindings(
|
||||
SourceProductionContext context,
|
||||
TypeWorkItem workItem,
|
||||
ImmutableArray<ResolvedBindingDescriptor> descriptors,
|
||||
ContextSymbols symbols,
|
||||
ICollection<BindingInfo> bindings,
|
||||
ISet<IFieldSymbol> explicitFields)
|
||||
{
|
||||
foreach (var candidate in workItem.FieldCandidates
|
||||
.OrderBy(static candidate => candidate.Variable.SpanStart)
|
||||
.ThenBy(static candidate => candidate.FieldSymbol.Name, StringComparer.Ordinal))
|
||||
@ -228,31 +269,25 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
|
||||
|
||||
bindings.Add(binding);
|
||||
}
|
||||
}
|
||||
|
||||
if (workItem.GetAllDeclaration is not null)
|
||||
private static void AddInferredBindings(
|
||||
SourceProductionContext context,
|
||||
TypeWorkItem workItem,
|
||||
ContextSymbols symbols,
|
||||
ICollection<BindingInfo> bindings,
|
||||
ISet<IFieldSymbol> explicitFields)
|
||||
{
|
||||
if (workItem.GetAllDeclaration is null)
|
||||
return;
|
||||
|
||||
foreach (var field in GetAllFields(workItem.TypeSymbol))
|
||||
{
|
||||
if (explicitFields.Contains(field))
|
||||
continue;
|
||||
|
||||
if (field.IsStatic)
|
||||
{
|
||||
ReportFieldDiagnostic(
|
||||
context,
|
||||
ContextGetDiagnostics.StaticFieldNotSupported,
|
||||
field);
|
||||
if (!CanInferBinding(context, field))
|
||||
continue;
|
||||
}
|
||||
|
||||
if (field.IsReadOnly)
|
||||
{
|
||||
ReportFieldDiagnostic(
|
||||
context,
|
||||
ContextGetDiagnostics.ReadOnlyFieldNotSupported,
|
||||
field);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!TryCreateInferredBinding(field, symbols, out var binding))
|
||||
continue;
|
||||
@ -261,12 +296,25 @@ public sealed class ContextGetGenerator : IIncrementalGenerator
|
||||
}
|
||||
}
|
||||
|
||||
if (bindings.Count == 0 && workItem.GetAllDeclaration is null)
|
||||
continue;
|
||||
|
||||
var source = GenerateSource(workItem.TypeSymbol, bindings);
|
||||
context.AddSource(GetHintName(workItem.TypeSymbol), source);
|
||||
private static bool CanInferBinding(SourceProductionContext context, IFieldSymbol field)
|
||||
{
|
||||
if (field.IsStatic)
|
||||
{
|
||||
ReportFieldDiagnostic(
|
||||
context,
|
||||
ContextGetDiagnostics.StaticFieldNotSupported,
|
||||
field);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!field.IsReadOnly)
|
||||
return true;
|
||||
|
||||
ReportFieldDiagnostic(
|
||||
context,
|
||||
ContextGetDiagnostics.ReadOnlyFieldNotSupported,
|
||||
field);
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Dictionary<INamedTypeSymbol, TypeWorkItem> CollectWorkItems(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user