public CommonConversions(Document document, SemanticModel semanticModel, TypeConversionAnalyzer typeConversionAnalyzer, SyntaxGenerator csSyntaxGenerator, CSharpCompilation csCompilation, ITypeContext typeContext, VisualBasicEqualityComparison visualBasicEqualityComparison) { TypeConversionAnalyzer = typeConversionAnalyzer; Document = document; _semanticModel = semanticModel; CsSyntaxGenerator = csSyntaxGenerator; _csCompilation = csCompilation; _typeContext = typeContext; VisualBasicEqualityComparison = visualBasicEqualityComparison; WinformsConversions = new WinformsConversions(typeContext); }
/// <summary> /// Make winforms designer work: https://github.com/icsharpcode/CodeConverter/issues/321 /// </summary> public SyntaxList <StatementSyntax> GetPostAssignmentStatements(Microsoft.CodeAnalysis.VisualBasic.Syntax.AssignmentStatementSyntax node, ISymbol potentialPropertySymbol) { if (WinformsConversions.MustInlinePropertyWithEventsAccess(node, potentialPropertySymbol)) { var fieldName = SyntaxFactory.IdentifierName("_" + potentialPropertySymbol.Name); var handledMethods = _handledMethodsFromPropertyWithEventName[potentialPropertySymbol.Name].ToArray(); if (handledMethods.Any()) { var postAssignmentStatements = handledMethods.SelectMany(h => h.GetPostInitializationStatements(potentialPropertySymbol.Name, fieldName)); return(SyntaxFactory.List(postAssignmentStatements)); } } return(SyntaxFactory.List <StatementSyntax>()); }
public SyntaxToken ConvertIdentifier(SyntaxToken id, bool isAttribute = false, SourceTriviaMapKind sourceTriviaMapKind = SourceTriviaMapKind.All) { string text = id.ValueText; if (id.SyntaxTree == _semanticModel.SyntaxTree) { var idSymbol = _semanticModel.GetSymbolInfo(id.Parent).Symbol ?? _semanticModel.GetDeclaredSymbol(id.Parent); if (idSymbol != null && !String.IsNullOrWhiteSpace(idSymbol.Name)) { text = WithDeclarationCasing(id, idSymbol, text); var normalizedText = text.WithHalfWidthLatinCharacters(); if (idSymbol.IsConstructor() && isAttribute) { text = idSymbol.ContainingType.Name; if (normalizedText.EndsWith("Attribute", StringComparison.OrdinalIgnoreCase)) { text = text.Remove(text.Length - "Attribute".Length); } } else if (idSymbol.IsKind(SymbolKind.Parameter) && idSymbol.ContainingSymbol.IsAccessorWithValueInCsharp() && ((idSymbol.IsImplicitlyDeclared && idSymbol.Name.WithHalfWidthLatinCharacters().Equals("value", StringComparison.OrdinalIgnoreCase)) || idSymbol.Equals(idSymbol.ContainingSymbol.GetParameters().FirstOrDefault(x => !x.IsImplicitlyDeclared)))) { // The case above is basically that if the symbol is a parameter, and the corresponding definition is a property set definition // AND the first explicitly declared parameter is this symbol, we need to replace it with value. text = "value"; } else if (normalizedText.StartsWith("_", StringComparison.OrdinalIgnoreCase) && idSymbol is IFieldSymbol propertyFieldSymbol && propertyFieldSymbol.AssociatedSymbol?.IsKind(SymbolKind.Property) == true) { text = propertyFieldSymbol.AssociatedSymbol.Name; } else if (normalizedText.EndsWith("Event", StringComparison.OrdinalIgnoreCase) && idSymbol is IFieldSymbol eventFieldSymbol && eventFieldSymbol.AssociatedSymbol?.IsKind(SymbolKind.Event) == true) { text = eventFieldSymbol.AssociatedSymbol.Name; } else if (WinformsConversions.MustInlinePropertyWithEventsAccess(id.Parent, idSymbol)) { // For C# Winforms designer, we need to use direct field access - see other usage of MustInlinePropertyWithEventsAccess text = "_" + text; } }