Ejemplo n.º 1
0
        public async Task <EmbeddedBraceMatchingResult?> FindBracesAsync(
            Document document, int position, CancellationToken cancellationToken)
        {
            var option = document.Project.Solution.Workspace.Options.GetOption(
                RegularExpressionsOptions.HighlightRelatedRegexComponentsUnderCursor, document.Project.Language);

            if (!option)
            {
                return(null);
            }

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var token       = root.FindToken(position);
            var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

            if (RegexPatternDetector.IsDefinitelyNotPattern(token, syntaxFacts))
            {
                return(null);
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var detector = RegexPatternDetector.TryGetOrCreate(semanticModel, _language);
            var tree     = detector?.TryParseRegexPattern(token, cancellationToken);

            if (tree == null)
            {
                return(null);
            }

            return(GetMatchingBraces(tree, position));
        }
Ejemplo n.º 2
0
        public override void AddClassifications(
            Workspace workspace,
            SyntaxToken token,
            SemanticModel semanticModel,
            ArrayBuilder <ClassifiedSpan> result,
            CancellationToken cancellationToken
            )
        {
            if (_info.StringLiteralTokenKind != token.RawKind)
            {
                return;
            }

            if (
                !workspace.Options.GetOption(
                    RegularExpressionsOptions.ColorizeRegexPatterns,
                    semanticModel.Language
                    )
                )
            {
                return;
            }

            // Do some quick syntactic checks before doing any complex work.
            if (!RegexPatternDetector.IsPossiblyPatternToken(token, _info.SyntaxFacts))
            {
                return;
            }

            var detector = RegexPatternDetector.TryGetOrCreate(semanticModel.Compilation, _info);
            var tree     = detector?.TryParseRegexPattern(token, semanticModel, cancellationToken);

            if (tree == null)
            {
                return;
            }

            var visitor = s_visitorPool.Allocate();

            try
            {
                visitor.Result = result;
                AddClassifications(tree.Root, visitor, result);
            }
            finally
            {
                visitor.Result = null;
                s_visitorPool.Free(visitor);
            }
        }
Ejemplo n.º 3
0
        public void Analyze(SemanticModelAnalysisContext context, OptionSet optionSet)
        {
            var semanticModel     = context.SemanticModel;
            var syntaxTree        = semanticModel.SyntaxTree;
            var cancellationToken = context.CancellationToken;

            var option = optionSet.GetOption(RegularExpressionsOptions.ReportInvalidRegexPatterns, syntaxTree.Options.Language);

            if (!option)
            {
                return;
            }

            var detector = RegexPatternDetector.TryGetOrCreate(semanticModel, _language);

            if (detector == null)
            {
                return;
            }

            // Use an actual stack object so that we don't blow the actual stack through recursion.
            var root  = syntaxTree.GetRoot(cancellationToken);
            var stack = new Stack <SyntaxNode>();

            stack.Push(root);

            while (stack.Count != 0)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var current = stack.Pop();

                foreach (var child in current.ChildNodesAndTokens())
                {
                    if (child.IsNode)
                    {
                        stack.Push(child.AsNode());
                    }
                    else
                    {
                        AnalyzeToken(context, detector, child.AsToken(), cancellationToken);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void AddClassifications(
            Workspace workspace, SyntaxToken token, SemanticModel semanticModel,
            ArrayBuilder <ClassifiedSpan> result, CancellationToken cancellationToken)
        {
            if (!workspace.Options.GetOption(RegularExpressionsOptions.ColorizeRegexPatterns, semanticModel.Language))
            {
                return;
            }

            // Do some quick syntactic checks before doing any complex work.
            if (RegexPatternDetector.IsDefinitelyNotPattern(token, _language.SyntaxFacts))
            {
                return;
            }

            var detector = RegexPatternDetector.TryGetOrCreate(semanticModel, _language);
            var tree     = detector?.TryParseRegexPattern(token, cancellationToken);

            if (tree == null)
            {
                return;
            }

            var visitor = _visitorPool.Allocate();

            try
            {
                visitor.Result = result;
                AddClassifications(tree.Root, visitor, result);
            }
            finally
            {
                visitor.Result = null;
                _visitorPool.Free(visitor);
            }
        }