public static CodeClassItem MapClass(VisualBasicSyntax.TypeBlockSyntax member, CodeViewUserControl control, SemanticModel semanticModel, SyntaxTree tree) { if (member == null) { return(null); } var item = BaseMapper.MapBase <CodeClassItem>(member, member.BlockStatement.Identifier, member.BlockStatement.Modifiers, control, semanticModel); item.Kind = CodeItemKindEnum.Class; item.Moniker = IconMapper.MapMoniker(item.Kind, item.Access); item.Parameters = MapInheritance(member); item.BorderBrush = ColorHelper.ToBrush(Colors.DarkGray); item.Tooltip = TooltipMapper.Map(item.Access, string.Empty, item.Name, item.Parameters); var regions = RegionMapper.MapRegions(tree, member.Span); var implementedInterfaces = InterfaceMapper.MapImplementedInterfaces(member, semanticModel); foreach (var classMember in member.Members) { var memberItem = SyntaxMapper.MapMember(classMember); if (memberItem != null && !InterfaceMapper.IsPartOfImplementedInterface(implementedInterfaces, memberItem) && !RegionMapper.AddToRegion(regions, memberItem)) { item.Members.Add(memberItem); } } // Add implemented interfaces to class or region if they have a interface member inside them if (implementedInterfaces.Any()) { foreach (var interfaceItem in implementedInterfaces) { if (interfaceItem.Members.Any()) { if (!RegionMapper.AddToRegion(regions, interfaceItem)) { item.Members.Add(interfaceItem); } } } } // Add regions to class if they have a region member inside them if (regions.Any()) { foreach (var region in regions) { if (region.Members.Any()) { item.Members.Add(region); } } } return(item); }
private CsSyntax.BaseTypeDeclarationSyntax WithTypeBlockTrivia(VbSyntax.TypeBlockSyntax sourceNode, CsSyntax.BaseTypeDeclarationSyntax destNode) { var beforeOpenBrace = destNode.OpenBraceToken.GetPreviousToken(); var withAnnotation = TriviaConverter.WithDelegateToParentAnnotation(sourceNode.BlockStatement, beforeOpenBrace); withAnnotation = TriviaConverter.WithDelegateToParentAnnotation(sourceNode.Inherits, withAnnotation); withAnnotation = TriviaConverter.WithDelegateToParentAnnotation(sourceNode.Implements, withAnnotation); return(destNode.ReplaceToken(beforeOpenBrace, withAnnotation)); }
private static bool IsBestPartToAddParameterlessConstructor(VBSyntax.TypeBlockSyntax typeSyntax, INamedTypeSymbol namedTypeSybol) { if (namedTypeSybol == null) { return(false); } var bestPartToAddTo = namedTypeSybol.DeclaringSyntaxReferences .OrderByDescending(l => l.SyntaxTree.FilePath?.IsGeneratedFile() == false).ThenBy(l => l.GetSyntax() is VBSyntax.TypeBlockSyntax tbs && HasAttribute(tbs, "DesignerGenerated")) .First(); return(typeSyntax.OverlapsWith(bestPartToAddTo)); }
public AdditionalInitializers(VBSyntax.TypeBlockSyntax typeSyntax, INamedTypeSymbol namedTypeSybol, Compilation vbCompilation) { var(instanceConstructors, staticConstructors) = namedTypeSybol.GetDeclaredConstructorsInAllParts(); var isBestPartToAddParameterlessConstructor = IsBestPartToAddParameterlessConstructor(typeSyntax, namedTypeSybol); _shouldAddInstanceConstructor = !instanceConstructors.Any() && isBestPartToAddParameterlessConstructor; _shouldAddStaticConstructor = !staticConstructors.Any() && isBestPartToAddParameterlessConstructor; IsBestPartToAddTypeInit = isBestPartToAddParameterlessConstructor; HasInstanceConstructorsOutsideThisPart = instanceConstructors.Any(c => c.DeclaringSyntaxReferences.Any( reference => !typeSyntax.OverlapsWith(reference) )) || !instanceConstructors.Any() && !isBestPartToAddParameterlessConstructor; RequiresInitializeComponent = namedTypeSybol.IsDesignerGeneratedTypeWithInitializeComponent(vbCompilation); }
private static string MapInheritance(VisualBasicSyntax.TypeBlockSyntax member) { if (member?.Inherits == null) { return(string.Empty); } var inheritanceList = new List <string>(); foreach (var item in member.Inherits) { inheritanceList.AddRange(item.Types.Select(t => t.ToString())); } return(!inheritanceList.Any() ? string.Empty : $" : {string.Join(", ", inheritanceList)}"); }
private static bool HasAttribute(VBSyntax.TypeBlockSyntax tbs, string attributeName) { return(tbs.BlockStatement.AttributeLists.Any(list => list.Attributes.Any(a => a.Name.GetText().ToString().Contains(attributeName)))); }