private static SyntaxNode SimplifyBlock( BlockSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken) { if (node.Statements.Count != 1) { return(node); } if (!CanHaveEmbeddedStatement(node.Parent)) { return(node); } switch (optionSet.GetOption(CSharpCodeStyleOptions.PreferBraces).Value) { case PreferBracesPreference.Always: default: return(node); case PreferBracesPreference.WhenMultiline: // Braces are optional in several scenarios for 'when_multiline', but are only automatically removed // in a subset of cases where all of the following are met: // // 1. This is an 'if' statement // 1. The 'if' statement does not have an 'else' clause and is not part of a larger 'if'/'else if'/'else' sequence // 2. The 'if' statement is not considered multiline if (!node.Parent.IsKind(SyntaxKind.IfStatement)) { // Braces are only removed for 'if' statements return(node); } if (node.Parent.IsParentKind(SyntaxKind.IfStatement, SyntaxKind.ElseClause)) { // Braces are not removed from more complicated 'if' sequences return(node); } if (!FormattingRangeHelper.AreTwoTokensOnSameLine(node.Statements[0].GetFirstToken(), node.Statements[0].GetLastToken())) { // Braces are not removed when the embedded statement is multiline return(node); } if (!FormattingRangeHelper.AreTwoTokensOnSameLine(node.Parent.GetFirstToken(), node.GetFirstToken().GetPreviousToken())) { // Braces are not removed when the part of the 'if' statement preceding the embedded statement // is multiline. return(node); } break; case PreferBracesPreference.None: break; } return(node.Statements[0]); }
static void Main(string[] args) { Console.WriteLine("Building..."); semi = SyntaxFactory.Token(SyntaxFactory.TriviaList(), SyntaxKind.SemicolonToken, SyntaxFactory.TriviaList(SyntaxFactory.EndOfLine("\n"))); CSharpParseOptions cSharpParseOptions = new CSharpParseOptions(LanguageVersion.CSharp1, DocumentationMode.Parse, SourceCodeKind.Script); tree = CSharpSyntaxTree.ParseText("", options: cSharpParseOptions) as CSharpSyntaxTree; root = tree.GetCompilationUnitRoot(); //(CompilationUnitSyntax)tree.GetRoot(); //create the block and add it var syntaxAnnotation = new SyntaxAnnotation("Block", "1"); BlockSyntax block = SyntaxFactory.Block().WithAdditionalAnnotations(syntaxAnnotation); currentBlock = syntaxAnnotation; //add block to root GlobalStatementSyntax globalStatement = SyntaxFactory.GlobalStatement(block); root = root.AddMembers(globalStatement); //root = root.WithMembers(root.Members.Add(globalStatement)); //add to first block ExpressionSyntax expressionSyntax = SyntaxFactory.IdentifierName("Print"); ArgumentListSyntax argumentList = SyntaxFactory.ParseArgumentList("(" + "arg1, \"literal1\"" + ")"); //Print("// Sebastian Marsh (001790): That’s not going to happen, Robert."); // InvocationExpressionSyntax invocationExpression = SyntaxFactory.InvocationExpression(expressionSyntax, argumentList); ExpressionStatementSyntax expressionStatement = SyntaxFactory.ExpressionStatement(invocationExpression, semi); AddToCurrentBlock(expressionStatement); //add a block to the block syntaxAnnotation = new SyntaxAnnotation("Block", "2"); BlockSyntax newBlock = SyntaxFactory.Block().WithAdditionalAnnotations(syntaxAnnotation); AddToCurrentBlock(newBlock); currentBlock = syntaxAnnotation; var variableIdentifier = SyntaxFactory.IdentifierName("vVar"); //var variableExpression = GetVariableExpression(assignValue); int value = int.Parse("1"); var variableExpression = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)); var binaryExpression = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, variableIdentifier, variableExpression); //AssignmentExpressionSyntax assignment = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, variableIdentifier, variableExpression); AssignmentExpressionSyntax assignment = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, variableIdentifier, binaryExpression); expressionStatement = SyntaxFactory.ExpressionStatement(assignment, semi); //add to the current block AddToCurrentBlock(expressionStatement); //move back a block GetPreviousBlock(); var variable = GetVariable(); SyntaxToken insertAfter = block.GetFirstToken(); SyntaxNode insertAfterNode = null; BlockSyntax theBlock = GetCurrentBlock(); foreach (var token in theBlock.DescendantTokens().Where(n => n.ValueText == "literal1")) { insertAfter = FindNextSemi(token); insertAfterNode = insertAfter.Parent; } VariableDeclaratorSyntax declarator = SyntaxFactory.VariableDeclarator(identifier: SyntaxFactory.Identifier("var12")); VariableDeclarationSyntax variableDeclaration = SyntaxFactory.VariableDeclaration(type: SyntaxFactory.ParseTypeName("String "), variables: SyntaxFactory.SeparatedList <VariableDeclaratorSyntax>().Add(declarator)); SyntaxTokenList modifiers = new SyntaxTokenList(); LocalDeclarationStatementSyntax localDeclaration = SyntaxFactory.LocalDeclarationStatement(modifiers, variableDeclaration, semi); BlockSyntax addBlock = SyntaxFactory.Block(localDeclaration); root = root.InsertNodesAfter(insertAfterNode, addBlock.ChildNodes()); //show source //block = root.DescendantNodes().OfType<BlockSyntax>().Where(n => n.HasAnnotation(currentBlock)).Single(); //Console.WriteLine("block source:"); //Console.WriteLine(block.GetText()); Console.WriteLine("root source:"); Console.WriteLine(root.GetText()); Console.WriteLine("Complete."); Console.ReadLine(); }