Example #1
0
        public static void WriteFilePathEnd(int count, MaxReason maxReason, bool includeCount)
        {
            Verbosity verbosity = ConsoleOut.Verbosity;

            if (verbosity >= Verbosity.Detailed ||
                includeCount)
            {
                verbosity = (includeCount) ? Verbosity.Minimal : Verbosity.Detailed;

                ConsoleOut.Write("  ", Colors.Message_OK, verbosity);
                ConsoleOut.Write(count.ToString("n0"), Colors.Message_OK, verbosity);
                ConsoleOut.WriteIf(maxReason == MaxReason.CountExceedsMax, "+", Colors.Message_OK, verbosity);
            }

            ConsoleOut.WriteLine(Verbosity.Minimal);

            if (Out != null)
            {
                verbosity = Out.Verbosity;

                if (verbosity >= Verbosity.Detailed ||
                    includeCount)
                {
                    verbosity = (includeCount) ? Verbosity.Minimal : Verbosity.Detailed;

                    Out.Write("  ", verbosity);
                    Out.Write(count.ToString("n0"), verbosity);
                    Out.WriteIf(maxReason == MaxReason.CountExceedsMax, "+", verbosity);
                }

                Out.WriteLine(Verbosity.Minimal);
            }
        }
Example #2
0
        protected override void ExecuteMatchWithContentCore(
            FileMatch fileMatch,
            SearchContext context,
            ContentWriterOptions writerOptions,
            string?baseDirectoryPath  = null,
            ColumnWidths?columnWidths = null)
        {
            List <Capture>?groups = null;

            try
            {
                groups = ListCache <Capture> .GetInstance();

                MaxReason maxReason = GetCaptures(
                    fileMatch.ContentMatch !,
                    writerOptions.GroupNumber,
                    context,
                    isPathDisplayed: false,
                    predicate: Options.ContentFilter !.Predicate,
                    captures: groups);

                List <ICapture>?captures = GetCaptures(groups, context.CancellationToken);

                using (IEnumerator <ICapture> en = (captures ?? groups.Select(f => (ICapture) new RegexCapture(f))).GetEnumerator())
                {
                    if (en.MoveNext())
                    {
                        if (SpellcheckState != null)
                        {
                            SpellcheckState.CurrentPath = fileMatch.Path;
                        }

                        ExecuteMatchWithContentCore(
                            en,
                            captures?.Count ?? groups.Count,
                            maxReason,
                            fileMatch,
                            context,
                            writerOptions,
                            baseDirectoryPath,
                            columnWidths);
                    }
                }
            }
            finally
            {
                if (groups != null)
                {
                    ListCache <Capture> .Free(groups);
                }

                if (SpellcheckState != null)
                {
                    SpellcheckState.CurrentPath = null;
                }
            }
        }
Example #3
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 #4
0
        private void ExecuteMatchWithContentCore(
            IEnumerator <ICapture> groups,
            int count,
            MaxReason maxReason,
            FileMatch fileMatch,
            SearchContext context,
            ContentWriterOptions writerOptions,
            string?baseDirectoryPath  = null,
            ColumnWidths?columnWidths = null)
        {
            string indent = GetPathIndent(baseDirectoryPath);

            if (!Options.OmitPath)
            {
                WritePath(context, fileMatch, baseDirectoryPath, indent, columnWidths, includeNewline: false);
                WriteFilePathEnd(count, maxReason, Options.IncludeCount);
            }

            SearchTelemetry telemetry = context.Telemetry;

            ContentWriter?contentWriter = null;
            TextWriter?   textWriter    = null;

            try
            {
                int fileMatchCount       = 0;
                int fileReplacementCount = 0;

                if (!Options.DryRun)
                {
                    if (Options.AskMode == AskMode.File)
                    {
                        textWriter = new StringWriter();
                    }
                    else if (Options.AskMode != AskMode.Value &&
                             !Options.Interactive)
                    {
                        textWriter = (CanUseStreamWriter)
                            ? new StreamWriter(fileMatch.Path, false, fileMatch.Encoding)
                            : new StringWriter();
                    }
                }

                if (Options.AskMode == AskMode.Value ||
                    Options.Interactive ||
                    (!Options.OmitContent &&
                     ShouldLog(Verbosity.Normal)))
                {
                    MatchOutputInfo?outputInfo = Options.CreateOutputInfo(
                        fileMatch.ContentText,
                        fileMatch.ContentMatch !,
                        ContentFilter !);

                    if (Options.AskMode == AskMode.Value ||
                        Options.Interactive)
                    {
                        Lazy <TextWriter>?lazyWriter = null;

                        if (!Options.DryRun)
                        {
                            lazyWriter = new Lazy <TextWriter>(() =>
                            {
                                textWriter = new StringWriter();
                                return(textWriter);
                            });
                        }

                        contentWriter = AskReplacementWriter.Create(
                            Options.ContentDisplayStyle,
                            fileMatch.ContentText,
                            Options.Replacer,
                            lazyWriter,
                            writerOptions,
                            outputInfo,
                            isInteractive: Options.Interactive,
                            SpellcheckState);
                    }
                    else
                    {
                        contentWriter = CreateReplacementWriter(
                            Options.ContentDisplayStyle,
                            fileMatch.ContentText,
                            Options.Replacer,
                            writerOptions,
                            textWriter,
                            outputInfo);
                    }
                }
                else if (Options.DryRun &&
                         CanUseEmptyWriter)
                {
                    contentWriter = new EmptyContentWriter(FileWriterOptions);
                }
                else
                {
                    contentWriter = new TextWriterContentWriter(
                        fileMatch.ContentText,
                        Options.Replacer,
                        writerOptions,
                        textWriter,
                        SpellcheckState);
                }

                WriteMatches(contentWriter, groups, context);

                fileMatchCount = contentWriter.MatchCount;

                fileReplacementCount = (contentWriter is IReportReplacement reportReplacement)
                    ? reportReplacement.ReplacementCount
                    : fileMatchCount;

                telemetry.MatchCount += fileMatchCount;

                if (contentWriter.MatchingLineCount >= 0)
                {
                    if (telemetry.MatchingLineCount == -1)
                    {
                        telemetry.MatchingLineCount = 0;
                    }

                    telemetry.MatchingLineCount += contentWriter.MatchingLineCount;
                }

                if (Options.AskMode == AskMode.Value ||
                    Options.Interactive)
                {
                    if (textWriter != null)
                    {
                        File.WriteAllText(fileMatch.Path, textWriter.ToString(), fileMatch.Encoding);

                        if (Options.AskMode == AskMode.Value &&
                            ((AskReplacementWriter)contentWriter).ContinueWithoutAsking)
                        {
                            Options.AskMode = AskMode.None;
                        }
                    }
                }
                else if (Options.AskMode == AskMode.File)
                {
                    if (context.TerminationReason == TerminationReason.Canceled)
                    {
                        fileReplacementCount = 0;
                    }
                    else
                    {
                        try
                        {
                            if (Options.DryRun)
                            {
                                if (ConsoleHelpers.AskToContinue(indent) == DialogResult.YesToAll)
                                {
                                    Options.AskMode = AskMode.None;
                                }
                            }
                            else if (fileReplacementCount > 0 &&
                                     ConsoleHelpers.AskToExecute("Replace content?", indent))
                            {
                                File.WriteAllText(fileMatch.Path, textWriter !.ToString(), fileMatch.Encoding);
                            }
                            else
                            {
                                fileReplacementCount = 0;
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            context.TerminationReason = TerminationReason.Canceled;
                            fileReplacementCount      = 0;
                        }
                    }
                }
                else if (fileReplacementCount > 0 &&
                         textWriter is StringWriter)
                {
                    File.WriteAllText(fileMatch.Path, textWriter.ToString(), fileMatch.Encoding);
                }

                telemetry.ProcessedMatchCount += fileReplacementCount;

                if (fileReplacementCount > 0)
                {
                    telemetry.ProcessedFileCount++;
                }
            }
            catch (Exception ex) when(ex is IOException ||
                                      ex is UnauthorizedAccessException)
            {
                WriteFileError(ex, indent: indent);
            }
            finally
            {
                contentWriter?.Dispose();
            }
        }
Example #5
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;