private Doc PrintInitializerExpressionSyntax( InitializerExpressionSyntax node) { var result = Concat( node.Kind() == SyntaxKind.ArrayInitializerExpression ? string.Empty : node.Kind() == SyntaxKind.ComplexElementInitializerExpression ? SoftLine : Line, this.PrintSyntaxToken(node.OpenBraceToken), Indent( Concat( Line, this.PrintSeparatedSyntaxList( node.Expressions, this.Print, Line ) ) ), Line, this.PrintSyntaxToken(node.CloseBraceToken) ); return(node.Parent is not ObjectCreationExpressionSyntax ? Group(result) : result); }
public static void Write( this InitializerExpressionSyntax syntax, IIndentedTextWriterWrapper textWriter, IContext context) { // Note, there are 4 different implementations using InitializerExpressionSyntax if (syntax.Kind() == SyntaxKind.ComplexElementInitializerExpression) { textWriter.Write("["); syntax.Expressions.First().Write(textWriter, context); textWriter.Write("] = "); syntax.Expressions.Last().Write(textWriter, context); return; } textWriter.Write(".__Initialize({"); if (syntax.Kind() == SyntaxKind.ArrayInitializerExpression) { if (syntax.Expressions.Any()) { textWriter.Write("[0] = "); } } else if (syntax.Expressions.Count > 1 || GetKind(syntax) == SyntaxKind.CollectionInitializerExpression) { textWriter.WriteLine(); } textWriter.Indent++; if (GetKind(syntax) == SyntaxKind.CollectionInitializerExpression) { syntax.Expressions.Write( ExpressionExtensions.Write, textWriter, context, () => textWriter.WriteLine(",")); } else { syntax.Expressions.Write(ExpressionExtensions.Write, textWriter, context); } textWriter.Indent--; if (syntax.Kind() == SyntaxKind.ArrayInitializerExpression) { } else if (syntax.Expressions.Count > 1 || GetKind(syntax) == SyntaxKind.CollectionInitializerExpression) { textWriter.WriteLine(); } textWriter.Write("})"); }
public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node) { SyntaxNode DefaultVisit() => base.VisitInitializerExpression(node); try { if (!IsDictionary(node)) { return(DefaultVisit()); } var initializerExtractionResults = node.Kind() switch { SyntaxKind.CollectionInitializerExpression => ExtractInitializersWithCollectionSyntax(node), SyntaxKind.ObjectInitializerExpression => ExtractInitializersWithObjectSyntax(node), _ => throw new InvalidKindException { Kind = node.Kind() } }; if (!initializerExtractionResults.Success) { return(DefaultVisit()); } var replacementNode = BuildReplacementSyntaxWithCollectionInitialization(initializerExtractionResults.InitializerSyntaxNodePairs); return(base.VisitInitializerExpression(replacementNode)); } catch (InvalidKindException ike) { Log.Error( $"{nameof(NormalizeDictionaryInitialization)}: {nameof(InitializerExpressionSyntax)} found with unexpected Kind {ike.Kind}"); throw; } catch (Exception e) { Log.Error(e, $"{nameof(NormalizeDictionaryInitialization)}: unknown error"); throw; } }
public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node) { if (node.Kind() != SyntaxKind.CollectionInitializerExpression) return node; var initialiserExpressions = GetInitializerItems(_nameDataPairs); var complexElementInitializerExpression = SyntaxFactory.InitializerExpression(SyntaxKind.ComplexElementInitializerExpression, initialiserExpressions); return complexElementInitializerExpression; }
public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node) { if (node.Kind() != SyntaxKind.CollectionInitializerExpression) { return(node); } var initialiserExpressions = GetInitializerItems(_icons); var complexElementInitializerExpression = SyntaxFactory.InitializerExpression(SyntaxKind.ComplexElementInitializerExpression, initialiserExpressions); return(complexElementInitializerExpression); }
public override void VisitInitializerExpression(InitializerExpressionSyntax node) { //Special-case object initializers, which look like assignments and thus confuse us: if (node.Kind() == SyntaxKind.ObjectInitializerExpression) { foreach (var initializer in node.Expressions) { var initializerAssignment = initializer as AssignmentExpressionSyntax; Visit(initializerAssignment != null ? initializerAssignment.Right : initializer); } } else { base.VisitInitializerExpression(node); } }
public static Doc Print(InitializerExpressionSyntax node) { var result = Doc.Concat( node.Kind() is (SyntaxKind.ArrayInitializerExpression or SyntaxKind.ComplexElementInitializerExpression) ? Doc.Null : Doc.Line, Token.Print(node.OpenBraceToken), Doc.Indent( Doc.Line, SeparatedSyntaxList.Print(node.Expressions, Node.Print, Doc.Line) ), Doc.Line, Token.Print(node.CloseBraceToken) ); return node.Parent is not ObjectCreationExpressionSyntax ? Doc.Group(result) : result; }
public Statement GetStatement() { var kind = initializerExpressionSyntax.Kind(); if (kind == Microsoft.CodeAnalysis.CSharp.SyntaxKind.ArrayInitializerExpression) { var statement = new ArrayCreationStatement(); statement.Type = GetTypeSyntax(initializerExpressionSyntax); statement.Bounds = new List <Statement>(); statement.Bounds.Add(new ConstantStatement(initializerExpressionSyntax.Expressions.Count)); statement.Initializers = new List <Statement>(); bool getInnerArrayLength = false; foreach (var expression in initializerExpressionSyntax.Expressions) { var innerStatement = statementInterpreterHandler.GetStatement(expression); if (innerStatement is ArrayCreationStatement arrayCreationStatement) { if (!getInnerArrayLength) { statement.Bounds.AddRange( arrayCreationStatement.Bounds.Select(x => x)); getInnerArrayLength = true; } statement.Initializers.AddRange( arrayCreationStatement.Initializers.Select(x => x)); } else { statement.Initializers.Add(innerStatement); } } return(statement); } else { var statement = new InitializerStatement(); statement.Statements = initializerExpressionSyntax.Expressions.Select(x => statementInterpreterHandler.GetStatement(x)).ToList(); return(statement); } }
private static Doc Print(InitializerExpressionSyntax node, string?groupId) { var result = Doc.Concat( groupId != null ? Doc.IfBreak(" ", Doc.Line, groupId) : Doc.Null, Token.Print(node.OpenBraceToken), Doc.Indent( Doc.Line, SeparatedSyntaxList.Print(node.Expressions, Node.Print, Doc.Line) ), Doc.Line, Token.Print(node.CloseBraceToken) ); return(node.Parent is not(ObjectCreationExpressionSyntax or ArrayCreationExpressionSyntax or ImplicitArrayCreationExpressionSyntax) && node.Kind() is not SyntaxKind.WithInitializerExpression?Doc.Group(result) : result); }
public override Ust VisitInitializerExpression(InitializerExpressionSyntax node) { Ust result = null; var children = node.Expressions.Select(e => (Expression)VisitAndReturnNullIfError(e)) .ToArray(); var textSpan = node.GetTextSpan(); if (node.Kind() == SyntaxKind.ArrayInitializerExpression) { var sizes = new List <Expression> { new IntLiteral(children.Length) }; result = new ArrayCreationExpression(new TypeToken("", textSpan), sizes, children.ToList(), textSpan); } else { result = new MultichildExpression(children, textSpan); } return(result); }
public override void VisitInitializerExpression(InitializerExpressionSyntax node) { if (lang == TargetLang.TypeScript) { if (node.Kind() == SyntaxKind.ArrayInitializerExpression) { bool is_byte_array = false; if (node.Parent.Kind() == SyntaxKind.ArrayCreationExpression && ((ArrayCreationExpressionSyntax)node.Parent).Type.ElementType.ToString() == "byte") { is_byte_array = true; } if (is_byte_array) { emit("new Uint8Array("); } emit("[ "); current_depth++; foreach (var exp in node.Expressions) { this.Visit(exp); emit(", "); } current_depth--; emit(" ]"); if (is_byte_array) { emit(")"); } } else { emit_line("{"); current_depth++; foreach (var exp in node.Expressions) { this.Visit(exp); emit_line(","); } current_depth--; emit("}"); } } else { if (node.Kind() == SyntaxKind.ArrayInitializerExpression) { emit("{ "); current_depth++; foreach (var exp in node.Expressions) { this.Visit(exp); emit(", "); } current_depth--; emit(" }"); } else { emit_line("{"); current_depth++; foreach (var exp in node.Expressions) { this.Visit(exp); emit_line(","); } current_depth--; emit("}"); } } }
public static void ComputeRefactorings( RefactoringContext context, InitializerExpressionSyntax initializer, SemanticModel semanticModel) { Debug.Assert(initializer.IsKind(SyntaxKind.ObjectInitializerExpression, SyntaxKind.WithInitializerExpression), initializer.Kind().ToString()); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(initializer.Parent, context.CancellationToken); if (typeSymbol?.IsErrorType() != false) { return; } if (typeSymbol.IsAnonymousType) { return; } int position = initializer.OpenBraceToken.Span.End; ImmutableArray <ISymbol> symbols = semanticModel.LookupSymbols(position, typeSymbol); if (!symbols.Any()) { return; } SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; if (expressions.Any()) { var initializedPropertyNames = new HashSet <string>(); foreach (ExpressionSyntax expression in expressions) { Debug.Assert(expression.IsKind(SyntaxKind.SimpleAssignmentExpression), expression.Kind().ToString()); if (expression.IsKind(SyntaxKind.SimpleAssignmentExpression)) { SimpleAssignmentExpressionInfo assignmentInfo = SyntaxInfo.SimpleAssignmentExpressionInfo((AssignmentExpressionSyntax)expression); if (assignmentInfo.Success) { ExpressionSyntax left = assignmentInfo.Left; Debug.Assert(left.IsKind(SyntaxKind.IdentifierName), left.Kind().ToString()); if (left is IdentifierNameSyntax identifierName) { initializedPropertyNames.Add(identifierName.Identifier.ValueText); } } } } Dictionary <string, IPropertySymbol> namesToProperties = null; foreach (ISymbol symbol in symbols) { if (initializedPropertyNames.Contains(symbol.Name)) { continue; } IPropertySymbol propertySymbol = GetInitializableProperty(symbol, position, semanticModel); if (propertySymbol == null) { continue; } if (namesToProperties != null) { if (namesToProperties.ContainsKey(propertySymbol.Name)) { continue; } } else { namesToProperties = new Dictionary <string, IPropertySymbol>(); } namesToProperties.Add(propertySymbol.Name, propertySymbol); } if (namesToProperties != null) { Document document = context.Document; context.RegisterRefactoring( "Initialize all properties", ct => { IEnumerable <IPropertySymbol> propertySymbols = namesToProperties.Select(f => f.Value); return(RefactorAsync(document, initializer, propertySymbols, initializeToDefault: false, ct)); }, RefactoringDescriptors.AddAllPropertiesToInitializer); } } else if (HasAccessiblePropertySetter()) { Document document = context.Document; context.RegisterRefactoring( "Initialize all properties", ct => { IEnumerable <IPropertySymbol> propertySymbols = GetInitializableProperties(initializer, symbols, semanticModel); return(RefactorAsync(document, initializer, propertySymbols, initializeToDefault: false, ct)); }, RefactoringDescriptors.AddAllPropertiesToInitializer); } bool HasAccessiblePropertySetter() { foreach (ISymbol symbol in symbols) { if (GetInitializableProperty(symbol, position, semanticModel) != null) { return(true); } } return(false); } }