Exemple #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            EndRegionDirectiveTriviaSyntax endRegionDirective = root
                                                                .FindNode(context.Span, findInsideTrivia: true, getInnermostNodeForTie: true)?
                                                                .FirstAncestorOrSelf <EndRegionDirectiveTriviaSyntax>();

            Debug.Assert(endRegionDirective != null, $"{nameof(endRegionDirective)} is null");

            if (endRegionDirective == null)
            {
                return;
            }

            RegionDirectiveTriviaSyntax regionDirective = endRegionDirective.GetRegionDirective();

            SyntaxTrivia trivia = regionDirective.GetPreprocessingMessageTrivia();

            CodeAction codeAction = CodeAction.Create(
                (trivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
                    ? "Add region name to #endregion"
                    : "Remove region name from #endregion",
                cancellationToken => AddOrRemoveRegionNameRefactoring.RefactorAsync(context.Document, endRegionDirective, trivia, cancellationToken),
                DiagnosticIdentifiers.AddOrRemoveRegionName + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        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());
                            }
                        }
                    }
                }
            }
        }
        public static async Task <Document> RemoveRegionAsync(
            this Document document,
            EndRegionDirectiveTriviaSyntax endRegionDirective,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (endRegionDirective == null)
            {
                throw new ArgumentNullException(nameof(endRegionDirective));
            }

            List <DirectiveTriviaSyntax> list = endRegionDirective.GetRelatedDirectives();

            if (list.Count == 2 &&
                list[0].IsKind(SyntaxKind.RegionDirectiveTrivia))
            {
                var regionDirective = (RegionDirectiveTriviaSyntax)list[0];

                return(await RemoveRegionAsync(document, regionDirective, endRegionDirective, cancellationToken).ConfigureAwait(false));
            }

            return(document);
        }
Exemple #4
0
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     // if (debug) Console.WriteLine(node.ToFullString());
     // Todo("EndRegionDirectiveTrivia"); var nl = OurLine.NewLine(LineKind.Decl, "EndRegionDirectiveTrivia");
     // nl.Source = node.ToFullString();
     // nl.ParentKind = node.Parent.RawKind;
     // nl.RawKind = node.RawKind;
     // LogCommand(nl);
     base.VisitEndRegionDirectiveTrivia(node);
 }
 public static void ComputeRefactorings(RefactoringContext context, EndRegionDirectiveTriviaSyntax endRegionDirective)
 {
     if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveRegion) &&
         context.IsRootCompilationUnit)
     {
         context.RegisterRefactoring(
             "Remove region",
             cancellationToken => context.Document.RemoveRegionAsync(endRegionDirective, cancellationToken));
     }
 }
Exemple #6
0
        public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
        {
            if (!PreVisit(node))
            {
                return;
            }

            base.VisitEndRegionDirectiveTrivia(node);

            PostVisit(node);
        }
Exemple #7
0
        public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
        {
            if (regionStarts.Count == 0)
            {
                cx.ExtractionError("Couldn't find start region", null,
                                   cx.CreateLocation(node.GetLocation()), null, Util.Logging.Severity.Warning);
                return;
            }

            var start = regionStarts.Pop();

            new Entities.EndRegionDirective(cx, node, start);
        }
        public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
        {
            if (_stack.Count == 0)
            {
                return;
            }

            var region = _stack.Pop();

            region.EndIndex = node.FullSpan.End;

            Regions[region.Name] = region;

            base.VisitEndRegionDirectiveTrivia(node);
        }
Exemple #9
0
        public override SyntaxNode VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
        {
            var region = _stack.Pop();

            if (region.IsDeclaration)
            {
                return(null);
            }
            region = _stack.Peek();
            if (region.Annotation != null)
            {
                return(node.WithAdditionalAnnotations(region.Annotation));
            }
            return(node);
        }
        public static void ComputeRefactorings(RefactoringContext context, EndRegionDirectiveTriviaSyntax endRegionDirective)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveRegion) &&
                context.IsRootCompilationUnit)
            {
                RegionInfo region = SyntaxInfo.RegionInfo(endRegionDirective);

                if (region.Success)
                {
                    context.RegisterRefactoring(
                        "Remove region",
                        ct => context.Document.RemoveRegionAsync(region, ct),
                        RefactoringDescriptors.RemoveRegion);
                }
            }
        }
Exemple #11
0
        internal static RegionInfo Create(EndRegionDirectiveTriviaSyntax endRegionDirective)
        {
            if (endRegionDirective == null)
            {
                return(Default);
            }

            List <DirectiveTriviaSyntax> list = endRegionDirective.GetRelatedDirectives();

            if (list.Count != 2)
            {
                return(Default);
            }

            if (list[0].Kind() != SyntaxKind.RegionDirectiveTrivia)
            {
                return(Default);
            }

            return(new RegionInfo((RegionDirectiveTriviaSyntax)list[0], endRegionDirective));
        }
Exemple #12
0
        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());
                            }
                        }
                    }
                }
            }
        }
        public static Task <Document> RefactorAsync(
            Document document,
            EndRegionDirectiveTriviaSyntax endRegionDirective,
            SyntaxTrivia trivia,
            CancellationToken cancellationToken)
        {
            SyntaxToken endRegionKeyword = endRegionDirective.EndRegionKeyword;

            EndRegionDirectiveTriviaSyntax newNode = endRegionDirective;

            if (trivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
            {
                SyntaxTriviaList trailingTrivia = endRegionKeyword.TrailingTrivia;

                if (trailingTrivia.Any())
                {
                    if (endRegionDirective.HasPreprocessingMessageTrivia())
                    {
                        newNode = newNode.WithEndOfDirectiveToken(newNode.EndOfDirectiveToken.WithoutLeadingTrivia());
                    }

                    newNode = newNode.WithEndRegionKeyword(endRegionKeyword.WithTrailingTrivia(SyntaxFactory.Space, trivia));
                }
                else
                {
                    newNode = endRegionDirective.Update(
                        endRegionDirective.HashToken,
                        endRegionKeyword.WithTrailingTrivia(SyntaxFactory.Space),
                        endRegionDirective.EndOfDirectiveToken.WithLeadingTrivia(trivia),
                        endRegionDirective.IsActive);
                }
            }
            else
            {
                newNode = endRegionDirective.WithEndOfDirectiveToken(endRegionDirective.EndOfDirectiveToken.WithoutLeadingTrivia());
            }

            return(document.ReplaceNodeAsync(endRegionDirective, newNode, cancellationToken));
        }
Exemple #14
0
            public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax endRegionDirective)
            {
                if (regionsStack.Count == 0 || cancellationToken.IsCancellationRequested)
                {
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        base.VisitEndRegionDirectiveTrivia(endRegionDirective);
                    }

                    return;
                }

                RegionDirectiveTriviaSyntax regionDirective = regionsStack.Pop();
                string regionNameUpperCase = GetRegionName(regionDirective).ToUpperInvariant();

                if (regionNameUpperCase.Contains(identifierToRemove))
                {
                    RegionNodesToRemove.Add(endRegionDirective);
                }

                base.VisitEndRegionDirectiveTrivia(endRegionDirective);
            }
        public static void AnalyzeRegionDirective(SyntaxNodeAnalysisContext context)
        {
            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;
                        if (trivia.TryGetContainingList(out list))
                        {
                            EndRegionDirectiveTriviaSyntax endRegion2 = FindEndRegion(list, list.IndexOf(trivia));

                            if (endRegion == endRegion2)
                            {
                                context.ReportDiagnostic(
                                    DiagnosticDescriptors.RemoveEmptyRegion,
                                    region.GetLocation(),
                                    additionalLocations: ImmutableArray.Create(endRegion.GetLocation()));

                                context.ReportDiagnostic(DiagnosticDescriptors.RemoveEmptyRegionFadeOut, region.GetLocation());
                                context.ReportDiagnostic(DiagnosticDescriptors.RemoveEmptyRegionFadeOut, endRegion.GetLocation());
                            }
                        }
                    }
                }
            }
        }
Exemple #16
0
 private RegionInfo(RegionDirectiveTriviaSyntax directive, EndRegionDirectiveTriviaSyntax endDirective)
 {
     Directive    = directive;
     EndDirective = endDirective;
 }
        private static async Task <Document> RemoveRegionAsync(Document document, RegionDirectiveTriviaSyntax regionDirective, EndRegionDirectiveTriviaSyntax endRegionDirective, CancellationToken cancellationToken)
        {
            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, "");

            SourceText newSourceText = sourceText.WithChanges(textChange);

            return(document.WithText(newSourceText));
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitEndRegionDirectiveTrivia(node);
 }
Exemple #19
0
        public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
        {
            base.VisitEndRegionDirectiveTrivia(node);

            Regions.Add(node);
        }
Exemple #20
0
 public override SyntaxNode VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 => null;
Exemple #21
0
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     InsertLLOCMap(node.GetLocation());
     base.VisitEndRegionDirectiveTrivia(node);
 }
Exemple #22
0
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     throw new NotImplementedException();
 }
Exemple #23
0
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     AddRegionData(node);
     base.VisitEndRegionDirectiveTrivia(node);
 }
Exemple #24
0
 /// <summary>
 /// Creates a new <see cref="Syntax.RegionInfo"/> from the specified endregion directive.
 /// </summary>
 /// <param name="endRegionDirective"></param>
 /// <returns></returns>
 public static RegionInfo RegionInfo(EndRegionDirectiveTriviaSyntax endRegionDirective)
 {
     return(Syntax.RegionInfo.Create(endRegionDirective));
 }
Exemple #25
0
 public override SyntaxNode VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 => SyntaxFactory.SkippedTokensTrivia();
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitEndRegionDirectiveTrivia(node);
 }
Exemple #27
0
 public override Evaluation VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     return(base.VisitEndRegionDirectiveTrivia(node));
 }
Exemple #28
0
 public TameEndRegionDirectiveTriviaSyntax(EndRegionDirectiveTriviaSyntax node)
 {
     Node = node;
     AddChildren();
 }
 public override SyntaxNode VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     return SyntaxFactory.SkippedTokensTrivia();
 }
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     Debug.Fail(node.ToString());
     base.VisitEndRegionDirectiveTrivia(node);
 }
Exemple #31
0
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
 }
Exemple #32
0
 private void ClassifyEndRegionDirective(EndRegionDirectiveTriviaSyntax node)
 {
     AddClassification(node.HashToken, ClassificationTypeNames.PreprocessorKeyword);
     AddClassification(node.EndRegionKeyword, ClassificationTypeNames.PreprocessorKeyword);
     ClassifyDirectiveTrivia(node);
 }
Exemple #33
0
 public override Ust VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     throw new InvalidOperationException();
 }
Exemple #34
0
 private void ClassifyEndRegionDirective(EndRegionDirectiveTriviaSyntax node)
 {
     AddClassification(node.HashToken, ClassificationTypeNames.PreprocessorKeyword);
     AddClassification(node.EndRegionKeyword, ClassificationTypeNames.PreprocessorKeyword);
     ClassifyDirectiveTrivia(node);
 }
 public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
 {
     base.VisitEndRegionDirectiveTrivia(node);
     this.EndRegionCount++;
 }