private static SyntaxRemoveOptions CreateSyntaxRemoveOptions( TLocalDeclarationStatement localDeclaration, IBlockFactsService blockFacts) { var removeOptions = SyntaxGenerator.DefaultRemoveOptions; if (localDeclaration != null) { if (localDeclaration.GetLeadingTrivia().Contains(t => t.IsDirective)) { removeOptions |= SyntaxRemoveOptions.KeepLeadingTrivia; } else { var statementParent = localDeclaration.Parent; if (blockFacts.IsExecutableBlock(statementParent)) { var siblings = blockFacts.GetExecutableBlockStatements(statementParent); var localDeclarationIndex = siblings.IndexOf(localDeclaration); if (localDeclarationIndex != 0) { // if we're removing the first statement in a block, then we // want to have the elastic marker on it so that the next statement // properly formats with the space left behind. But if it's // not the first statement then just keep the trivia as is // so that the statement before and after it stay appropriately // spaced apart. removeOptions &= ~SyntaxRemoveOptions.AddElasticMarker; } } } } return(removeOptions); }
protected override bool ShouldOfferFixForLocalDeclaration(IBlockFactsService blockFacts, SyntaxNode node) { // If the fix location is not for a local declaration then we can allow it (eg, when inside a for // or catch). if (node.Parent?.Parent is not LocalDeclarationStatementSyntax localDeclaration) { return(true); } // Local declarations must be parented by an executable block, or global statement, otherwise // removing them would be invalid (and more than likely crash the fixer) return(localDeclaration.Parent is GlobalStatementSyntax || blockFacts.IsExecutableBlock(localDeclaration.Parent)); }
private static bool TryGetSiblingStatement( ISyntaxFactsService syntaxFacts, IBlockFactsService blockFacts, SyntaxNode ifOrElseIf, int relativeIndex, out SyntaxNode statement) { if (syntaxFacts.IsExecutableStatement(ifOrElseIf) && blockFacts.IsExecutableBlock(ifOrElseIf.Parent)) { var blockStatements = blockFacts.GetExecutableBlockStatements(ifOrElseIf.Parent); statement = blockStatements.ElementAtOrDefault(blockStatements.IndexOf(ifOrElseIf) + relativeIndex); return(statement != null); } statement = null; return(false); }