Example #1
0
        protected MaxReason GetCaptures(
            Match match,
            int groupNumber,
            Func <string, bool>?predicate,
            List <Capture> captures)
        {
            int maxMatchesInFile = MaxMatchesInFile;
            int maxTotalMatches  = MaxTotalMatches;

            int count = 0;

            if (maxMatchesInFile > 0)
            {
                if (maxTotalMatches > 0)
                {
                    maxTotalMatches -= Telemetry.MatchCount;
                    count            = Math.Min(maxMatchesInFile, maxTotalMatches);
                }
                else
                {
                    count = maxMatchesInFile;
                }
            }
            else if (maxTotalMatches > 0)
            {
                maxTotalMatches -= Telemetry.MatchCount;
                count            = maxTotalMatches;
            }

            Debug.Assert(count >= 0, count.ToString());

            MaxReason maxReason = CaptureFactory.GetCaptures(
                ref captures,
                match,
                groupNumber,
                count,
                predicate,
                CancellationToken);

            if ((maxReason == MaxReason.CountEqualsMax || maxReason == MaxReason.CountExceedsMax) &&
                maxTotalMatches > 0 &&
                (maxMatchesInFile == 0 || maxTotalMatches <= maxMatchesInFile))
            {
                TerminationReason = FileSystem.TerminationReason.MaxReached;
            }

            return(maxReason);
        }
Example #2
0
        private List <TextSpan> GetFilteredSpans(List <Capture> groups, CancellationToken cancellationToken)
        {
            ImmutableArray <TextSpan> .Builder?allSpans = null;

            foreach (Capture group in groups)
            {
                foreach (Filter filter in SpellcheckState.Filters)
                {
                    Match?match = filter.Match(group.Value);

                    if (match != null)
                    {
                        var captures = new List <Capture>();

                        MaxReason _ = CaptureFactory.GetCaptures(
                            ref captures,
                            match,
                            filter.GroupNumber,
                            count: 0,
                            filter.Predicate,
                            cancellationToken);

                        if (allSpans == null)
                        {
                            allSpans = ImmutableArray.CreateBuilder <TextSpan>();
                        }

                        foreach (Capture capture in captures)
                        {
                            allSpans.Add(new TextSpan(capture.Index, capture.Length));
                        }
                    }
                }
            }

            if (allSpans == null)
            {
                return(new List <TextSpan>());
            }

            allSpans.Sort((x, y) =>
            {
                int diff = x.Start.CompareTo(y.Start);

                return((diff != 0) ? diff : -x.Length.CompareTo(y.Length));
            });

            TextSpan span = allSpans[0];

            var spans = new List <TextSpan>()
            {
                span
            };

            for (int i = 1; i < allSpans.Count; i++)
            {
                TextSpan span2 = allSpans[i];

                if (span.Start == span2.Start)
                {
                    continue;
                }

                if (span2.Start <= span.End + 1)
                {
                    span       = TextSpan.FromBounds(span.Start, span2.End);
                    spans[^ 1] = span;