Exemplo n.º 1
0
 public CommonConversions(Document document, SemanticModel semanticModel,
                          TypeConversionAnalyzer typeConversionAnalyzer, SyntaxGenerator csSyntaxGenerator,
                          Compilation compilation,
                          CSharpCompilation csCompilation, ITypeContext typeContext, VisualBasicEqualityComparison visualBasicEqualityComparison)
 {
     TypeConversionAnalyzer = typeConversionAnalyzer;
     Document                      = document;
     SemanticModel                 = semanticModel;
     CsSyntaxGenerator             = csSyntaxGenerator;
     Compilation                   = compilation;
     _csCompilation                = csCompilation;
     _typeContext                  = typeContext;
     VisualBasicEqualityComparison = visualBasicEqualityComparison;
     WinformsConversions           = new WinformsConversions(typeContext);
 }
Exemplo n.º 2
0
    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 = WithDeclarationName(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), SymbolEqualityComparer.IncludeNullability)))
                {
                    // 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.MayNeedToInlinePropertyAccess(id.Parent, idSymbol) && _typeContext.HandledEventsAnalysis.ShouldGeneratePropertyFor(idSymbol.Name))
                {
                    // For C# Winforms designer, we need to use direct field access - see other usage of MayNeedToInlinePropertyAccess
                    text = "_" + text;
                }
            }