Browse Source

file clean-up + catching OperationCanceledExceptions

pull/23/head
Siegfried Pammer 14 years ago
parent
commit
6a0d2bcf91
  1. 140
      src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs
  2. 11
      src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs
  3. 6
      src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs

140
src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs

@ -27,54 +27,7 @@ namespace SearchAndReplace
/// </summary> /// </summary>
public class SearchManager public class SearchManager
{ {
static IEnumerable<FileName> GenerateFileList(SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false) #region FindAll
{
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();
}
public static IObservable<SearchedFile> FindAllParallel(IProgressMonitor progressMonitor, string pattern, bool ignoreCase, bool matchWholeWords, SearchMode mode, 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) SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false, ISegment selection = null)
{ {
@ -237,16 +190,7 @@ namespace SearchAndReplace
return null; return null;
} }
} }
#endregion
public static ITextEditor GetActiveTextEditor()
{
ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
if (provider != null) {
return provider.TextEditor;
} else {
return null;
}
}
#region FindNext #region FindNext
static SearchRegion currentSearchRegion; static SearchRegion currentSearchRegion;
@ -410,6 +354,7 @@ namespace SearchAndReplace
} }
#endregion #endregion
#region Mark & Replace(All)
public static void MarkAll(IObservable<SearchedFile> results) public static void MarkAll(IObservable<SearchedFile> results)
{ {
int count = 0; int count = 0;
@ -445,6 +390,15 @@ namespace SearchAndReplace
return count; 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) static void MarkResult(SearchResultMatch result, bool switchToOpenedView = true)
{ {
@ -462,7 +416,9 @@ namespace SearchAndReplace
BookmarkManager.AddMark(new Bookmark(result.FileName, result.StartLocation)); BookmarkManager.AddMark(new Bookmark(result.FileName, result.StartLocation));
} }
} }
#endregion
#region TextEditor helpers
static ITextEditor OpenTextArea(string fileName, bool switchToOpenedView = true) static ITextEditor OpenTextArea(string fileName, bool switchToOpenedView = true)
{ {
ITextEditorProvider textEditorProvider; ITextEditorProvider textEditorProvider;
@ -478,6 +434,18 @@ namespace SearchAndReplace
return null; 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) public static void ShowReplaceDoneMessage(int count)
{ {
if (count == 0) { if (count == 0) {
@ -506,7 +474,9 @@ namespace SearchAndReplace
{ {
MessageService.ShowMessage("${res:Dialog.NewProject.SearchReplace.SearchStringNotFound}", "${res:Dialog.NewProject.SearchReplace.SearchStringNotFound.Title}"); MessageService.ShowMessage("${res:Dialog.NewProject.SearchReplace.SearchStringNotFound}", "${res:Dialog.NewProject.SearchReplace.SearchStringNotFound.Title}");
} }
#endregion
#region Result display helpers
public static void SelectResult(SearchResultMatch result) public static void SelectResult(SearchResultMatch result)
{ {
if (result == null) { if (result == null) {
@ -534,13 +504,53 @@ namespace SearchAndReplace
&& result.Length == editor.SelectionLength; && result.Length == editor.SelectionLength;
} }
public static void Replace(SearchResultMatch lastMatch, string replacement) public static void ShowSearchResults(string pattern, IObservable<SearchedFile> results)
{ {
if (lastMatch == null) string title = StringParser.Parse("${res:MainWindow.Windows.SearchResultPanel.OccurrencesOf}",
return; new StringTagPair("Pattern", pattern));
ITextEditor textArea = OpenTextArea(lastMatch.FileName); SearchResultsPad.Instance.ShowSearchResults(title, results);
if (textArea != null) SearchResultsPad.Instance.BringToFront();
textArea.Document.Replace(lastMatch.StartOffset, lastMatch.Length, lastMatch.TransformReplacePattern(replacement)); }
#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();
} }
} }
} }

11
src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs

@ -192,7 +192,16 @@ namespace SearchAndReplace
void IObserver<SearchedFile>.OnError(Exception error) 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(); OnCompleted();
} }

6
src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs

@ -104,7 +104,7 @@ namespace SearchAndReplace
{ {
WritebackOptions(); WritebackOptions();
var monitor = WorkbenchSingleton.StatusBar.CreateProgressMonitor(); var monitor = WorkbenchSingleton.StatusBar.CreateProgressMonitor();
monitor.TaskName = "Searching ..."; monitor.TaskName = StringParser.Parse("${res:AddIns.SearchReplace.SearchProgressTitle}");
try { try {
var results = SearchManager.FindAllParallel(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, selection); 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); SearchManager.ShowSearchResults(SearchOptions.FindPattern, results);
@ -115,7 +115,7 @@ namespace SearchAndReplace
{ {
WritebackOptions(); WritebackOptions();
var monitor = WorkbenchSingleton.StatusBar.CreateProgressMonitor(); var monitor = WorkbenchSingleton.StatusBar.CreateProgressMonitor();
monitor.TaskName = "Searching ..."; monitor.TaskName = StringParser.Parse("${res:AddIns.SearchReplace.SearchProgressTitle}");
try { try {
var results = SearchManager.FindAllParallel(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories); var results = SearchManager.FindAllParallel(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories);
SearchManager.MarkAll(results); SearchManager.MarkAll(results);
@ -127,7 +127,7 @@ namespace SearchAndReplace
WritebackOptions(); WritebackOptions();
int count = -1; int count = -1;
AsynchronousWaitDialog.RunInCancellableWaitDialog( AsynchronousWaitDialog.RunInCancellableWaitDialog(
"Searching ...", null, StringParser.Parse("${res:AddIns.SearchReplace.SearchProgressTitle}"), null,
monitor => { monitor => {
var results = SearchManager.FindAll(monitor, SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, selection); 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); count = SearchManager.ReplaceAll(results, SearchOptions.ReplacePattern, monitor.CancellationToken);

Loading…
Cancel
Save