Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

file clean-up + catching OperationCanceledExceptions

  • Loading branch information...
commit 6a0d2bcf91f9bf24a9bd95ebfacf877cf6f1e47c 1 parent a72eb6c
@siegfriedpammer siegfriedpammer authored
View
140 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs
@@ -27,54 +27,7 @@ namespace SearchAndReplace
/// </summary>
public class SearchManager
{
- static IEnumerable<FileName> GenerateFileList(SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false)
- {
- List<FileName> files = new List<FileName>();
-
- switch (target) {
- case SearchTarget.CurrentDocument:
- case SearchTarget.CurrentSelection:
- ITextEditorProvider vc = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
- if (vc != null)
- files.Add(vc.TextEditor.FileName);
- break;
- case SearchTarget.AllOpenFiles:
- foreach (ITextEditorProvider editor in WorkbenchSingleton.Workbench.ViewContentCollection.OfType<ITextEditorProvider>())
- files.Add(editor.TextEditor.FileName);
- break;
- case SearchTarget.WholeProject:
- if (ProjectService.CurrentProject == null)
- break;
- foreach (FileProjectItem item in ProjectService.CurrentProject.Items.OfType<FileProjectItem>())
- files.Add(new FileName(item.FileName));
- break;
- case SearchTarget.WholeSolution:
- if (ProjectService.OpenSolution == null)
- break;
- foreach (var item in ProjectService.OpenSolution.SolutionFolderContainers.Select(f => f.SolutionItems).SelectMany(si => si.Items))
- files.Add(new FileName(Path.Combine(ProjectService.OpenSolution.Directory, item.Location)));
- foreach (var item in ProjectService.OpenSolution.Projects.SelectMany(p => p.Items).OfType<FileProjectItem>())
- files.Add(new FileName(item.FileName));
- break;
- case SearchTarget.Directory:
- if (!Directory.Exists(baseDirectory))
- break;
- return FileUtility.LazySearchDirectory(baseDirectory, filter, searchSubdirs);
- default:
- throw new Exception("Invalid value for FileListType");
- }
-
- return files.Distinct();
- }
-
- public static void ShowSearchResults(string pattern, IObservable<SearchedFile> results)
- {
- string title = StringParser.Parse("${res:MainWindow.Windows.SearchResultPanel.OccurrencesOf}",
- new StringTagPair("Pattern", pattern));
- SearchResultsPad.Instance.ShowSearchResults(title, results);
- SearchResultsPad.Instance.BringToFront();
- }
-
+ #region FindAll
public static IObservable<SearchedFile> FindAllParallel(IProgressMonitor progressMonitor, string pattern, bool ignoreCase, bool matchWholeWords, SearchMode mode,
SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false, ISegment selection = null)
{
@@ -237,16 +190,7 @@ SearchedFile SearchFile(FileName fileName)
return null;
}
}
-
- public static ITextEditor GetActiveTextEditor()
- {
- ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
- if (provider != null) {
- return provider.TextEditor;
- } else {
- return null;
- }
- }
+ #endregion
#region FindNext
static SearchRegion currentSearchRegion;
@@ -410,6 +354,7 @@ public bool IsSameState(FileName[] files, string pattern, bool ignoreCase, bool
}
#endregion
+ #region Mark & Replace(All)
public static void MarkAll(IObservable<SearchedFile> results)
{
int count = 0;
@@ -445,6 +390,15 @@ public static int ReplaceAll(IEnumerable<SearchedFile> results, string replaceme
return count;
}
+
+ public static void Replace(SearchResultMatch lastMatch, string replacement)
+ {
+ if (lastMatch == null)
+ return;
+ ITextEditor textArea = OpenTextArea(lastMatch.FileName);
+ if (textArea != null)
+ textArea.Document.Replace(lastMatch.StartOffset, lastMatch.Length, lastMatch.TransformReplacePattern(replacement));
+ }
static void MarkResult(SearchResultMatch result, bool switchToOpenedView = true)
{
@@ -462,7 +416,9 @@ static void MarkResult(SearchResultMatch result, bool switchToOpenedView = true)
BookmarkManager.AddMark(new Bookmark(result.FileName, result.StartLocation));
}
}
-
+ #endregion
+
+ #region TextEditor helpers
static ITextEditor OpenTextArea(string fileName, bool switchToOpenedView = true)
{
ITextEditorProvider textEditorProvider;
@@ -478,6 +434,18 @@ static ITextEditor OpenTextArea(string fileName, bool switchToOpenedView = true)
return null;
}
+ public static ITextEditor GetActiveTextEditor()
+ {
+ ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
+ if (provider != null) {
+ return provider.TextEditor;
+ } else {
+ return null;
+ }
+ }
+ #endregion
+
+ #region Show Messages
public static void ShowReplaceDoneMessage(int count)
{
if (count == 0) {
@@ -506,7 +474,9 @@ static void ShowNotFoundMessage()
{
MessageService.ShowMessage("${res:Dialog.NewProject.SearchReplace.SearchStringNotFound}", "${res:Dialog.NewProject.SearchReplace.SearchStringNotFound.Title}");
}
+ #endregion
+ #region Result display helpers
public static void SelectResult(SearchResultMatch result)
{
if (result == null) {
@@ -534,13 +504,53 @@ public static bool IsResultSelected(SearchResultMatch result)
&& result.Length == editor.SelectionLength;
}
- public static void Replace(SearchResultMatch lastMatch, string replacement)
+ public static void ShowSearchResults(string pattern, IObservable<SearchedFile> results)
{
- if (lastMatch == null)
- return;
- ITextEditor textArea = OpenTextArea(lastMatch.FileName);
- if (textArea != null)
- textArea.Document.Replace(lastMatch.StartOffset, lastMatch.Length, lastMatch.TransformReplacePattern(replacement));
+ string title = StringParser.Parse("${res:MainWindow.Windows.SearchResultPanel.OccurrencesOf}",
+ new StringTagPair("Pattern", pattern));
+ SearchResultsPad.Instance.ShowSearchResults(title, results);
+ SearchResultsPad.Instance.BringToFront();
+ }
+ #endregion
+
+ static IEnumerable<FileName> GenerateFileList(SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false)
+ {
+ List<FileName> files = new List<FileName>();
+
+ switch (target) {
+ case SearchTarget.CurrentDocument:
+ case SearchTarget.CurrentSelection:
+ ITextEditorProvider vc = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
+ if (vc != null)
+ files.Add(vc.TextEditor.FileName);
+ break;
+ case SearchTarget.AllOpenFiles:
+ foreach (ITextEditorProvider editor in WorkbenchSingleton.Workbench.ViewContentCollection.OfType<ITextEditorProvider>())
+ files.Add(editor.TextEditor.FileName);
+ break;
+ case SearchTarget.WholeProject:
+ if (ProjectService.CurrentProject == null)
+ break;
+ foreach (FileProjectItem item in ProjectService.CurrentProject.Items.OfType<FileProjectItem>())
+ files.Add(new FileName(item.FileName));
+ break;
+ case SearchTarget.WholeSolution:
+ if (ProjectService.OpenSolution == null)
+ break;
+ foreach (var item in ProjectService.OpenSolution.SolutionFolderContainers.Select(f => f.SolutionItems).SelectMany(si => si.Items))
+ files.Add(new FileName(Path.Combine(ProjectService.OpenSolution.Directory, item.Location)));
+ foreach (var item in ProjectService.OpenSolution.Projects.SelectMany(p => p.Items).OfType<FileProjectItem>())
+ files.Add(new FileName(item.FileName));
+ break;
+ case SearchTarget.Directory:
+ if (!Directory.Exists(baseDirectory))
+ break;
+ return FileUtility.LazySearchDirectory(baseDirectory, filter, searchSubdirs);
+ default:
+ throw new Exception("Invalid value for FileListType");
+ }
+
+ return files.Distinct();
}
}
}
View
11 src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs
@@ -192,7 +192,16 @@ void IObserver<SearchedFile>.OnNext(SearchedFile value)
void IObserver<SearchedFile>.OnError(Exception error)
{
- MessageService.ShowException(error);
+ // flatten AggregateException and
+ // filter OperationCanceledException
+ try {
+ if (error is AggregateException)
+ ((AggregateException)error).Flatten().Handle(ex => ex is OperationCanceledException);
+ else if (!(error is OperationCanceledException))
+ throw error;
+ } catch (Exception ex) {
+ MessageService.ShowException(ex);
+ }
OnCompleted();
}
View
6 src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs
@@ -104,7 +104,7 @@ void FindAllButtonClicked(object sender, EventArgs e)
{
WritebackOptions();
var monitor = WorkbenchSingleton.StatusBar.CreateProgressMonitor();
- monitor.TaskName = "Searching ...";
+ monitor.TaskName = StringParser.Parse("${res:AddIns.SearchReplace.SearchProgressTitle}");
try {
var results = SearchManager.FindAllParallel(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, selection);
SearchManager.ShowSearchResults(SearchOptions.FindPattern, results);
@@ -115,7 +115,7 @@ void BookmarkAllButtonClicked(object sender, EventArgs e)
{
WritebackOptions();
var monitor = WorkbenchSingleton.StatusBar.CreateProgressMonitor();
- monitor.TaskName = "Searching ...";
+ monitor.TaskName = StringParser.Parse("${res:AddIns.SearchReplace.SearchProgressTitle}");
try {
var results = SearchManager.FindAllParallel(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories);
SearchManager.MarkAll(results);
@@ -127,7 +127,7 @@ void ReplaceAllButtonClicked(object sender, EventArgs e)
WritebackOptions();
int count = -1;
AsynchronousWaitDialog.RunInCancellableWaitDialog(
- "Searching ...", null,
+ StringParser.Parse("${res:AddIns.SearchReplace.SearchProgressTitle}"), null,
monitor => {
var results = SearchManager.FindAll(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, selection);
count = SearchManager.ReplaceAll(results, SearchOptions.ReplacePattern, monitor.CancellationToken);
Please sign in to comment.
Something went wrong with that request. Please try again.