protected override SyntaxNode ConvertForNode( ForStatementSyntax forStatement, TypeSyntax typeNode, SyntaxToken foreachIdentifier, ExpressionSyntax collectionExpression, ITypeSymbol iterationVariableType, OptionSet optionSet) { if (typeNode == null) { // types are not apparent in foreach statements. var isBuiltInTypeContext = TypeStyleHelper.IsBuiltInType(iterationVariableType); if (TypeStyleHelper.IsImplicitStylePreferred( optionSet, isBuiltInTypeContext, isTypeApparentContext: false)) { typeNode = SyntaxFactory.IdentifierName("var"); } else { typeNode = (TypeSyntax)CSharpSyntaxGenerator.Instance.TypeExpression(iterationVariableType); } } return(SyntaxFactory.ForEachStatement( SyntaxFactory.Token(SyntaxKind.ForEachKeyword).WithTriviaFrom(forStatement.ForKeyword), forStatement.OpenParenToken, typeNode, foreachIdentifier, SyntaxFactory.Token(SyntaxKind.InKeyword), collectionExpression, forStatement.CloseParenToken, forStatement.Statement)); }
/// <summary> /// Returns true if type information could be gleaned by simply looking at the given statement. /// This typically means that the type name occurs in right hand side of an assignment. /// </summary> private bool IsTypeApparentInDeclaration(VariableDeclarationSyntax variableDeclaration, SemanticModel semanticModel, TypeStylePreference stylePreferences, CancellationToken cancellationToken) { var initializer = variableDeclaration.Variables.Single().Initializer; var initializerExpression = GetInitializerExpression(initializer.Value); var declaredTypeSymbol = semanticModel.GetTypeInfo(variableDeclaration.Type, cancellationToken).Type; return(TypeStyleHelper.IsTypeApparentInAssignmentExpression(stylePreferences, initializerExpression, semanticModel, cancellationToken, declaredTypeSymbol)); }
private TypeSyntax GetDeclarationType( TypeSyntax type, bool useVarWhenDeclaringLocals, bool useImplicitTypeForIntrinsicTypes) { if (useVarWhenDeclaringLocals) { if (useImplicitTypeForIntrinsicTypes || !TypeStyleHelper.IsPredefinedType(type)) { return(SyntaxFactory.IdentifierName("var")); } } return(type); }
/// <summary> /// Returns true if type information could be gleaned by simply looking at the given statement. /// This typically means that the type name occurs in right hand side of an assignment. /// </summary> private bool IsTypeApparentInDeclaration(VariableDeclarationSyntax variableDeclaration, SemanticModel semanticModel, TypeStylePreference stylePreferences, CancellationToken cancellationToken) { var initializer = variableDeclaration.Initializer; if (initializer == null) { return(false); } var initializerExpression = CSharpUseImplicitTypeHelper.GetInitializerExpression(initializer.Value); var declaredTypeSymbol = semanticModel.GetTypeInfo(variableDeclaration.Type.StripRefIfNeeded(), cancellationToken).Type; return(TypeStyleHelper.IsTypeApparentInAssignmentExpression(stylePreferences, initializerExpression, semanticModel, cancellationToken, declaredTypeSymbol)); }
private TypeSyntax GetTypeSyntax(SemanticDocument document, DocumentOptionSet options, ExpressionSyntax expression, bool isConstant, CancellationToken cancellationToken) { var typeSymbol = GetTypeSymbol(document, expression, cancellationToken); if (typeSymbol.ContainsAnonymousType()) { return(SyntaxFactory.IdentifierName("var")); } if (!isConstant && CanUseVar(typeSymbol) && TypeStyleHelper.IsImplicitTypePreferred(expression, document.SemanticModel, options, cancellationToken)) { return(SyntaxFactory.IdentifierName("var")); } return(typeSymbol.GenerateTypeSyntax()); }
private static TypeSyntax GenerateTypeSyntax(ITypeSymbol type, OptionSet options, ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken) { // if there isn't a semantic model, we cannot perform further analysis. if (semanticModel != null) { if (type.ContainsAnonymousType()) { return(SyntaxFactory.IdentifierName("var")); } if (type.TypeKind != TypeKind.Delegate && TypeStyleHelper.IsImplicitTypePreferred(expression, semanticModel, options, cancellationToken)) { return(SyntaxFactory.IdentifierName("var")); } } return(type.GenerateTypeSyntax()); }
/// <summary> /// checks if the type represented by the given symbol is one of the /// simple types defined in the compiler. /// </summary> /// <remarks> /// From the IDE perspective, we also include object and string to be simplified /// to var. <see cref="SyntaxFacts.IsPredefinedType(SyntaxKind)"/> considers string /// and object but the compiler's implementation of IsIntrinsicType does not. /// </remarks> private bool IsPredefinedTypeInDeclaration(SyntaxNode declarationStatement) { return(TypeStyleHelper.IsPredefinedType( GetTypeSyntaxFromDeclaration(declarationStatement))); }