public static void Analyze(SyntaxNodeAnalysisContext context, RegionDirectiveTriviaSyntax region) { if (region.IsKind(SyntaxKind.RegionDirectiveTrivia)) { List <DirectiveTriviaSyntax> relatedDirectives = region.GetRelatedDirectives(); if (relatedDirectives.Count == 2 && relatedDirectives[1].IsKind(SyntaxKind.EndRegionDirectiveTrivia)) { DirectiveTriviaSyntax endRegion = relatedDirectives[1]; if (endRegion.IsKind(SyntaxKind.EndRegionDirectiveTrivia)) { SyntaxTrivia trivia = region.ParentTrivia; SyntaxTriviaList list = trivia.GetContainingList(); if (list.Any()) { EndRegionDirectiveTriviaSyntax endRegion2 = FindEndRegion(list, list.IndexOf(trivia)); if (endRegion == endRegion2) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveEmptyRegion, region.GetLocation(), endRegion.GetLocation()); } } } } } }
private static Task <Document> RefactorAsync( Document document, PragmaWarningDirectiveTriviaSyntax directive, CancellationToken cancellationToken) { SyntaxTrivia trivia = directive.ParentTrivia; SyntaxTriviaList list = trivia.GetContainingList(); int index = list.IndexOf(trivia); int start = directive.EndOfDirectiveToken.SpanStart; StringBuilder sb = StringBuilderCache.GetInstance(); int i = index + 1; SyntaxKind disableOrRestoreKind = directive.DisableOrRestoreKeyword.Kind(); int end = start; bool addComma = !directive.ErrorCodes.HasTrailingSeparator(); while (i < list.Count) { SyntaxTrivia trivia2 = list[i]; if (trivia2.IsWhitespaceOrEndOfLineTrivia()) { i++; continue; } if (trivia2.GetStructure() is PragmaWarningDirectiveTriviaSyntax directive2 && disableOrRestoreKind == directive2.DisableOrRestoreKeyword.Kind()) { if (addComma) { sb.Append(","); } sb.Append(" "); SeparatedSyntaxList <ExpressionSyntax> errorCodes = directive2.ErrorCodes; sb.Append(errorCodes.ToString()); addComma = !errorCodes.HasTrailingSeparator(); end = directive2.ErrorCodes.Span.End; } i++; } return(document.WithTextChangeAsync( TextSpan.FromBounds(start, end), StringBuilderCache.GetStringAndFree(sb), cancellationToken)); }
private void AnalyzeRegionDirectiveTrivia(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var region = (RegionDirectiveTriviaSyntax)context.Node; if (region.IsKind(SyntaxKind.RegionDirectiveTrivia)) { List <DirectiveTriviaSyntax> relatedDirectives = region.GetRelatedDirectives(); if (relatedDirectives.Count == 2 && relatedDirectives[1].IsKind(SyntaxKind.EndRegionDirectiveTrivia)) { DirectiveTriviaSyntax endRegion = relatedDirectives[1]; if (endRegion.IsKind(SyntaxKind.EndRegionDirectiveTrivia)) { SyntaxTrivia trivia = region.ParentTrivia; SyntaxTriviaList list = trivia.GetContainingList(); if (list.Any()) { EndRegionDirectiveTriviaSyntax endRegion2 = FindEndRegion(list, list.IndexOf(trivia)); if (endRegion == endRegion2) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveEmptyRegion, region.GetLocation(), endRegion.GetLocation()); } } } } } }