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()); } } } } } }
/// <summary> /// Checks if a region is completely part of a body. That means that the <c>#region</c> and <c>#endregion</c> /// tags both have to have a common <see cref="BlockSyntax"/> as one of their ancestors. /// </summary> /// <param name="regionSyntax">The <see cref="RegionDirectiveTriviaSyntax"/> that should be analyzed.</param> /// <returns><see langword="true"/>, if both tags have a common <see cref="BlockSyntax"/> as one of their /// ancestors; otherwise, <see langword="false"/>.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="regionSyntax"/> is <see langword="null"/>. /// </exception> internal static bool IsCompletelyContainedInBody(RegionDirectiveTriviaSyntax regionSyntax) { if (regionSyntax == null) { throw new ArgumentNullException(nameof(regionSyntax)); } BlockSyntax syntax = null; foreach (var directive in regionSyntax.GetRelatedDirectives()) { BlockSyntax blockSyntax = directive.AncestorsAndSelf().OfType <BlockSyntax>().LastOrDefault(); if (blockSyntax == null) { return(false); } else if (syntax == null) { syntax = blockSyntax; } else if (blockSyntax != syntax) { return(false); } } return(true); }
public static async Task <Document> RemoveRegionAsync( this Document document, RegionDirectiveTriviaSyntax regionDirective, CancellationToken cancellationToken = default(CancellationToken)) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (regionDirective == null) { throw new ArgumentNullException(nameof(regionDirective)); } List <DirectiveTriviaSyntax> list = regionDirective.GetRelatedDirectives(); if (list.Count == 2 && list[1].IsKind(SyntaxKind.EndRegionDirectiveTrivia)) { var endRegionDirective = (EndRegionDirectiveTriviaSyntax)list[1]; return(await RemoveRegionAsync(document, regionDirective, endRegionDirective, cancellationToken).ConfigureAwait(false)); } return(document); }
/// <summary> /// Checks if a region is completely part of a body. That means that the <c>#region</c> and <c>#endregion</c> /// tags both have to have a common <see cref="BlockSyntax"/> as one of their ancestors. /// </summary> /// <param name="regionSyntax">The <see cref="RegionDirectiveTriviaSyntax"/> that should be analyzed.</param> /// <returns><see langword="true"/>, if both tags have a common <see cref="BlockSyntax"/> as one of their /// ancestors; otherwise, <see langword="false"/>.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="regionSyntax"/> is <see langword="null"/>. /// </exception> internal static bool IsCompletelyContainedInBody(RegionDirectiveTriviaSyntax regionSyntax) { if (regionSyntax == null) { throw new ArgumentNullException(nameof(regionSyntax)); } BlockSyntax syntax = null; foreach (var directive in regionSyntax.GetRelatedDirectives()) { BlockSyntax blockSyntax = directive.AncestorsAndSelf().OfType<BlockSyntax>().LastOrDefault(); if (blockSyntax == null) { return false; } else if (syntax == null) { syntax = blockSyntax; } else if (blockSyntax != syntax) { return false; } } return true; }
public static async Task <Document> RemoveRegionAsync( Document document, RegionDirectiveTriviaSyntax regionDirective, CancellationToken cancellationToken = default(CancellationToken)) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (regionDirective == null) { throw new ArgumentNullException(nameof(regionDirective)); } List <DirectiveTriviaSyntax> list = regionDirective.GetRelatedDirectives(); if (list.Count == 2 && list[1].IsKind(SyntaxKind.EndRegionDirectiveTrivia)) { var endRegionDirective = (EndRegionDirectiveTriviaSyntax)list[1]; SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); int startLine = regionDirective.GetSpanStartLine(); int endLine = endRegionDirective.GetSpanEndLine(); TextLineCollection lines = sourceText.Lines; TextSpan span = TextSpan.FromBounds( lines[startLine].Start, lines[endLine].EndIncludingLineBreak); var textChange = new TextChange(span, string.Empty); SourceText newSourceText = sourceText.WithChanges(textChange); return(document.WithText(newSourceText)); } return(document); }
internal static RegionInfo Create(RegionDirectiveTriviaSyntax regionDirective) { if (regionDirective == null) { return(Default); } List <DirectiveTriviaSyntax> list = regionDirective.GetRelatedDirectives(); if (list.Count != 2) { return(Default); } if (list[1].Kind() != SyntaxKind.EndRegionDirectiveTrivia) { return(Default); } return(new RegionInfo(regionDirective, (EndRegionDirectiveTriviaSyntax)list[1])); }
private static EndRegionDirectiveTriviaSyntax GetEndRegion(RegionDirectiveTriviaSyntax region) { return(region.GetRelatedDirectives().Skip(1).FirstOrDefault() as EndRegionDirectiveTriviaSyntax); }