protected override void WriteEndReplacement(Match match, string result) { if (IsInteractive && result.IndexOfAny(_newLineChars) == -1) { string?newResult = ConsoleHelpers.ReadUserInput("Replacement: ", result, Options.Indent); if (newResult == null) { return; } result = newResult; } if (_lazyWriter != null) { if (IsInteractive || ConsoleHelpers.AskToExecute("Replace?", Options.Indent)) { _lazyWriter.Value.Write(Input.AsSpan(_writerIndex, match.Index - _writerIndex)); _lazyWriter.Value.Write(result); _writerIndex = match.Index + match.Length; ReplacementCount++; } } else if (!ContinueWithoutAsking && !IsInteractive && ConsoleHelpers.AskToContinue(Options.Indent) == DialogResult.YesToAll) { ContinueWithoutAsking = true; } }
protected override void WriteEndReplacement(ICapture capture, string?result) { bool isUserInput = IsInteractive && (capture is RegexCapture || result == null) && (result == null || result.IndexOfAny(_newLineChars) == -1); string replacement = result ?? capture.Value; if (isUserInput) { replacement = ConsoleHelpers.ReadUserInput(replacement, Options.Indent + "Replacement: "); } if (!string.Equals(capture.Value, replacement, StringComparison.Ordinal)) { if (_lazyWriter != null) { if (IsInteractive || ConsoleHelpers.AskToExecute("Replace?", Options.Indent)) { _lazyWriter.Value.Write(Input.AsSpan(_writerIndex, capture.Index - _writerIndex)); _lazyWriter.Value.Write(replacement); _writerIndex = capture.Index + capture.Length; ReplacementCount++; } } else if (!ContinueWithoutAsking && !IsInteractive && ConsoleHelpers.AskToContinue(Options.Indent) == DialogResult.YesToAll) { ContinueWithoutAsking = true; } } SpellcheckState?.ProcessReplacement( Input, capture, replacement, lineNumber: (ValueWriter as LineNumberValueWriter)?.LineNumber, isUserInput: true); }
protected override void WriteEndReplacement(Match match, string result) { if (_lazyWriter != null) { if (ConsoleHelpers.AskToExecute("Replace?", Options.Indent)) { _lazyWriter.Value.Write(Input.AsSpan(_writerIndex, match.Index - _writerIndex)); _lazyWriter.Value.Write(result); _writerIndex = match.Index + match.Length; ReplacementCount++; } } else if (!ContinueWithoutAsking && ConsoleHelpers.AskToContinue(Options.Indent)) { ContinueWithoutAsking = true; } }
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 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(); } }