private CodeFile ProcessClass(INamedTypeSymbol classSymbol, CodeWriter codeWriter) { if (!classSymbol.HasAttribute(typeof(AutoConstructorAttribute))) { return(null); } var nameMapper = GetSymbolNameMapper(codeWriter.Compilation, classSymbol); if (!nameMapper.Any()) { return(null); } var isDependencyInjection = codeWriter.Compilation.ReferencedAssemblyNames .Any(p => p.Name == "Microsoft.Extensions.DependencyInjection.Abstractions"); var nullChecked = GetNullCheckValue(classSymbol); CsharpCodeBuilder builder = new CsharpCodeBuilder(); AppendNamespace(classSymbol, builder); AppendClassDefinition(classSymbol, builder); AppendPublicCtor(classSymbol, nameMapper, isDependencyInjection, nullChecked, builder); builder.EndAllSegments(); return(new CodeFile { BasicName = classSymbol.GetCodeFileBasicName(), Content = builder.ToString(), }); }
private CodeFile ProcessClass(INamedTypeSymbol classSymbol, CodeWriter codeWriter) { if (!classSymbol.HasAttribute(typeof(ConvertToAttribute))) { return(null); } CsharpCodeBuilder codeBuilder = new CsharpCodeBuilder(); AppendUsingLines(classSymbol, codeBuilder); AppendNamespace(classSymbol, codeBuilder); AppendClassDefinition(classSymbol, codeBuilder); foreach (var convertToAttr in classSymbol.GetAttributes()) { if (!convertToAttr.AttributeClass.SafeEquals(typeof(ConvertToAttribute))) { continue; } var convertMappingInfo = ConvertMappingInfo.FromAttributeData(convertToAttr); if (ConvertMappingInfo.CanMappingSubObject(convertMappingInfo.SourceType, convertMappingInfo.TargetType)) { AppendConvertToFunctions(new ConvertContext( classSymbol, codeWriter.Compilation, codeBuilder, convertMappingInfo)); } else { // TOTO report error } } codeBuilder.EndAllSegments(); return(new CodeFile { BasicName = classSymbol.GetCodeFileBasicName(), Content = codeBuilder.ToString(), }); }
private CodeFile ProcessClass(INamedTypeSymbol classSymbol, CodeWriter codeWriter) { if (!classSymbol.HasAttribute(typeof(SingletonPatternAttribute))) { return(null); } if (classSymbol.Constructors.Any(p => p.DeclaringSyntaxReferences.Count() > 0)) { codeWriter.Context.ReportDiagnostic(KnifeDiagnostic.Singleton.AlreadyExistsConstructor(classSymbol)); } CsharpCodeBuilder codeBuilder = new CsharpCodeBuilder(); AppendUsingLines(classSymbol, codeBuilder); AppendNamespace(classSymbol, codeBuilder); AppendClassDefinition(classSymbol, codeBuilder); AppendSingletonBody(classSymbol, codeBuilder); codeBuilder.EndAllSegments(); return(new CodeFile { BasicName = classSymbol.GetCodeFileBasicName(), Content = codeBuilder.ToString(), }); }
private CodeFile ProcessClass(INamedTypeSymbol classSymbol, List <IFieldSymbol> fields, CodeWriter codeWriter) { var compilation = codeWriter.Compilation; INamedTypeSymbol notifySymbol = compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged"); var classSymbols = classSymbol.GetContainerClassChains(); CsharpCodeBuilder codeBuilder = new CsharpCodeBuilder(); var allNamespaces = new HashSet <string>(); allNamespaces.Add("System.ComponentModel"); foreach (var usingNamespace in allNamespaces.OrderBy(p => p)) { codeBuilder.AppendCodeLines($"using {usingNamespace};"); } //codeBuilder.AppendCodeLines($@"using System.ComponentModel;"); if (!classSymbol.ContainingNamespace.IsGlobalNamespace) { codeBuilder.AppendCodeLines($"namespace {classSymbol.ContainingNamespace.ToDisplayString()}"); codeBuilder.BeginSegment(); } foreach (var parentClass in classSymbols) { if (parentClass != classSymbol) { codeBuilder.AppendCodeLines($@"partial class {parentClass.GetClassSymbolDisplayText()}"); codeBuilder.BeginSegment(); } } if (!classSymbol.AllInterfaces.Contains(notifySymbol)) { codeBuilder.AppendCodeLines( $@"partial class {classSymbol.GetClassSymbolDisplayText()} : {notifySymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}"); codeBuilder.BeginSegment(); codeBuilder.AppendCodeLines("public event PropertyChangedEventHandler PropertyChanged;"); } else { codeBuilder.AppendCodeLines($@"partial class {classSymbol.Name}"); codeBuilder.BeginSegment(); } // create properties for each field foreach (IFieldSymbol fieldSymbol in fields) { ProcessField(codeBuilder, fieldSymbol, codeWriter); } codeBuilder.EndAllSegments(); return(new CodeFile { BasicName = classSymbol.GetCodeFileBasicName(), Content = codeBuilder.ToString(), }); }