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; } } }
private void ExecuteInput(SearchContext context) { string input = Options.Input; int count = 0; var maxReason = MaxReason.None; Match match = Options.ContentFilter.Match(input, context.CancellationToken); if (match.Success) { ContentWriter contentWriter = null; List <Capture> groups = null; try { groups = ListCache <Capture> .GetInstance(); maxReason = GetCaptures(match, FileWriterOptions.GroupNumber, context, isPathWritten: false, predicate: Options.ContentFilter.Predicate, captures: groups); if (ShouldLog(Verbosity.Normal)) { MatchOutputInfo outputInfo = Options.CreateOutputInfo(input, match); contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, input, Options.ReplaceOptions, FileWriterOptions, outputInfo: outputInfo); } else { contentWriter = new EmptyContentWriter(null, FileWriterOptions); } WriteMatches(contentWriter, groups, context); count = contentWriter.MatchCount; } finally { contentWriter?.Dispose(); if (groups != null) { ListCache <Capture> .Free(groups); } } } if (ShouldLog(Verbosity.Detailed) || Options.IncludeSummary) { Verbosity verbosity = (Options.IncludeSummary) ? Verbosity.Minimal : Verbosity.Detailed; WriteLine(verbosity); WriteCount("Replacements", count, Colors.Message_OK, verbosity); WriteIf(maxReason == MaxReason.CountExceedsMax, "+", Colors.Message_OK, verbosity); WriteLine(verbosity); } }
private void ReplaceMatches( string filePath, Encoding encoding, string input, Match match, string indent, ContentWriterOptions writerOptions, SearchContext context) { SearchTelemetry telemetry = context.Telemetry; ContentWriter contentWriter = null; TextWriter textWriter = null; List <Capture> groups = null; try { groups = ListCache <Capture> .GetInstance(); GetCaptures(match, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: Options.ContentFilter.Predicate, captures: groups); int fileMatchCount = 0; int fileReplacementCount = 0; if (!Options.DryRun) { if (Options.AskMode == AskMode.File) { textWriter = new StringWriter(); } else if (Options.AskMode == AskMode.None) { textWriter = new StreamWriter(filePath, false, encoding); } } if (Options.AskMode == AskMode.Value || ShouldLog(Verbosity.Normal)) { MatchOutputInfo outputInfo = Options.CreateOutputInfo(input, match); if (Options.AskMode == AskMode.Value) { Lazy <TextWriter> lazyWriter = (Options.DryRun) ? null : new Lazy <TextWriter>(() => new StreamWriter(filePath, false, encoding)); contentWriter = AskReplacementWriter.Create(Options.ContentDisplayStyle, input, Options.ReplaceOptions, lazyWriter, writerOptions, outputInfo); } else { contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, input, Options.ReplaceOptions, writerOptions, textWriter, outputInfo); } } else if (Options.DryRun) { contentWriter = new EmptyContentWriter(null, FileWriterOptions); } else { contentWriter = new TextWriterContentWriter(input, Options.ReplaceOptions, textWriter, writerOptions); } WriteMatches(contentWriter, groups, context); fileMatchCount = contentWriter.MatchCount; fileReplacementCount = (contentWriter is AskReplacementWriter askReplacementWriter) ? askReplacementWriter.ReplacementCount : fileMatchCount; telemetry.MatchCount += fileMatchCount; if (contentWriter.MatchingLineCount >= 0) { if (telemetry.MatchingLineCount == -1) { telemetry.MatchingLineCount = 0; } telemetry.MatchingLineCount += contentWriter.MatchingLineCount; } if (Options.AskMode == AskMode.File) { if (context.TerminationReason == TerminationReason.Canceled) { fileReplacementCount = 0; } else { try { if (Options.DryRun) { if (ConsoleHelpers.Question("Continue without asking?", indent)) { Options.AskMode = AskMode.None; } } else if (ConsoleHelpers.Question("Replace content?", indent)) { File.WriteAllText(filePath, textWriter !.ToString(), encoding); } else { fileReplacementCount = 0; } } catch (OperationCanceledException) { context.TerminationReason = TerminationReason.Canceled; fileReplacementCount = 0; } } } else if (Options.AskMode == AskMode.Value) { if (((AskReplacementWriter)contentWriter).ContinueWithoutAsking) { Options.AskMode = AskMode.None; } } 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(); if (groups != null) { ListCache <Capture> .Free(groups); } } }
protected override void ExecuteMatchWithContentCore( 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); } SearchTelemetry telemetry = context.Telemetry; ContentWriter? contentWriter = null; TextWriter? textWriter = null; List <Capture>?groups = null; try { groups = ListCache <Capture> .GetInstance(); GetCaptures(fileMatch.ContentMatch !, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: Options.ContentFilter !.Predicate, captures: groups); int fileMatchCount = 0; int fileReplacementCount = 0; if (!Options.DryRun) { if (Options.AskMode == AskMode.File) { textWriter = new StringWriter(); } else if (Options.AskMode == AskMode.None) { textWriter = new StreamWriter(fileMatch.Path, false, fileMatch.Encoding); } } if (Options.AskMode == AskMode.Value || ShouldLog(Verbosity.Normal)) { MatchOutputInfo?outputInfo = Options.CreateOutputInfo(fileMatch.ContentText, fileMatch.ContentMatch !, ContentFilter !); if (Options.AskMode == AskMode.Value) { Lazy <TextWriter>?lazyWriter = (Options.DryRun) ? null : new Lazy <TextWriter>(() => new StreamWriter(fileMatch.Path, false, fileMatch.Encoding)); contentWriter = AskReplacementWriter.Create(Options.ContentDisplayStyle, fileMatch.ContentText, Options.ReplaceOptions, lazyWriter, writerOptions, outputInfo); } else { contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, fileMatch.ContentText, Options.ReplaceOptions, writerOptions, textWriter, outputInfo); } } else if (Options.DryRun) { contentWriter = new EmptyContentWriter(FileWriterOptions); } else { contentWriter = new TextWriterContentWriter(fileMatch.ContentText, Options.ReplaceOptions, textWriter !, writerOptions); } WriteMatches(contentWriter, groups, context); fileMatchCount = contentWriter.MatchCount; fileReplacementCount = (contentWriter is AskReplacementWriter askReplacementWriter) ? askReplacementWriter.ReplacementCount : fileMatchCount; telemetry.MatchCount += fileMatchCount; if (contentWriter.MatchingLineCount >= 0) { if (telemetry.MatchingLineCount == -1) { telemetry.MatchingLineCount = 0; } telemetry.MatchingLineCount += contentWriter.MatchingLineCount; } if (Options.AskMode == AskMode.File) { if (context.TerminationReason == TerminationReason.Canceled) { fileReplacementCount = 0; } else { try { if (Options.DryRun) { if (ConsoleHelpers.AskToContinue(indent)) { Options.AskMode = AskMode.None; } } else if (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 (Options.AskMode == AskMode.Value) { if (((AskReplacementWriter)contentWriter).ContinueWithoutAsking) { Options.AskMode = AskMode.None; } } 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(); if (groups != null) { ListCache <Capture> .Free(groups); } } }
private void ExecuteInput(SearchContext context, string input) { int count = 0; var maxReason = MaxReason.None; Filter contentFilter = ContentFilter !; Match? match = contentFilter.Match(input); if (match != null) { ContentWriter? contentWriter = null; List <Capture>?groups = null; try { groups = ListCache <Capture> .GetInstance(); maxReason = GetCaptures( match, FileWriterOptions.GroupNumber, context, isPathDisplayed: false, predicate: contentFilter.Predicate, captures: groups); IEnumerable <ICapture> captures = GetCaptures(groups, context.CancellationToken) ?? groups.Select(f => (ICapture) new RegexCapture(f)); using (IEnumerator <ICapture> en = captures.GetEnumerator()) { if (en.MoveNext()) { if (ShouldLog(Verbosity.Normal)) { MatchOutputInfo?outputInfo = Options.CreateOutputInfo(input, match, contentFilter); contentWriter = CreateReplacementWriter( Options.ContentDisplayStyle, input, Options.Replacer, FileWriterOptions, outputInfo: outputInfo); } else { contentWriter = new EmptyContentWriter(FileWriterOptions); } WriteMatches(contentWriter, en, context); count = contentWriter.MatchCount; } } } finally { contentWriter?.Dispose(); if (groups != null) { ListCache <Capture> .Free(groups); } } } if (ShouldLog(Verbosity.Detailed) || Options.IncludeSummary) { Verbosity verbosity = (Options.IncludeSummary) ? Verbosity.Minimal : Verbosity.Detailed; WriteLine(verbosity); WriteCount("Replacements", count, Colors.Message_OK, verbosity); WriteIf(maxReason == MaxReason.CountExceedsMax, "+", Colors.Message_OK, verbosity); WriteLine(verbosity); } }
private void WriteMatches( string input, Match match, ContentWriterOptions writerOptions, SearchContext context) { SearchTelemetry telemetry = context.Telemetry; ContentWriter contentWriter = null; List <Capture> captures = null; try { captures = ListCache <Capture> .GetInstance(); GetCaptures(match, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: ContentFilter.Predicate, captures: captures); bool hasAnyFunction = Options.ModifyOptions.HasAnyFunction; if (hasAnyFunction || Options.AskMode == AskMode.Value || ShouldLog(Verbosity.Normal)) { if (hasAnyFunction) { if (_fileValues == null) { _fileValues = new List <string>(); } else { _fileValues.Clear(); } if (_fileStorage == null) { _fileStorage = new ListResultStorage(_fileValues); } } contentWriter = ContentWriter.CreateFind( contentDisplayStyle: Options.ContentDisplayStyle, input: input, options: writerOptions, storage: (hasAnyFunction) ? _fileStorage : _storage, outputInfo: Options.CreateOutputInfo(input, match), writer: (hasAnyFunction) ? null : ContentTextWriter.Default, ask: _askMode == AskMode.Value); } else { contentWriter = new EmptyContentWriter(null, writerOptions); } WriteMatches(contentWriter, captures, context); if (hasAnyFunction) { ConsoleColors colors = (Options.HighlightMatch) ? Colors.Match : default; ConsoleColors boundaryColors = (Options.HighlightBoundary) ? Colors.MatchBoundary : default; var valueWriter = new ValueWriter(ContentTextWriter.Default, writerOptions.Indent, includeEndingIndent: false); foreach (string value in _fileValues.Modify(Options.ModifyOptions)) { Write(writerOptions.Indent, Verbosity.Normal); valueWriter.Write(value, Symbols, colors, boundaryColors); WriteLine(Verbosity.Normal); _storage?.Add(value); telemetry.MatchCount++; } _storageIndexes?.Add(_storage !.Count); } else { telemetry.MatchCount += contentWriter.MatchCount; if (contentWriter.MatchingLineCount >= 0) { if (telemetry.MatchingLineCount == -1) { telemetry.MatchingLineCount = 0; } telemetry.MatchingLineCount += contentWriter.MatchingLineCount; } } if (_askMode == AskMode.Value) { if (contentWriter is AskValueContentWriter askValueContentWriter) { if (!askValueContentWriter.Ask) { _askMode = AskMode.None; } } else if (contentWriter is AskLineContentWriter askLineContentWriter) { if (!askLineContentWriter.Ask) { _askMode = AskMode.None; } } } } finally { contentWriter?.Dispose(); if (captures != null) { ListCache <Capture> .Free(captures); } } }