From e0f184d63ee6ff162832a0606f4320d4c40b1ec3 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 8 Oct 2011 14:20:59 +0200 Subject: [PATCH] create new search using Rx --- .../Commands/SearchMainMenuCommands.cs | 72 ++-- .../AllOpenDocumentIterator.cs | 124 ------ .../CurrentDocumentIterator.cs | 63 --- .../DirectoryDocumentIterator.cs | 100 ----- .../DocumentIterator/IDocumentIterator.cs | 91 ---- .../WholeProjectDocumentIterator.cs | 99 ----- .../WholeSolutionDocumentIterator.cs | 101 ----- .../{ITextIteratorBuilder.cs => Enums.cs} | 13 +- .../SearchAndReplace/Project/Engine/Search.cs | 159 ------- .../Project/Engine/SearchManager.cs | 132 ++++++ .../Engine/SearchReplaceInFilesManager.cs | 89 ---- .../Project/Engine/SearchReplaceManager.cs | 393 ------------------ .../Project/Engine/SearchReplaceUtilities.cs | 112 ----- .../BoyerMooreSearchStrategy.cs | 128 ------ .../BruteForceSearchStrategy.cs | 91 ---- .../Engine/SearchStrategy/ISearchStrategy.cs | 35 -- .../SearchStrategy/KMPSearchStrategy.cs | 78 ---- .../SearchStrategy/RegExSearchStrategy.cs | 103 ----- .../SearchStrategy/WildcardSearchStrategy.cs | 190 --------- .../TextIterator/ForwardTextIterator.cs | 163 -------- .../ForwardTextIteratorBuilder.cs | 18 - .../Engine/TextIterator/ITextIterator.cs | 69 --- .../Project/Engine/TextSelection.cs | 45 -- .../Project/Gui/DefaultSearchResult.cs | 66 +++ .../Project/Gui/SearchAndReplacePanel.cs | 133 +++--- .../Project/SearchAndReplace.csproj | 26 +- .../SearchAndReplace/Project/SearchOptions.cs | 17 +- .../Search/ISearchStrategy.cs | 4 +- .../Search/RegexSearchStrategy.cs | 5 +- .../Search/SearchPanel.xaml.cs | 2 +- .../Search/SearchStrategyFactory.cs | 9 +- .../Src/Editor/Search/ISearchResultFactory.cs | 2 + .../Src/Editor/Search/SearchResultsPad.cs | 20 + .../Project/Src/Services/MimeTypeDetection.cs | 39 +- 34 files changed, 384 insertions(+), 2407 deletions(-) delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/AllOpenDocumentIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/CurrentDocumentIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/DirectoryDocumentIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/IDocumentIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeProjectDocumentIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeSolutionDocumentIterator.cs rename src/AddIns/Misc/SearchAndReplace/Project/Engine/{ITextIteratorBuilder.cs => Enums.cs} (52%) delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/Search.cs create mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceInFilesManager.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceManager.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BoyerMooreSearchStrategy.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BruteForceSearchStrategy.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/ISearchStrategy.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/KMPSearchStrategy.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/RegExSearchStrategy.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/WildcardSearchStrategy.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIteratorBuilder.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ITextIterator.cs delete mode 100644 src/AddIns/Misc/SearchAndReplace/Project/Engine/TextSelection.cs diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Commands/SearchMainMenuCommands.cs b/src/AddIns/Misc/SearchAndReplace/Project/Commands/SearchMainMenuCommands.cs index 99642f7876..2202e579a6 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/Commands/SearchMainMenuCommands.cs +++ b/src/AddIns/Misc/SearchAndReplace/Project/Commands/SearchMainMenuCommands.cs @@ -12,13 +12,13 @@ namespace SearchAndReplace public static void SetSearchPattern() { // Get Highlighted value and set it to FindDialog.searchPattern - ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor(); - if (textArea != null) { - string selectedText = textArea.SelectedText; - if (selectedText != null && selectedText.Length > 0 && !IsMultipleLines(selectedText)) { - SearchOptions.CurrentFindPattern = selectedText; - } - } +// ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor(); +// if (textArea != null) { +// string selectedText = textArea.SelectedText; +// if (selectedText != null && selectedText.Length > 0 && !IsMultipleLines(selectedText)) { +// SearchOptions.CurrentFindPattern = selectedText; +// } +// } } public override void Run() @@ -38,7 +38,7 @@ namespace SearchAndReplace public override void Run() { if (SearchOptions.CurrentFindPattern.Length > 0) { - SearchReplaceManager.FindNext(null); +// SearchReplaceManager.FindNext(null); } else { Find find = new Find(); find.Run(); @@ -65,34 +65,34 @@ namespace SearchAndReplace { public override void Run() { - ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor(); - if (textArea == null) { - return; - } - - // Determine what text we should search for. - string textToFind; - - string selectedText = textArea.SelectedText; - if (selectedText.Length > 0) { - if (Find.IsMultipleLines(selectedText)) { - // Locate the nearest word at the selection start. - textToFind = textArea.Document.GetWordAt(textArea.SelectionStart); - } else { - // Search for selected text. - textToFind = selectedText; - } - } else { - textToFind = textArea.Document.GetWordAt(textArea.Caret.Offset); - } - - if (textToFind != null && textToFind.Length > 0) { - SearchOptions.CurrentFindPattern = textToFind; - if (SearchOptions.DocumentIteratorType == DocumentIteratorType.CurrentSelection) { - SearchOptions.DocumentIteratorType = DocumentIteratorType.CurrentDocument; - } - SearchReplaceManager.FindNext(null); - } +// ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor(); +// if (textArea == null) { +// return; +// } +// +// // Determine what text we should search for. +// string textToFind; +// +// string selectedText = textArea.SelectedText; +// if (selectedText.Length > 0) { +// if (Find.IsMultipleLines(selectedText)) { +// // Locate the nearest word at the selection start. +// textToFind = textArea.Document.GetWordAt(textArea.SelectionStart); +// } else { +// // Search for selected text. +// textToFind = selectedText; +// } +// } else { +// textToFind = textArea.Document.GetWordAt(textArea.Caret.Offset); +// } +// +// if (textToFind != null && textToFind.Length > 0) { +// SearchOptions.CurrentFindPattern = textToFind; +// if (SearchOptions.DocumentIteratorType == SearchMode.CurrentSelection) { +// SearchOptions.DocumentIteratorType = SearchMode.CurrentDocument; +// } +// SearchReplaceManager.FindNext(null); +// } } } diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/AllOpenDocumentIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/AllOpenDocumentIterator.cs deleted file mode 100644 index a96f8d7699..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/AllOpenDocumentIterator.cs +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor; -using System; -using System.Linq; -using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Editor.Search; -using ICSharpCode.SharpDevelop.Gui; -using ICSharpCode.SharpDevelop.Refactoring; - -namespace SearchAndReplace -{ - public class AllOpenDocumentIterator : IDocumentIterator - { - int startIndex = -1; - int curIndex = -1; - bool resetted = true; - - public AllOpenDocumentIterator() - { - Reset(); - } - - public string CurrentFileName { - get { - IViewContent viewContent = GetCurrentTextEditorViewContent(); - if (viewContent != null) { - return viewContent.PrimaryFileName; - } - return null; - } - } - - IViewContent GetCurrentTextEditorViewContent() - { - GetCurIndex(); - if (curIndex >= 0) { - IViewContent viewContent = WorkbenchSingleton.Workbench.ViewContentCollection.ToList()[curIndex]; - if (viewContent is ITextEditorProvider) { - return viewContent; - } - } - return null; - } - - public ProvidedDocumentInformation Current { - get { - IViewContent viewContent = GetCurrentTextEditorViewContent(); - if (viewContent != null) { - ITextEditor textEditor = (((ITextEditorProvider)viewContent).TextEditor); - return new ProvidedDocumentInformation(textEditor.Document, - CurrentFileName, - textEditor); - } - return null; - } - } - - void GetCurIndex() - { - IViewContent[] viewContentCollection = WorkbenchSingleton.Workbench.ViewContentCollection.ToArray(); - int viewCount = WorkbenchSingleton.Workbench.ViewContentCollection.Count; - if (curIndex == -1 || curIndex >= viewCount) { - for (int i = 0; i < viewCount; ++i) { - if (WorkbenchSingleton.Workbench.ActiveViewContent == viewContentCollection[i]) { - curIndex = i; - return; - } - } - curIndex = -1; - } - } - - public bool MoveForward() - { - GetCurIndex(); - if (curIndex < 0) { - return false; - } - - if (resetted) { - resetted = false; - return true; - } - - curIndex = (curIndex + 1) % WorkbenchSingleton.Workbench.ViewContentCollection.Count; - if (curIndex == startIndex) { - return false; - } - return true; - } - - public bool MoveBackward() - { - GetCurIndex(); - if (curIndex < 0) { - return false; - } - if (resetted) { - resetted = false; - return true; - } - - if (curIndex == 0) { - curIndex = WorkbenchSingleton.Workbench.ViewContentCollection.Count - 1; - } - - if (curIndex > 0) { - --curIndex; - return true; - } - return false; - } - - public void Reset() - { - curIndex = -1; - GetCurIndex(); - startIndex = curIndex; - resetted = true; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/CurrentDocumentIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/CurrentDocumentIterator.cs deleted file mode 100644 index 32db08e85f..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/CurrentDocumentIterator.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor; -using System; -using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Editor.Search; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - public class CurrentDocumentIterator : IDocumentIterator - { - bool didRead = false; - - public CurrentDocumentIterator() - { - Reset(); - } - - public string CurrentFileName { - get { - if (!SearchReplaceUtilities.IsTextAreaSelected) { - return null; - } - return WorkbenchSingleton.Workbench.ActiveViewContent.PrimaryFileName; - } - } - - public ProvidedDocumentInformation Current { - get { - ITextEditor textEditor = SearchReplaceUtilities.GetActiveTextEditor(); - if (textEditor != null) - return new ProvidedDocumentInformation(textEditor.Document, CurrentFileName, textEditor); - else - return null; - } - } - - public bool MoveForward() - { - if (!SearchReplaceUtilities.IsTextAreaSelected) { - return false; - } - if (didRead) { - return false; - } - didRead = true; - - return true; - } - - public bool MoveBackward() - { - return MoveForward(); - } - - public void Reset() - { - didRead = false; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/DirectoryDocumentIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/DirectoryDocumentIterator.cs deleted file mode 100644 index ba24235744..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/DirectoryDocumentIterator.cs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop; -using System; -using System.Collections.Generic; -using System.IO; -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop.Editor; -using ICSharpCode.SharpDevelop.Editor.Search; -using ICSharpCode.SharpDevelop.Gui; -using ICSharpCode.SharpDevelop.Refactoring; - -namespace SearchAndReplace -{ - public class DirectoryDocumentIterator : IDocumentIterator - { - string searchDirectory; - string fileMask; - bool searchSubdirectories; - - List files = null; - int curIndex = -1; - - public DirectoryDocumentIterator(string searchDirectory, string fileMask, bool searchSubdirectories) - { - this.searchDirectory = searchDirectory; - this.fileMask = fileMask; - this.searchSubdirectories = searchSubdirectories; - - Reset(); - } - - public string CurrentFileName { - get { - if (curIndex < 0 || curIndex >= files.Count) { - return null; - } - - return files[curIndex].ToString();; - } - } - - public ProvidedDocumentInformation Current { - get { - if (curIndex < 0 || curIndex >= files.Count) { - return null; - } - string fileName = files[curIndex].ToString(); - if (!File.Exists(fileName) || !SearchReplaceUtilities.IsSearchable(fileName)) { - ++curIndex; - return Current; - } - IDocument document; - foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) { - if (content.PrimaryFileName != null && - FileUtility.IsEqualFileName(content.PrimaryFileName, fileName) && - content is ITextEditorProvider) { - document = ((ITextEditorProvider)content).TextEditor.Document; - return new ProvidedDocumentInformation(document, - fileName, - 0); - } - } - ITextBuffer fileContent; - try { - fileContent = ParserService.GetParseableFileContent(fileName); - } catch (Exception) { - return null; - } - return new ProvidedDocumentInformation(fileContent, - fileName, - 0); - } - } - - public bool MoveForward() - { - if (curIndex == -1) { - files = FileUtility.SearchDirectory(this.searchDirectory, this.fileMask, this.searchSubdirectories); - } - return ++curIndex < files.Count; - } - - public bool MoveBackward() - { - if (curIndex == -1) { - curIndex = files.Count - 1; - return true; - } - return --curIndex >= -1; - } - - - public void Reset() - { - curIndex = -1; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/IDocumentIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/IDocumentIterator.cs deleted file mode 100644 index 7a235c21fe..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/IDocumentIterator.cs +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; - -namespace SearchAndReplace -{ - public enum DocumentIteratorType { - CurrentDocument, - CurrentSelection, - AllOpenFiles, - WholeProject, - WholeSolution, - Directory // only used for search in files - } - - /// - /// Represents a bi-directional iterator which could move froward/backward - /// in a document queue. Note that after move forward is called - /// move backward needn't to function correctly either move forward or move - /// backward is called but they're not mixed. After a reset the move operation - /// can be switched. - /// - public interface IDocumentIterator - { - /// - /// Returns the current ProvidedDocumentInformation. This method - /// usually creates a new ProvidedDocumentInformation object which can - /// be time consuming - /// - ProvidedDocumentInformation Current { - get; - } - - /// - /// Returns the file name of the current provided document information. This - /// property usually is not time consuming - /// - string CurrentFileName { - get; - } - - /// - /// Moves the iterator one document forward. - /// - bool MoveForward(); - - /// - /// Moves the iterator one document backward. - /// - bool MoveBackward(); - - /// - /// Resets the iterator to the start position. - /// - void Reset(); - } - - /// - /// A document iterator which never returns any results. - /// - public sealed class DummyDocumentIterator : IDocumentIterator - { - public ProvidedDocumentInformation Current { - get { - return null; - } - } - - public string CurrentFileName { - get { - return null; - } - } - - public bool MoveForward() - { - return false; - } - - public bool MoveBackward() - { - return false; - } - - public void Reset() - { - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeProjectDocumentIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeProjectDocumentIterator.cs deleted file mode 100644 index 2d530681de..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeProjectDocumentIterator.cs +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop; -using System; -using System.Collections; -using System.IO; -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop.Editor; -using ICSharpCode.SharpDevelop.Editor.Search; -using ICSharpCode.SharpDevelop.Gui; -using ICSharpCode.SharpDevelop.Project; -using ICSharpCode.SharpDevelop.Refactoring; - -namespace SearchAndReplace -{ - public class WholeProjectDocumentIterator : IDocumentIterator - { - ArrayList files = new ArrayList(); - int curIndex = -1; - - public WholeProjectDocumentIterator() - { - Reset(); - } - - public string CurrentFileName { - get { - if (curIndex < 0 || curIndex >= files.Count) { - return null; - } - - return files[curIndex].ToString();; - } - } - - public ProvidedDocumentInformation Current { - get { - if (curIndex < 0 || curIndex >= files.Count) { - return null; - } - if (!File.Exists(files[curIndex].ToString())) { - ++curIndex; - return Current; - } - IDocument document; - string fileName = files[curIndex].ToString(); - foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) { - if (content.PrimaryFileName != null && - FileUtility.IsEqualFileName(content.PrimaryFileName, fileName) && - content is ITextEditorProvider) - { - document = (((ITextEditorProvider)content).TextEditor).Document; - return new ProvidedDocumentInformation(document, - fileName, - 0); - } - } - ITextBuffer fileContent; - try { - fileContent = ParserService.GetParseableFileContent(fileName); - } catch (Exception) { - return null; - } - return new ProvidedDocumentInformation(fileContent, - fileName, - 0); - } - } - - public bool MoveForward() - { - return ++curIndex < files.Count; - } - - public bool MoveBackward() - { - if (curIndex == -1) { - curIndex = files.Count - 1; - return true; - } - return --curIndex >= -1; - } - - public void Reset() - { - files.Clear(); - if (ProjectService.CurrentProject != null) { - foreach (ProjectItem item in ProjectService.CurrentProject.Items) { - if (item is FileProjectItem && SearchReplaceUtilities.IsSearchable(item.FileName)) { - files.Add(item.FileName); - } - } - } - - curIndex = -1; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeSolutionDocumentIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeSolutionDocumentIterator.cs deleted file mode 100644 index b7d9dabee7..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/DocumentIterator/WholeSolutionDocumentIterator.cs +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop; -using System; -using System.Collections; -using System.IO; -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop.Editor; -using ICSharpCode.SharpDevelop.Editor.Search; -using ICSharpCode.SharpDevelop.Gui; -using ICSharpCode.SharpDevelop.Project; -using ICSharpCode.SharpDevelop.Refactoring; - -namespace SearchAndReplace -{ - public class WholeSolutionDocumentIterator : IDocumentIterator - { - ArrayList files = new ArrayList(); - int curIndex = -1; - - public WholeSolutionDocumentIterator() - { - Reset(); - } - - public string CurrentFileName { - get { - if (curIndex < 0 || curIndex >= files.Count) { - return null; - } - - return files[curIndex].ToString();; - } - } - - public ProvidedDocumentInformation Current { - get { - if (curIndex < 0 || curIndex >= files.Count) { - return null; - } - if (!File.Exists(files[curIndex].ToString())) { - ++curIndex; - return Current; - } - IDocument document; - string fileName = files[curIndex].ToString(); - foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) { - if (content.PrimaryFileName != null && - FileUtility.IsEqualFileName(content.PrimaryFileName, fileName) && - content is ITextEditorProvider) - { - document = (((ITextEditorProvider)content).TextEditor).Document; - return new ProvidedDocumentInformation(document, - fileName, - 0); - } - } - ITextBuffer fileContent; - try { - fileContent = ParserService.GetParseableFileContent(fileName); - } catch (Exception) { - return null; - } - return new ProvidedDocumentInformation(fileContent, - fileName, - 0); - } - } - - public bool MoveForward() - { - return ++curIndex < files.Count; - } - - public bool MoveBackward() - { - if (curIndex == -1) { - curIndex = files.Count - 1; - return true; - } - return --curIndex >= -1; - } - - public void Reset() - { - files.Clear(); - if (ProjectService.OpenSolution != null) { - foreach (IProject project in ProjectService.OpenSolution.Projects) { - foreach (ProjectItem item in project.Items) { - if (item is FileProjectItem && SearchReplaceUtilities.IsSearchable(item.FileName)) { - files.Add(item.FileName); - } - } - } - } - - curIndex = -1; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/ITextIteratorBuilder.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/Enums.cs similarity index 52% rename from src/AddIns/Misc/SearchAndReplace/Project/Engine/ITextIteratorBuilder.cs rename to src/AddIns/Misc/SearchAndReplace/Project/Engine/Enums.cs index fb334806c6..2f4ce555f8 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/ITextIteratorBuilder.cs +++ b/src/AddIns/Misc/SearchAndReplace/Project/Engine/Enums.cs @@ -1,16 +1,17 @@ // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) -using ICSharpCode.SharpDevelop.Editor.Search; using System; namespace SearchAndReplace { - /// - /// Builds a text iterator object. - /// - public interface ITextIteratorBuilder + public enum SearchTarget { - ITextIterator BuildTextIterator(ProvidedDocumentInformation info); + CurrentDocument, + CurrentSelection, + AllOpenFiles, + WholeProject, + WholeSolution, + Directory } } diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/Search.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/Search.cs deleted file mode 100644 index 111e42d2c4..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/Search.cs +++ /dev/null @@ -1,159 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Diagnostics; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - public class Search - { - ISearchStrategy searchStrategy = null; - IDocumentIterator documentIterator = null; - ITextIterator textIterator = null; - ITextIteratorBuilder textIteratorBuilder = null; - ProvidedDocumentInformation info = null; - - public ProvidedDocumentInformation CurrentDocumentInformation { - get { - return info; - } - } - - public ITextIteratorBuilder TextIteratorBuilder { - get { - return textIteratorBuilder; - } - set { - textIteratorBuilder = value; - } - } - - public ITextIterator TextIterator { - get { - return textIterator; - } - } - - public ISearchStrategy SearchStrategy { - get { - return searchStrategy; - } - set { - searchStrategy = value; - } - } - - public IDocumentIterator DocumentIterator { - get { - return documentIterator; - } - set { - documentIterator = value; - } - } - - SearchResultMatch CreateNamedSearchResult(SearchResultMatch pos) - { - if (info == null || pos == null) { - return null; - } - pos.ProvidedDocumentInformation = info; - return pos; - } - - public void Reset() - { - documentIterator.Reset(); - textIterator = null; - } - - public void Replace(int offset, int length, string pattern) - { - if (CurrentDocumentInformation != null && TextIterator != null) { - CurrentDocumentInformation.Replace(offset, length, pattern); - TextIterator.InformReplace(offset, length, pattern.Length); - } - } - - public SearchResultMatch FindNext(IProgressMonitor monitor) - { - // insanity check - Debug.Assert(searchStrategy != null); - Debug.Assert(documentIterator != null); - Debug.Assert(textIteratorBuilder != null); - - if (monitor != null && monitor.CancellationToken.IsCancellationRequested) - return null; - - if (info != null && textIterator != null && documentIterator.CurrentFileName != null) { - ProvidedDocumentInformation currentInfo = documentIterator.Current; - if (currentInfo != null) { - if (!info.Equals(currentInfo)) { // create new iterator, if document changed - info = currentInfo; - textIterator = textIteratorBuilder.BuildTextIterator(info); - } else { // old document -> initialize iterator position to caret pos - textIterator.Position = info.CurrentOffset; - } - - SearchResultMatch result = CreateNamedSearchResult(searchStrategy.FindNext(textIterator)); - if (result != null) { - info.CurrentOffset = textIterator.Position; - return result; - } - } - } - - // not found or first start -> move forward to the next document - if (documentIterator.MoveForward()) { - info = documentIterator.Current; - // document is valid for searching -> set iterator & fileName - if (info != null && info.EndOffset >= 0 && info.EndOffset <= info.Document.TextLength) { - textIterator = textIteratorBuilder.BuildTextIterator(info); - } else { - textIterator = null; - } - - return FindNext(monitor); - } - return null; - } - - public SearchResultMatch FindNext(int offset, int length) - { - if (info != null && textIterator != null && documentIterator.CurrentFileName != null) { - ProvidedDocumentInformation currentInfo = documentIterator.Current; - if (currentInfo != null) { - if (!info.Equals(currentInfo)) { // create new iterator, if document changed - info = currentInfo; - textIterator = textIteratorBuilder.BuildTextIterator(info); - } else { // old document -> initialize iterator position to caret pos - textIterator.Position = info.CurrentOffset; - } - - SearchResultMatch result = CreateNamedSearchResult(searchStrategy.FindNext(textIterator, offset, length)); - if (result != null) { - info.CurrentOffset = textIterator.Position; - return result; - } - } - } - - // not found or first start -> move forward to the next document - if (documentIterator.MoveForward()) { - info = documentIterator.Current; - // document is valid for searching -> set iterator & fileName - if (info != null && info.EndOffset >= 0 && info.EndOffset <= info.Document.TextLength) { - textIterator = textIteratorBuilder.BuildTextIterator(info); - } else { - textIterator = null; - } - - return FindNext(offset, length); - } - return null; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs new file mode 100644 index 0000000000..e97df5b613 --- /dev/null +++ b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs @@ -0,0 +1,132 @@ +/* + * Created by SharpDevelop. + * User: Siegfried + * Date: 06.10.2011 + * Time: 21:33 + * + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.AvalonEdit.Document; +using ICSharpCode.AvalonEdit.Search; +using ICSharpCode.Core; +using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.Editor; +using ICSharpCode.SharpDevelop.Editor.Search; +using ICSharpCode.SharpDevelop.Gui; +using ICSharpCode.SharpDevelop.Project; + +namespace SearchAndReplace +{ + /// + /// Description of SearchManager. + /// + public class SearchManager + { + static IEnumerable GenerateFileList(SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false) + { + List files = new List(); + + 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()) + files.Add(editor.TextEditor.FileName); + break; + case SearchTarget.WholeProject: + if (ProjectService.CurrentProject == null) + break; + foreach (FileProjectItem item in ProjectService.CurrentProject.Items.OfType()) + 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()) + files.Add(new FileName(item.FileName)); + break; + case SearchTarget.Directory: + if (!Directory.Exists(baseDirectory)) + break; + foreach (var name in FileUtility.SearchDirectory(baseDirectory, filter, searchSubdirs)) + files.Add(new FileName(name)); + break; + default: + throw new Exception("Invalid value for FileListType"); + } + + return files.Distinct(); + } + + public static IObservable FindAll(string pattern, bool ignoreCase, bool matchWholeWords, SearchMode mode, + SearchTarget target, string baseDirectory = null, string filter = "*.*", bool searchSubdirs = false) + { + CancellationTokenSource cts = new CancellationTokenSource(); + var monitor = WorkbenchSingleton.Workbench.StatusBar.CreateProgressMonitor(cts.Token); + monitor.TaskName = "Find All"; + monitor.Status = OperationStatus.Normal; + var strategy = SearchStrategyFactory.Create(pattern, ignoreCase, matchWholeWords, mode); + ParseableFileContentFinder fileFinder = new ParseableFileContentFinder(); + var fileList = GenerateFileList(target, baseDirectory, filter, searchSubdirs); + return new SearchRun(strategy, fileFinder, fileList, monitor, cts); + } + + class SearchRun : IObservable, IDisposable + { + IObserver observer; + ISearchStrategy strategy; + ParseableFileContentFinder fileFinder; + IEnumerable fileList; + IProgressMonitor monitor; + CancellationTokenSource cts; + + public SearchRun(ISearchStrategy strategy, ParseableFileContentFinder fileFinder, IEnumerable fileList, IProgressMonitor monitor, CancellationTokenSource cts) + { + this.strategy = strategy; + this.fileFinder = fileFinder; + this.fileList = fileList; + this.monitor = monitor; + this.cts = cts; + } + + public IDisposable Subscribe(IObserver observer) + { + this.observer = observer; + new System.Threading.Tasks.Task(delegate { Parallel.ForEach(fileList, fileName => SearchFile(fileFinder.Create(fileName).CreateSnapshot(), strategy, monitor.CancellationToken)); }).Start(); + return this; + } + + public void Dispose() + { + if (!cts.IsCancellationRequested) + cts.Cancel(); + monitor.Dispose(); + } + + void SearchFile(ITextBuffer buffer, ISearchStrategy strategy, CancellationToken ct) + { + if (!MimeTypeDetection.FindMimeType(buffer).StartsWith("text/")) + return; + var source = DocumentUtilitites.GetTextSource(buffer); + foreach(var result in strategy.FindAll(source)) { + ct.ThrowIfCancellationRequested(); + observer.OnNext(new SearchResultMatch(result.Offset, result.Length)); + } + lock (monitor) + monitor.Progress += 1 / fileList.Count(); + } + } + } +} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceInFilesManager.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceInFilesManager.cs deleted file mode 100644 index 6a58abc05f..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceInFilesManager.cs +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Collections.Generic; -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - public static class SearchInFilesManager - { - static Search find = new Search(); - - static string currentFileName = String.Empty; - - static SearchInFilesManager() - { - find.TextIteratorBuilder = new ForwardTextIteratorBuilder(); - } - - static void SetSearchOptions(IProgressMonitor monitor) - { - find.SearchStrategy = SearchReplaceUtilities.CreateSearchStrategy(SearchOptions.SearchStrategyType); - find.DocumentIterator = SearchReplaceUtilities.CreateDocumentIterator(SearchOptions.DocumentIteratorType, monitor); - } - - static bool InitializeSearchInFiles(IProgressMonitor monitor) - { - SetSearchOptions(monitor); - - find.Reset(); - if (!find.SearchStrategy.CompilePattern(monitor)) - return false; - - currentFileName = String.Empty; - return true; - } - - static void FinishSearchInFiles(List results) - { - ShowSearchResults(SearchOptions.FindPattern, results); - } - - public static void ShowSearchResults(string pattern, List results) - { - string title = StringParser.Parse("${res:MainWindow.Windows.SearchResultPanel.OccurrencesOf}", - new StringTagPair("Pattern", pattern)); - SearchResultsPad.Instance.ShowSearchResults(title, results); - SearchResultsPad.Instance.BringToFront(); - } - - public static void FindAll(IProgressMonitor monitor) - { - if (!InitializeSearchInFiles(monitor)) { - return; - } - - List results = new List(); - while (true) { - SearchResultMatch result = find.FindNext(monitor); - if (result == null) { - break; - } - results.Add(result); - } - FinishSearchInFiles(results); - } - - public static void FindAll(int offset, int length, IProgressMonitor monitor) - { - if (!InitializeSearchInFiles(monitor)) { - return; - } - - List results = new List(); - while (true) { - SearchResultMatch result = find.FindNext(offset, length); - if (result == null) { - break; - } - results.Add(result); - } - FinishSearchInFiles(results); - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceManager.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceManager.cs deleted file mode 100644 index 9ac0de602f..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceManager.cs +++ /dev/null @@ -1,393 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using System; -using System.Collections.Generic; -using System.Windows.Forms; - -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Bookmarks; -using ICSharpCode.SharpDevelop.Editor; -using ICSharpCode.SharpDevelop.Editor.Search; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - public class SearchReplaceManager - { - public static SearchAndReplaceDialog SearchAndReplaceDialog = null; - - static Search find = new Search(); - - static SearchReplaceManager() - { - find.TextIteratorBuilder = new ForwardTextIteratorBuilder(); - } - - static void SetSearchOptions(IProgressMonitor monitor) - { - find.SearchStrategy = SearchReplaceUtilities.CreateSearchStrategy(SearchOptions.SearchStrategyType); - find.DocumentIterator = SearchReplaceUtilities.CreateDocumentIterator(SearchOptions.DocumentIteratorType, monitor); - } - - public static void Replace(IProgressMonitor monitor) - { - SetSearchOptions(monitor); - if (lastResult != null) { - ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; - if (provider != null) { - ITextEditor textarea = provider.TextEditor; - - if (textarea.SelectionStart == lastResult.Offset - && textarea.SelectionLength == lastResult.Length - && lastResult.FileName == textarea.FileName) - { - string replacePattern = lastResult.TransformReplacePattern(SearchOptions.ReplacePattern); - - using (textarea.Document.OpenUndoGroup()) { - textarea.Document.Replace(lastResult.Offset, lastResult.Length, replacePattern); - textarea.Select(lastResult.Offset + replacePattern.Length, 0); // clear selection and set caret position - } - } - } - } - FindNext(monitor); - } - - static TextSelection textSelection; - - public static void ReplaceFirstInSelection(int offset, int length, IProgressMonitor monitor) - { - SetSearchOptions(monitor); - FindFirstInSelection(offset, length, monitor); - } - - public static bool ReplaceNextInSelection(IProgressMonitor monitor) - { - if (lastResult != null) { - ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; - if (provider != null) { - ITextEditor textarea = provider.TextEditor; - - if (textarea.SelectionStart == lastResult.Offset - && textarea.SelectionLength == lastResult.Length - && lastResult.FileName == textarea.FileName) - { - string replacePattern = lastResult.TransformReplacePattern(SearchOptions.ReplacePattern); - - using (textarea.Document.OpenUndoGroup()) { - textarea.Document.Replace(lastResult.Offset, lastResult.Length, replacePattern); - textarea.Select(lastResult.Offset + replacePattern.Length, 0); // clear selection and set caret position - } - - textSelection.Length -= lastResult.Length - replacePattern.Length; - } - } - } - return FindNextInSelection(monitor); - } - - public static void MarkAll(IProgressMonitor monitor) - { - SetSearchOptions(monitor); - ClearSelection(); - find.Reset(); - if (!find.SearchStrategy.CompilePattern(monitor)) - return; - List textAreas = new List(); - int count; - for (count = 0;; count++) { - SearchResultMatch result = SearchReplaceManager.find.FindNext(monitor); - - if (result == null) { - break; - } else { - MarkResult(textAreas, result); - } - } - find.Reset(); - ShowMarkDoneMessage(count, monitor); - } - - public static void MarkAll(int offset, int length, IProgressMonitor monitor) - { - SetSearchOptions(monitor); - find.Reset(); - - if (!find.SearchStrategy.CompilePattern(monitor)) - return; - - List textAreas = new List(); - int count; - for (count = 0;; count++) { - SearchResultMatch result = find.FindNext(offset, length); - if (result == null) { - break; - } else { - MarkResult(textAreas, result); - } - } - find.Reset(); - ShowMarkDoneMessage(count, monitor); - } - - static void MarkResult(List textAreas, SearchResultMatch result) - { - ITextEditor textArea = OpenTextArea(result.FileName); - if (textArea != null) { - if (!textAreas.Contains(textArea)) { - textAreas.Add(textArea); - } - textArea.Caret.Offset = result.Offset; - IDocumentLine segment = textArea.Document.GetLineForOffset(result.Offset); - - int lineNr = segment.LineNumber; - - foreach (var bookmark in BookmarkManager.GetBookmarks(result.FileName)) { - if (bookmark.CanToggle && bookmark.LineNumber == lineNr) { - // bookmark or breakpoint already exists at that line - return; - } - } - BookmarkManager.AddMark(new Bookmark(result.FileName, textArea.Document.OffsetToPosition(result.Offset))); - } - } - - static void ShowMarkDoneMessage(int count, IProgressMonitor monitor) - { - if (count == 0) { - ShowNotFoundMessage(monitor); - } else { - if (monitor != null) monitor.ShowingDialog = true; - MessageService.ShowMessage(StringParser.Parse("${res:ICSharpCode.TextEditor.Document.SearchReplaceManager.MarkAllDone}",new StringTagPair("Count", count.ToString())), - "${res:Global.FinishedCaptionText}"); - if (monitor != null) monitor.ShowingDialog = false; - } - } - - static void ShowReplaceDoneMessage(int count, IProgressMonitor monitor) - { - if (count == 0) { - ShowNotFoundMessage(monitor); - } else { - if (monitor != null) monitor.ShowingDialog = true; - MessageService.ShowMessage( - StringParser.Parse("${res:ICSharpCode.TextEditor.Document.SearchReplaceManager.ReplaceAllDone}", - new StringTagPair("Count", count.ToString())), - "${res:Global.FinishedCaptionText}"); - if (monitor != null) monitor.ShowingDialog = false; - } - } - - public static void ReplaceAll(IProgressMonitor monitor) - { - SetSearchOptions(monitor); - ClearSelection(); - find.Reset(); - if (!find.SearchStrategy.CompilePattern(monitor)) - return; - - List textAreas = new List(); - ITextEditor textArea = null; - for (int count = 0;; count++) { - SearchResultMatch result = SearchReplaceManager.find.FindNext(monitor); - - if (result == null) { - if (count != 0) { - foreach (ITextEditor ta in textAreas) { - ta.Document.EndUndoableAction(); - } - } - ShowReplaceDoneMessage(count, monitor); - find.Reset(); - return; - } else { - if (textArea == null || textArea.FileName != result.FileName) { - // we need to open another text area - textArea = OpenTextArea(result.FileName); - if (textArea != null) { - if (!textAreas.Contains(textArea)) { - textArea.Document.StartUndoableAction(); - textArea.Select(textArea.SelectionStart, 0); - textAreas.Add(textArea); - } - } - } - if (textArea != null) { - string transformedPattern = result.TransformReplacePattern(SearchOptions.ReplacePattern); - find.Replace(result.Offset, result.Length, transformedPattern); - if (find.CurrentDocumentInformation.IsDocumentCreatedFromTextBuffer) { - textArea.Document.Replace(result.Offset, result.Length, transformedPattern); - } - } else { - count--; - } - } - } - } - - public static void ReplaceAll(int offset, int length, IProgressMonitor monitor) - { - SetSearchOptions(monitor); - find.Reset(); - - if (!find.SearchStrategy.CompilePattern(monitor)) - return; - - for (int count = 0;; count++) { - SearchResultMatch result = find.FindNext(offset, length); - if (result == null) { - ShowReplaceDoneMessage(count, monitor); - return; - } - - string replacement = result.TransformReplacePattern(SearchOptions.ReplacePattern); - find.Replace(result.Offset, - result.Length, - replacement); - length -= result.Length - replacement.Length; - - // HACK - Move the cursor to the correct offset - the caret gets - // moved before the replace range if we replace a string with a - // single character. The ProvidedDocInfo.Replace method assumes that - // the current offset is at the end of the found text which it is not. - find.CurrentDocumentInformation.CurrentOffset = result.Offset + replacement.Length - 1; - } - } - - static SearchResultMatch lastResult = null; - - public static void FindNext(IProgressMonitor monitor) - { - SetSearchOptions(monitor); - if (find == null || - SearchOptions.FindPattern == null || - SearchOptions.FindPattern.Length == 0) { - return; - } - - if (!find.SearchStrategy.CompilePattern(monitor)) { - find.Reset(); - lastResult = null; - return; - } - - ITextEditor textArea = null; - while (textArea == null) { - SearchResultMatch result = find.FindNext(monitor); - if (result == null) { - ShowNotFoundMessage(monitor); - find.Reset(); - lastResult = null; - return; - } else { - textArea = OpenTextArea(result.FileName); - if (textArea != null) { - if (lastResult != null && lastResult.FileName == result.FileName && - textArea.Caret.Offset != lastResult.Offset + lastResult.Length) { - find.Reset(); - } - int startPos = Math.Min(textArea.Document.TextLength, Math.Max(0, result.Offset)); - int endPos = Math.Min(textArea.Document.TextLength, startPos + result.Length); - - textArea.Select(startPos, endPos - startPos); - lastResult = result; - } - } - } - } - - static bool foundAtLeastOneItem = false; - - public static void FindFirstInSelection(int offset, int length, IProgressMonitor monitor) - { - foundAtLeastOneItem = false; - textSelection = null; - SetSearchOptions(monitor); - - if (find == null || - SearchOptions.FindPattern == null || - SearchOptions.FindPattern.Length == 0) { - return; - } - - if (!find.SearchStrategy.CompilePattern(monitor)) { - find.Reset(); - lastResult = null; - return; - } - - textSelection = new TextSelection(offset, length); - FindNextInSelection(monitor); - } - - public static bool FindNextInSelection(IProgressMonitor monitor) - { - ITextEditor textArea = null; - while (textArea == null) { - SearchResultMatch result = find.FindNext(textSelection.Offset, textSelection.Length); - if (result == null) { - if (!foundAtLeastOneItem) { - ShowNotFoundMessage(monitor); - } - find.Reset(); - lastResult = null; - foundAtLeastOneItem = false; - return false; - } else { - textArea = OpenTextArea(result.FileName); - if (textArea != null) { - foundAtLeastOneItem = true; - if (lastResult != null && lastResult.FileName == result.FileName && - textArea.Caret.Offset != lastResult.Offset + lastResult.Length) { - } - int startPos = Math.Min(textArea.Document.TextLength, Math.Max(0, result.Offset)); - int endPos = Math.Min(textArea.Document.TextLength, startPos + result.Length); - textArea.Select(startPos, endPos - startPos); - lastResult = result; - } - } - } - return true; - } - - static void ShowNotFoundMessage(IProgressMonitor monitor) - { - if (monitor != null && monitor.CancellationToken.IsCancellationRequested) - return; - if (monitor != null) monitor.ShowingDialog = true; - MessageBox.Show(WorkbenchSingleton.MainWin32Window, - ResourceService.GetString("Dialog.NewProject.SearchReplace.SearchStringNotFound"), - ResourceService.GetString("Dialog.NewProject.SearchReplace.SearchStringNotFound.Title"), - MessageBoxButtons.OK, - MessageBoxIcon.Information); - if (monitor != null) monitor.ShowingDialog = false; - } - - static ITextEditor OpenTextArea(string fileName) - { - ITextEditorProvider textEditorProvider; - if (fileName != null) { - textEditorProvider = FileService.OpenFile(fileName) as ITextEditorProvider; - } else { - textEditorProvider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; - } - - if (textEditorProvider != null) { - return textEditorProvider.TextEditor; - } - return null; - } - - static void ClearSelection() - { - ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; - if (provider != null) { - ITextEditor editor = provider.TextEditor; - if (editor.SelectionLength > 0) - editor.Select(editor.Caret.Offset, 0); - } - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.cs deleted file mode 100644 index 296478f3ae..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.cs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; - -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Dom.Refactoring; -using ICSharpCode.SharpDevelop.Editor; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - public sealed class SearchReplaceUtilities - { - public static bool IsTextAreaSelected { - get { - return WorkbenchSingleton.Workbench.ActiveViewContent is ITextEditorProvider; - } - } - - public static ITextEditor GetActiveTextEditor() - { - ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; - if (provider != null) { - return provider.TextEditor; - } else { - return null; - } - } - - static bool IsWordPart(char c) - { - return char.IsLetterOrDigit(c) || c == '_'; - } - - public static bool IsWholeWordAt(IDocument document, int offset, int length) - { - return (offset - 1 < 0 || !IsWordPart(document.GetCharAt(offset - 1))) && - (offset + length + 1 >= document.TextLength || !IsWordPart(document.GetCharAt(offset + length))); - } - - public static ISearchStrategy CreateSearchStrategy(SearchStrategyType type) - { - switch (type) { - case SearchStrategyType.Normal: - return new BruteForceSearchStrategy(); // new KMPSearchStrategy(); - case SearchStrategyType.RegEx: - return new RegExSearchStrategy(); - case SearchStrategyType.Wildcard: - return new WildcardSearchStrategy(); - default: - throw new System.NotImplementedException("CreateSearchStrategy for type " + type); - } - } - - public static IDocumentIterator CreateDocumentIterator(DocumentIteratorType type, IProgressMonitor monitor) - { - switch (type) { - case DocumentIteratorType.CurrentDocument: - case DocumentIteratorType.CurrentSelection: - return new CurrentDocumentIterator(); - case DocumentIteratorType.Directory: - try { - if (!Directory.Exists(SearchOptions.LookIn)) { - if (monitor != null) monitor.ShowingDialog = true; - MessageService.ShowMessageFormatted("${res:Dialog.NewProject.SearchReplace.SearchStringNotFound.Title}", "${res:Dialog.NewProject.SearchReplace.LookIn.DirectoryNotFound}", FileUtility.NormalizePath(SearchOptions.LookIn)); - if (monitor != null) monitor.ShowingDialog = false; - return new DummyDocumentIterator(); - } - } catch (Exception ex) { - if (monitor != null) monitor.ShowingDialog = true; - MessageService.ShowMessage(ex.Message); - if (monitor != null) monitor.ShowingDialog = false; - return new DummyDocumentIterator(); - } - return new DirectoryDocumentIterator(SearchOptions.LookIn, - SearchOptions.LookInFiletypes, - SearchOptions.IncludeSubdirectories); - case DocumentIteratorType.AllOpenFiles: - return new AllOpenDocumentIterator(); - case DocumentIteratorType.WholeProject: - return new WholeProjectDocumentIterator(); - case DocumentIteratorType.WholeSolution: - return new WholeSolutionDocumentIterator(); - default: - throw new System.NotImplementedException("CreateDocumentIterator for type " + type); - } - } - - public static bool IsSearchable(string fileName) - { - const int BUFFER_LENGTH = 4 * 1024; - - if (!File.Exists(fileName)) - return false; - - using (var stream = File.OpenRead(fileName)) { - string mime = "text/plain"; - if (stream.Length > 0) { - stream.Position = 0; - mime = MimeTypeDetection.FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH)); - } - - return mime.StartsWith("text/", StringComparison.OrdinalIgnoreCase); - } - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BoyerMooreSearchStrategy.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BoyerMooreSearchStrategy.cs deleted file mode 100644 index cda736e349..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BoyerMooreSearchStrategy.cs +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Collections.Generic; - -namespace SearchAndReplace -{ - /// - /// This interface is the basic interface which all - /// search algorithms must implement. - /// - public class BoyerMooreSearchStrategy - { - // Shift table for chars present in the pattern - Dictionary patternCharShifts; - // Shifts for all other chars - int[] otherCharShifts; - // the patern to search - string searchPattern; - // Length of the search pattern - int patternLength; - - /// - /// Only with a call to this method the search strategy must - /// update their pattern information. This method will be called - /// before the FindNext function. - /// - public void CompilePattern() - { - searchPattern = SearchOptions.MatchCase ? SearchOptions.FindPattern : SearchOptions.FindPattern.ToUpper(); - - // Building shift table - patternLength = searchPattern.Length; - int maxShift = patternLength; - patternCharShifts = new Dictionary(); - // Constructing the table where number - // of columns is equal to PatternLength - // and number of rows is equal to the - // number of distinct chars in the pattern - for (int i = 0; i < patternLength; ++i) { - if (!patternCharShifts.ContainsKey(searchPattern[i])) { - patternCharShifts.Add(searchPattern[i], new int[patternLength]); - } - } - otherCharShifts = new int[patternLength]; - // Filling the last column of the - // table with maximum shifts (pattern length) - foreach(KeyValuePair row in patternCharShifts) { - row.Value[patternLength - 1] = maxShift; - } - otherCharShifts[patternLength - 1] = maxShift; - // Calculating other shifts (filling each column - // from PatternLength - 2 to 0 (from right to left) - for (int i = patternLength - 1; i >= 0; --i) - { - // Suffix string contains the characters - // right to the character being processsed - string suffix = new String(searchPattern.ToCharArray(), - i + 1, patternLength - i - 1); - // if Pattern begins with Suffix - // the maximum shift is equal to i + 1 - if (searchPattern.StartsWith(suffix)) { - maxShift = i + 1; - } - // Store shift for characters not present in the pattern - otherCharShifts[i] = maxShift; - // We shorten patter by one char in NewPattern. - string newPattern = new string(searchPattern.ToCharArray(), - 0, searchPattern.Length -1); - if ((newPattern.LastIndexOf(suffix) > 0) || (suffix.Length == 0)) { - foreach(KeyValuePair row in patternCharShifts) - { - string newSuffix = row.Key + suffix; - // Calculate shifts: - //Check if there are other occurences - //of the new suffix in the pattern - // If several occurences exist, we need the rightmost one - int newSuffixPos = newPattern.LastIndexOf(newSuffix); - if (newSuffixPos >= 0) { - row.Value[i] = i - newSuffixPos; - } else { - row.Value[i] = maxShift; - } - // Storing 0 if characters - // in a row and a columnt are the same - if (row.Key == searchPattern[i]) { - row.Value[i] = 0; - } - } - } else { - foreach(KeyValuePair row in patternCharShifts) - { - // if Suffix doesn't occure in NewPattern - // we simply use previous shift value - row.Value[i] = maxShift; - if (row.Key == searchPattern[i]) { - row.Value[i] = 0; - } - } - } - } - } - - int InternalFindNext(ITextIterator textIterator) - { -// while (textIterator.MoveAhead(1)) { -// if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.TextBuffer, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.TextBuffer, textIterator.Position, searchPattern)) { -// if (!SearchOptions.MatchWholeWord || IsWholeWordAt(textIterator.TextBuffer, textIterator.Position, searchPattern.Length)) { -// return textIterator.Position; -// } -// } -// } - return -1; - } - - /// - /// The find next method should search the next occurrence of the - /// compiled pattern in the text using the textIterator and options. - /// - public SearchResultMatch FindNext(ITextIterator textIterator) - { - int offset = InternalFindNext(textIterator); - return offset >= 0 ? new SearchResultMatch(offset, searchPattern.Length) : null; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BruteForceSearchStrategy.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BruteForceSearchStrategy.cs deleted file mode 100644 index 7bd15be0ef..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/BruteForceSearchStrategy.cs +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using ICSharpCode.SharpDevelop.Editor; - -namespace SearchAndReplace -{ - /// - /// Only for fallback purposes. - /// - public class BruteForceSearchStrategy : ISearchStrategy - { - string searchPattern; - - bool MatchCaseSensitive(IDocument document, int offset, string pattern) - { - for (int i = 0; i < pattern.Length; ++i) { - if (offset + i >= document.TextLength || document.GetCharAt(offset + i) != pattern[i]) { - return false; - } - } - return true; - } - - bool MatchCaseInsensitive(IDocument document, int offset, string pattern) - { - for (int i = 0; i < pattern.Length; ++i) { - if (offset + i >= document.TextLength || Char.ToUpper(document.GetCharAt(offset + i)) != pattern[i]) { - return false; - } - } - return true; - } - - int InternalFindNext(ITextIterator textIterator) - { - while (textIterator.MoveAhead(1)) { - if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.Document, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.Document, textIterator.Position, searchPattern)) { - if (!SearchOptions.MatchWholeWord || SearchReplaceUtilities.IsWholeWordAt(textIterator.Document, textIterator.Position, searchPattern.Length)) { - return textIterator.Position; - } - } - } - return -1; - } - - int InternalFindNext(ITextIterator textIterator, int offset, int length) - { - while (textIterator.MoveAhead(1)) { - if (textIterator.Position >= offset + length) { - textIterator.Position = offset; - } - if (SearchOptions.MatchCase ? MatchCaseSensitive(textIterator.Document, textIterator.Position, searchPattern) : MatchCaseInsensitive(textIterator.Document, textIterator.Position, searchPattern)) { - if (!SearchOptions.MatchWholeWord || SearchReplaceUtilities.IsWholeWordAt(textIterator.Document, textIterator.Position, searchPattern.Length)) { - if (TextSelection.IsInsideRange(textIterator.Position + searchPattern.Length - 1, offset, length)) { - return textIterator.Position; - } else { - return -1; - } - } - } - } - return -1; - } - - public bool CompilePattern(ICSharpCode.SharpDevelop.Gui.IProgressMonitor monitor) - { - searchPattern = SearchOptions.MatchCase ? SearchOptions.FindPattern : SearchOptions.FindPattern.ToUpper(); - return true; - } - - public SearchResultMatch FindNext(ITextIterator textIterator) - { - int offset = InternalFindNext(textIterator); - return GetSearchResult(offset); - } - - public SearchResultMatch FindNext(ITextIterator textIterator, int offset, int length) - { - int foundOffset = InternalFindNext(textIterator, offset, length); - return GetSearchResult(foundOffset); - } - - SearchResultMatch GetSearchResult(int offset) - { - return offset >= 0 ? new SearchResultMatch(offset, searchPattern.Length) : null; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/ISearchStrategy.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/ISearchStrategy.cs deleted file mode 100644 index 9eb289a09b..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/ISearchStrategy.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - /// - /// This interface is the basic interface which all - /// search algorithms must implement. - /// - public interface ISearchStrategy - { - /// - /// Only with a call to this method the search strategy must - /// update their pattern information. This method will be called - /// before the FindNext function. - /// The method might show a message box to the user if the pattern is invalid. - /// - bool CompilePattern(IProgressMonitor monitor); - - /// - /// The find next method should search the next occurrence of the - /// compiled pattern in the text using the textIterator and options. - /// - SearchResultMatch FindNext(ITextIterator textIterator); - - /// - /// Find only in the specified range. - /// - SearchResultMatch FindNext(ITextIterator textIterator, int offset, int length); - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/KMPSearchStrategy.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/KMPSearchStrategy.cs deleted file mode 100644 index d51ea5289d..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/KMPSearchStrategy.cs +++ /dev/null @@ -1,78 +0,0 @@ -//// -//// -//// -//// -//// -//// -// -//using System; -// -//namespace SearchAndReplace -//{ -// /// -// /// Implements the Knuth, Morris, Pratt searching algorithm. -// /// -// public class KMPSearchStrategy : ISearchStrategy -// { -// string searchPattern; -// int[] overlap; -// -// public void CompilePattern() -// { -// if (searchPattern != (options.IgnoreCase ? options.SearchPattern.ToUpper() : options.SearchPattern)) { -// searchPattern = options.IgnoreCase ? options.SearchPattern.ToUpper() : options.SearchPattern; -// overlap = new int[searchPattern.Length + 1]; -// Preprocessing(); -// } -// } -// -// void Preprocessing() -// { -// overlap[0] = -1; -// for (int i = 0, j = -1; i < searchPattern.Length;) { -// while (j >= 0 && searchPattern[i] != searchPattern[j]) { -// j = overlap[j]; -// } -// ++i; -// ++j; -// overlap[i] = j; -// } -// } -// -// int InternalFindNext(ITextIterator textIterator) -// { -// int j = 0; -// if (!textIterator.MoveAhead(1)) { -// return -1; -// } -// while (true) { // until pattern found or Iterator finished -// while (j >= 0 && searchPattern[j] != (options.IgnoreCase ? Char.ToUpper(textIterator.GetCharRelative(j)) : textIterator.GetCharRelative(j))) { -// if (!textIterator.MoveAhead(j - overlap[j])) { -// return -1; -// } -// j = overlap[j]; -// } -// if (++j >= searchPattern.Length) { -// if ((!options.SearchWholeWordOnly || SearchReplaceUtilities.IsWholeWordAt(textIterator.TextBuffer, textIterator.Position, searchPattern.Length))) { -// return textIterator.Position; -// } -// if (!textIterator.MoveAhead(j - overlap[j])) { -// return -1; -// } -// j = overlap[j]; -// } -// } -// } -// -// public SearchResult FindNext(ITextIterator textIterator) -// { -// int offset = InternalFindNext(textIterator, options); -// -// if (offset + searchPattern.Length >= textIterator.TextBuffer.Length) { -// return FindNext(textIterator, options); -// } -// -// return offset >= 0 ? new SearchResult(offset, searchPattern.Length) : null; -// } -// } -//} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/RegExSearchStrategy.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/RegExSearchStrategy.cs deleted file mode 100644 index 7296f2cea3..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/RegExSearchStrategy.cs +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Text.RegularExpressions; -using ICSharpCode.Core; -using ICSharpCode.SharpDevelop.Gui; - -namespace SearchAndReplace -{ - public class RegExSearchStrategy : ISearchStrategy - { - Regex regex = null; - - public bool CompilePattern(IProgressMonitor monitor) - { - RegexOptions regexOptions = RegexOptions.Compiled | RegexOptions.Multiline; - if (!SearchOptions.MatchCase) { - regexOptions |= RegexOptions.IgnoreCase; - } - try { - regex = new Regex(SearchOptions.FindPattern, regexOptions); - return true; - } catch (ArgumentException ex) { - if (monitor != null) monitor.ShowingDialog = true; - MessageService.ShowError("${res:Dialog.NewProject.SearchReplace.ErrorParsingRegex}\n" + ex.Message); - if (monitor != null) monitor.ShowingDialog = false; - return false; - } - } - - public SearchResultMatch FindNext(ITextIterator textIterator) - { - string document = textIterator.Document.Text; - - while (textIterator.MoveAhead(1)) { - Match m = regex.Match(document, textIterator.Position); - if (m == null || !m.Success) { - while (textIterator.Position < document.Length - 1) { - if (!textIterator.MoveAhead(1)) - return null; - } - } else { - int delta = m.Index - textIterator.Position; - if (delta <= 0 || textIterator.MoveAhead(delta)) { - return new RegexSearchResult(m); - } else { - return null; - } - } - } - - return null; - } - - public SearchResultMatch FindNext(ITextIterator textIterator, int offset, int length) - { - string document = textIterator.Document.GetText(0, textIterator.Document.TextLength); - - while (textIterator.MoveAhead(1)) { - if (textIterator.Position >= offset + length) { - textIterator.Position = offset; - } - Match m = regex.Match(document, textIterator.Position); - if (m == null || !m.Success) { - while (textIterator.Position < document.Length - 1) { - if (!textIterator.MoveAhead(1)) - return null; - } - } else { - int delta = m.Index - textIterator.Position; - if (delta <= 0 || textIterator.MoveAhead(delta)) { - if (TextSelection.IsInsideRange(m.Index + m.Length - 1, offset, length)) { - return new RegexSearchResult(m); - } else { - return null; - } - } else { - return null; - } - } - } - - return null; - } - - private sealed class RegexSearchResult : SearchResultMatch - { - Match m; - - internal RegexSearchResult(Match m) : base(m.Index, m.Length) - { - this.m = m; - } - - public override string TransformReplacePattern(string pattern) - { - return m.Result(pattern); - } - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/WildcardSearchStrategy.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/WildcardSearchStrategy.cs deleted file mode 100644 index 35516d1ea9..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchStrategy/WildcardSearchStrategy.cs +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Collections; -using ICSharpCode.SharpDevelop.Editor; - -namespace SearchAndReplace -{ - /// - /// Implements a wildcard search strategy. - /// - /// Wildcard search has following pattern code : - /// * = Zero or more of any character - /// ? = Any single character - /// # = Any single digit - /// [...] = Any one character in the set - /// [!...] = Any one character not in the set - /// - public class WildcardSearchStrategy : ISearchStrategy - { - enum CommandType { - Match, - AnyZeroOrMore, - AnySingle, - AnyDigit, - AnyInList, - NoneInList - } - - class Command { - public CommandType CommandType = CommandType.Match; - public char SingleChar = '\0'; - public string CharList = String.Empty; - } - - ArrayList patternProgram = null; - int curMatchEndOffset = -1; - - void CompilePattern(string pattern, bool ignoreCase) - { - patternProgram = new ArrayList(); - for (int i = 0; i < pattern.Length; ++i) { - Command newCommand = new Command(); - switch (pattern[i]) { - case '#': - newCommand.CommandType = CommandType.AnyDigit; - break; - case '*': - newCommand.CommandType = CommandType.AnyZeroOrMore; - break; - case '?': - newCommand.CommandType = CommandType.AnySingle; - break; - case '[': - int index = pattern.IndexOf(']', i); - if (index > 0) { - newCommand.CommandType = CommandType.AnyInList; - string list = pattern.Substring(i + 1, index - i - 1); - if (list[0] == '!') { - newCommand.CommandType = CommandType.NoneInList; - list = list.Substring(1); - } - newCommand.CharList = ignoreCase ? list.ToUpper() : list; - i = index; - } else { - goto default; - } - break; - default: - newCommand.CommandType = CommandType.Match; - newCommand.SingleChar = ignoreCase ? Char.ToUpper(pattern[i]) : pattern[i]; - break; - } - patternProgram.Add(newCommand); - } - } - - bool Match(IDocument document, - int offset, - bool ignoreCase, - int programStart) - { - int curOffset = offset; - curMatchEndOffset = -1; - - for (int pc = programStart; pc < patternProgram.Count; ++pc) { - if (curOffset >= document.TextLength) { - return false; - } - - char ch = ignoreCase ? Char.ToUpper(document.GetCharAt(curOffset)) : document.GetCharAt(curOffset); - Command cmd = (Command)patternProgram[pc]; - - switch (cmd.CommandType) { - case CommandType.Match: - if (ch != cmd.SingleChar) { - return false; - } - break; - case CommandType.AnyZeroOrMore: - if (ch == '\n') { - return false; - } - return Match(document, curOffset, ignoreCase, pc + 1) || - Match(document, curOffset + 1, ignoreCase, pc); - case CommandType.AnySingle: - break; - case CommandType.AnyDigit: - if (!Char.IsDigit(ch) && ch != '#') { - return false; - } - break; - case CommandType.AnyInList: - if (cmd.CharList.IndexOf(ch) < 0) { - return false; - } - break; - case CommandType.NoneInList: - if (cmd.CharList.IndexOf(ch) >= 0) { - return false; - } - break; - } - ++curOffset; - } - curMatchEndOffset = curOffset; - return true; - } - - int InternalFindNext(ITextIterator textIterator) - { - while (textIterator.MoveAhead(1)) { - int position = textIterator.Position; - if (Match(textIterator.Document, position, !SearchOptions.MatchCase, 0)) { - if (!SearchOptions.MatchWholeWord || SearchReplaceUtilities.IsWholeWordAt(textIterator.Document, position, curMatchEndOffset - position)) { - textIterator.MoveAhead(curMatchEndOffset - position - 1); - return position; - } - } - } - return -1; - } - - int InternalFindNext(ITextIterator textIterator, int offset, int length) - { - while (textIterator.MoveAhead(1)) { - if (textIterator.Position >= offset + length) { - textIterator.Position = offset; - } - int position = textIterator.Position; - if (Match(textIterator.Document, position, !SearchOptions.MatchCase, 0)) { - if (!SearchOptions.MatchWholeWord || SearchReplaceUtilities.IsWholeWordAt(textIterator.Document, position, curMatchEndOffset - position)) { - if (TextSelection.IsInsideRange(curMatchEndOffset - 1, offset, length)) { - textIterator.MoveAhead(curMatchEndOffset - position - 1); - return position; - } else { - return -1; - } - } - } - } - return -1; - } - - public bool CompilePattern(ICSharpCode.SharpDevelop.Gui.IProgressMonitor monitor) - { - CompilePattern(SearchOptions.FindPattern, !SearchOptions.MatchCase); - return true; - } - - public SearchResultMatch FindNext(ITextIterator textIterator) - { - int offset = InternalFindNext(textIterator); - return GetSearchResult(offset); - } - - public SearchResultMatch FindNext(ITextIterator textIterator, int offset, int length) - { - int foundOffset = InternalFindNext(textIterator, offset, length); - return GetSearchResult(foundOffset); - } - - SearchResultMatch GetSearchResult(int offset) - { - return offset >= 0 ? new SearchResultMatch(offset, curMatchEndOffset - offset) : null; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIterator.cs deleted file mode 100644 index 969e916dd2..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIterator.cs +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Diagnostics; -using ICSharpCode.SharpDevelop.Dom.Refactoring; -using ICSharpCode.SharpDevelop.Editor; - -namespace SearchAndReplace -{ - public class ForwardTextIterator : ITextIterator - { - ProvidedDocumentInformation info; - - enum TextIteratorState { - Resetted, - Iterating, - Done, - } - - TextIteratorState state; - - IDocument document; - int endOffset; - int oldOffset = -1; - - public IDocument Document { - get { - return document; - } - } - - public char Current { - get { - switch (state) { - case TextIteratorState.Resetted: - throw new System.InvalidOperationException("Call moveAhead first"); - case TextIteratorState.Iterating: - return document.GetCharAt(Position); - case TextIteratorState.Done: - throw new System.InvalidOperationException("TextIterator is at the end"); - default: - throw new System.InvalidOperationException("unknown text iterator state"); - } - } - } - - int position; - public int Position { - get { - return position; - } - set { - position = value; - } - } - - public ForwardTextIterator(ProvidedDocumentInformation info) - { - if (info == null) - throw new ArgumentNullException("info"); - - this.info = info; - this.document = info.Document; - this.position = info.CurrentOffset; - this.endOffset = info.EndOffset; - - Reset(); - } - - public char GetCharRelative(int offset) - { - if (state != TextIteratorState.Iterating) { - throw new System.InvalidOperationException(); - } - - int realOffset = (Position + (1 + Math.Abs(offset) / document.TextLength) * document.TextLength + offset) % document.TextLength; - return document.GetCharAt(realOffset); - } - - public bool MoveAhead(int numChars) - { - Debug.Assert(numChars > 0); - - // HACK : ignore files with length 1 (fixes SD-1815) - if (document.TextLength == 1) { - state = TextIteratorState.Done; - return false; - } - - switch (state) { - case TextIteratorState.Resetted: - if (document.TextLength == 0) { - state = TextIteratorState.Done; - return false; - } - Position = endOffset; - state = TextIteratorState.Iterating; - return true; - case TextIteratorState.Done: - return false; - case TextIteratorState.Iterating: - if (oldOffset == -1 && document.TextLength == endOffset) { - // HACK: Take off one if the iterator start - // position is at the end of the text. - Position--; - } - - if (oldOffset != -1 && Position == endOffset - 1 && document.TextLength == endOffset) { - state = TextIteratorState.Done; - return false; - } - - Position = (Position + numChars) % document.TextLength; - bool finish = oldOffset != -1 && (oldOffset > Position || oldOffset < endOffset) && Position >= endOffset; - - // HACK: Iterating is complete if Position == endOffset - 1 - // when the iterator start position was initially at the - // end of the text. - if (oldOffset != -1 && oldOffset == endOffset - 1 && document.TextLength == endOffset) { - finish = true; - } - - oldOffset = Position; - if (finish) { - state = TextIteratorState.Done; - return false; - } - return true; - default: - throw new Exception("Unknown text iterator state"); - } - } - - public void InformReplace(int offset, int length, int newLength) - { - if (offset <= endOffset) { - endOffset = endOffset - length + newLength; - } - - if (offset <= Position) { - Position = Position - length + newLength; - } - - if (offset <= oldOffset) { - oldOffset = oldOffset - length + newLength; - } - } - - public void Reset() - { - state = TextIteratorState.Resetted; - Position = endOffset; - oldOffset = -1; - } - - public override string ToString() - { - return String.Format("[ForwardTextIterator: Position={0}, endOffset={1}, state={2}]", Position, endOffset, state); - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIteratorBuilder.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIteratorBuilder.cs deleted file mode 100644 index c88fa241b8..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ForwardTextIteratorBuilder.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor.Search; -using System; -using System.Diagnostics; - -namespace SearchAndReplace -{ - public class ForwardTextIteratorBuilder : ITextIteratorBuilder - { - public ITextIterator BuildTextIterator(ProvidedDocumentInformation info) - { - Debug.Assert(info != null); - return new ForwardTextIterator(info); - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ITextIterator.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ITextIterator.cs deleted file mode 100644 index 206afe4345..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextIterator/ITextIterator.cs +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using ICSharpCode.SharpDevelop.Editor; -using System; -using ICSharpCode.SharpDevelop.Dom.Refactoring; - -namespace SearchAndReplace -{ - /// - /// This iterator iterates on a text buffer strategy. - /// - public interface ITextIterator - { - /// - /// The text buffer strategy - /// - IDocument Document { - get; - } - - /// - /// Gets the current char this is the same as - /// GetCharRelative(0) - /// - /// - /// If this method is called before the first MoveAhead or after - /// MoveAhead or after MoveAhead returns false. - /// - char Current { - get; - } - - /// - /// The current position=offset of the text iterator cursor - /// - int Position { - get; - set; - } - - /// - /// Gets a char relative to the current position (negative values - /// will work too). - /// - /// - /// If this method is called before the first MoveAhead or after - /// MoveAhead or after MoveAhead returns false. - /// - char GetCharRelative(int offset); - - /// - /// Moves the iterator position numChars - /// - bool MoveAhead(int numChars); - - /// - /// Rests the iterator - /// - void Reset(); - - /// - /// The find object calls the InformReplace method to inform the text iterator - /// about the replace operation on the TextBuffer. The text iterator must update - /// all internal offsets to the new offsets (if neccessary) - /// - void InformReplace(int offset, int length, int newLength); - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextSelection.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextSelection.cs deleted file mode 100644 index 1d223174f9..0000000000 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/TextSelection.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) - -using System; - -namespace SearchAndReplace -{ - public class TextSelection - { - int offset; - int length; - - public TextSelection(int offset, int length) - { - this.offset = offset; - this.length = length; - } - - public int Length { - get { - return length; - } - set { - length = value; - } - } - - public int Offset { - get { - return offset; - } - set { - offset = value; - } - } - - /// - /// Checks whether a position is in a specified range. - /// - public static bool IsInsideRange(int position, int offset, int length) - { - return position >= offset && position < offset + length; - } - } -} diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs b/src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs index 8358c5b2a9..a268ff7bc5 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs +++ b/src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Windows; using System.Windows.Controls; +using ICSharpCode.Core; using ICSharpCode.Core.Presentation; using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Editor.Search; @@ -118,5 +119,70 @@ namespace SearchAndReplace { return new DefaultSearchResult(title, matches); } + + public ISearchResult CreateSearchResult(string title, IObservable matches) + { + var osr = new ObserverSearchResult(title); + osr.Registration = matches.Subscribe(osr); + return osr; + } + } + + public class ObserverSearchResult : ISearchResult, IObserver + { + List matches; + Button stopButton; + SearchRootNode rootNode; + + public ObserverSearchResult(string title) + { + Text = title; + matches = new List(); + rootNode = new SearchRootNode(title, matches); + } + + public string Text { get; private set; } + public IDisposable Registration { get; set; } + + static ResultsTreeView resultsTreeViewInstance; + + public object GetControl() + { + WorkbenchSingleton.AssertMainThread(); + if (resultsTreeViewInstance == null) + resultsTreeViewInstance = new ResultsTreeView(); + rootNode.GroupResultsByFile(ResultsTreeView.GroupResultsByFile); + resultsTreeViewInstance.ItemsSource = new object[] { rootNode }; + return resultsTreeViewInstance; + } + + public IList GetToolbarItems() + { + stopButton = new Button { Content = "Stop" }; + stopButton.Click += delegate { if (Registration != null) Registration.Dispose(); }; + + return new ArrayList { stopButton }; + } + + void IObserver.OnNext(SearchResultMatch value) + { + matches.Add(value); + } + + void IObserver.OnError(Exception error) + { + MessageService.ShowException(error); + OnCompleted(); + } + + void OnCompleted() + { + stopButton.Visibility = Visibility.Collapsed; + } + + void IObserver.OnCompleted() + { + OnCompleted(); + } } } diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs b/src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs index 4a5de02c32..c87c6d9d96 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs +++ b/src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchAndReplacePanel.cs @@ -1,12 +1,14 @@ // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) +using ICSharpCode.AvalonEdit.Search; using ICSharpCode.SharpDevelop.Editor; using System; using System.Windows.Forms; using ICSharpCode.Core; using ICSharpCode.Core.WinForms; using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.Editor.Search; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Gui.XmlForms; @@ -84,9 +86,9 @@ namespace SearchAndReplace base.Dispose(disposing); } - public DocumentIteratorType DocumentIteratorType { + public SearchTarget SearchTarget { get { - return (DocumentIteratorType)(Get("lookIn").SelectedIndex); + return (SearchTarget)(Get("lookIn").SelectedIndex); } set { Get("lookIn").SelectedIndex = (int)value; @@ -115,7 +117,7 @@ namespace SearchAndReplace using (AsynchronousWaitDialog monitor = AsynchronousWaitDialog.ShowWaitDialog("Search", true)) { monitor.Progress = double.NaN; // progress not implemented, use indeterminate progress - SearchReplaceManager.FindNext(monitor); +// SearchReplaceManager.FindNext(monitor); } } Focus(); @@ -129,11 +131,8 @@ namespace SearchAndReplace RunAllInSelection(0); } } else { - using (AsynchronousWaitDialog monitor = AsynchronousWaitDialog.ShowWaitDialog("Search", true)) - { - monitor.Progress = double.NaN; // progress not implemented, use indeterminate progress - SearchInFilesManager.FindAll(monitor); - } + var results = SearchManager.FindAll(SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchStrategyType, SearchOptions.DocumentIteratorType, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories); + SearchResultsPad.Instance.ShowSearchResults("Search", results); } } @@ -148,7 +147,7 @@ namespace SearchAndReplace using (AsynchronousWaitDialog monitor = AsynchronousWaitDialog.ShowWaitDialog("Search", true)) { monitor.Progress = double.NaN; // progress not implemented, use indeterminate progress - SearchReplaceManager.MarkAll(monitor); +// SearchReplaceManager.MarkAll(monitor); } } } @@ -164,7 +163,7 @@ namespace SearchAndReplace using (AsynchronousWaitDialog monitor = AsynchronousWaitDialog.ShowWaitDialog("Search", true)) { monitor.Progress = double.NaN; // progress not implemented, use indeterminate progress - SearchReplaceManager.ReplaceAll(monitor); +// SearchReplaceManager.ReplaceAll(monitor); } } } @@ -180,7 +179,7 @@ namespace SearchAndReplace using (AsynchronousWaitDialog monitor = AsynchronousWaitDialog.ShowWaitDialog("Search", true)) { monitor.Progress = double.NaN; // progress not implemented, use indeterminate progress - SearchReplaceManager.Replace(monitor); +// SearchReplaceManager.Replace(monitor); } } Focus(); @@ -202,11 +201,11 @@ namespace SearchAndReplace SearchOptions.MatchWholeWord = Get("matchWholeWord").Checked; SearchOptions.IncludeSubdirectories = Get("includeSubFolder").Checked; - SearchOptions.SearchStrategyType = (SearchStrategyType)Get("use").SelectedIndex; + SearchOptions.SearchStrategyType = (SearchMode)Get("use").SelectedIndex; if (Get("lookIn").DropDownStyle == ComboBoxStyle.DropDown) { - SearchOptions.DocumentIteratorType = DocumentIteratorType.Directory; + SearchOptions.DocumentIteratorType = SearchTarget.Directory; } else { - SearchOptions.DocumentIteratorType = (DocumentIteratorType)Get("lookIn").SelectedIndex; + SearchOptions.DocumentIteratorType = (SearchTarget)Get("lookIn").SelectedIndex; } } @@ -246,14 +245,14 @@ namespace SearchAndReplace Get("lookIn").Items.Add(SearchOptions.LookIn); Get("lookIn").SelectedIndexChanged += new EventHandler(LookInSelectedIndexChanged); - if (IsMultipleLineSelection(SearchReplaceUtilities.GetActiveTextEditor())) { - DocumentIteratorType = DocumentIteratorType.CurrentSelection; - } else { - if (SearchOptions.DocumentIteratorType == DocumentIteratorType.CurrentSelection) { - SearchOptions.DocumentIteratorType = DocumentIteratorType.CurrentDocument; - } - DocumentIteratorType = SearchOptions.DocumentIteratorType; - } +// if (IsMultipleLineSelection(SearchReplaceUtilities.GetActiveTextEditor())) { +// SearchTarget = SearchTarget.CurrentSelection; +// } else { +// if (SearchOptions.DocumentIteratorType == SearchTarget.CurrentSelection) { +// SearchOptions.DocumentIteratorType = SearchTarget.CurrentDocument; +// } +// SearchTarget = SearchOptions.DocumentIteratorType; +// } Get("fileTypes").Text = SearchOptions.LookInFiletypes; Get("matchCase").Checked = SearchOptions.MatchCase; @@ -265,10 +264,10 @@ namespace SearchAndReplace Get("use").Items.Add(StringParser.Parse("${res:Dialog.NewProject.SearchReplace.SearchStrategy.RegexSearch}")); Get("use").Items.Add(StringParser.Parse("${res:Dialog.NewProject.SearchReplace.SearchStrategy.WildcardSearch}")); switch (SearchOptions.SearchStrategyType) { - case SearchStrategyType.RegEx: + case SearchMode.RegEx: Get("use").SelectedIndex = 1; break; - case SearchStrategyType.Wildcard: + case SearchMode.Wildcard: Get("use").SelectedIndex = 2; break; default: @@ -299,7 +298,7 @@ namespace SearchAndReplace bool IsSelectionSearch { get { - return DocumentIteratorType == DocumentIteratorType.CurrentSelection; + return SearchTarget == SearchTarget.CurrentSelection; } } @@ -327,9 +326,9 @@ namespace SearchAndReplace ignoreSelectionChanges = true; if (findFirst) { findFirst = false; - SearchReplaceManager.FindFirstInSelection(startOffset, endOffset - startOffset, null); +// SearchReplaceManager.FindFirstInSelection(startOffset, endOffset - startOffset, null); } else { - findFirst = !SearchReplaceManager.FindNextInSelection(null); +// findFirst = !SearchReplaceManager.FindNextInSelection(null); if (findFirst) { textEditor.Select(startOffset, endOffset - startOffset); } @@ -344,20 +343,20 @@ namespace SearchAndReplace /// static StoredSelection GetCurrentTextSelection() { - ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor(); - if (textArea != null) { - return new StoredSelection(textArea.SelectionStart, textArea.SelectionLength); - } +// ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor(); +// if (textArea != null) { +// return new StoredSelection(textArea.SelectionStart, textArea.SelectionLength); +// } return null; } void WorkbenchActiveViewContentChanged(object source, EventArgs e) { - ITextEditor activeTextEditorControl = SearchReplaceUtilities.GetActiveTextEditor(); - if (activeTextEditorControl != this.textEditor) { - AddSelectionChangedHandler(activeTextEditorControl); - TextSelectionChanged(source, e); - } +// ITextEditor activeTextEditorControl = SearchReplaceUtilities.GetActiveTextEditor(); +// if (activeTextEditorControl != this.textEditor) { +// AddSelectionChangedHandler(activeTextEditorControl); +// TextSelectionChanged(source, e); +// } } void AddSelectionChangedHandler(ITextEditor textEditor) @@ -402,7 +401,7 @@ namespace SearchAndReplace { findFirst = true; selection = GetCurrentTextSelection(); - AddSelectionChangedHandler(SearchReplaceUtilities.GetActiveTextEditor()); +// AddSelectionChangedHandler(SearchReplaceUtilities.GetActiveTextEditor()); WorkbenchSingleton.Workbench.ActiveViewContentChanged += WorkbenchActiveViewContentChanged; } @@ -424,22 +423,22 @@ namespace SearchAndReplace textEditor.Select(startOffset, endOffset - startOffset); - try { - ignoreSelectionChanges = true; - if (action == 0) { - SearchInFilesManager.FindAll(startOffset, endOffset - startOffset, monitor); - } else if (action == 1) { - SearchReplaceManager.MarkAll(startOffset, endOffset - startOffset, monitor); - } else if (action == 2) { - // use anchor for endOffset because the replacement might change the text length - var anchor = textEditor.Document.CreateAnchor(endOffset); - SearchReplaceManager.ReplaceAll(startOffset, endOffset - startOffset, monitor); - endOffset = anchor.Offset; - } - textEditor.Select(startOffset, endOffset - startOffset); - } finally { - ignoreSelectionChanges = false; - } +// try { +// ignoreSelectionChanges = true; +// if (action == 0) { +// SearchInFilesManager.FindAll(startOffset, endOffset - startOffset, monitor); +// } else if (action == 1) { +// SearchReplaceManager.MarkAll(startOffset, endOffset - startOffset, monitor); +// } else if (action == 2) { +// // use anchor for endOffset because the replacement might change the text length +// var anchor = textEditor.Document.CreateAnchor(endOffset); +// SearchReplaceManager.ReplaceAll(startOffset, endOffset - startOffset, monitor); +// endOffset = anchor.Offset; +// } +// textEditor.Select(startOffset, endOffset - startOffset); +// } finally { +// ignoreSelectionChanges = false; +// } } void ReplaceInSelection() @@ -451,20 +450,20 @@ namespace SearchAndReplace textEditor.Caret.Offset = startOffset; } - try { - ignoreSelectionChanges = true; - if (findFirst) { - findFirst = false; - SearchReplaceManager.ReplaceFirstInSelection(startOffset, endOffset - startOffset, null); - } else { - findFirst = !SearchReplaceManager.ReplaceNextInSelection(null); - if (findFirst) { - textEditor.Select(startOffset, endOffset - startOffset); - } - } - } finally { - ignoreSelectionChanges = false; - } +// try { +// ignoreSelectionChanges = true; +// if (findFirst) { +// findFirst = false; +// SearchReplaceManager.ReplaceFirstInSelection(startOffset, endOffset - startOffset, null); +// } else { +// findFirst = !SearchReplaceManager.ReplaceNextInSelection(null); +// if (findFirst) { +// textEditor.Select(startOffset, endOffset - startOffset); +// } +// } +// } finally { +// ignoreSelectionChanges = false; +// } } /// diff --git a/src/AddIns/Misc/SearchAndReplace/Project/SearchAndReplace.csproj b/src/AddIns/Misc/SearchAndReplace/Project/SearchAndReplace.csproj index 541c0ba4fb..14c5ff1174 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/SearchAndReplace.csproj +++ b/src/AddIns/Misc/SearchAndReplace/Project/SearchAndReplace.csproj @@ -69,27 +69,8 @@ - - - - - - - - - - - - - - - - - - - - - + + Code @@ -106,9 +87,6 @@ - - - diff --git a/src/AddIns/Misc/SearchAndReplace/Project/SearchOptions.cs b/src/AddIns/Misc/SearchAndReplace/Project/SearchOptions.cs index f3ef64664b..52a7aa5caf 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/SearchOptions.cs +++ b/src/AddIns/Misc/SearchAndReplace/Project/SearchOptions.cs @@ -2,16 +2,11 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using ICSharpCode.AvalonEdit.Search; using ICSharpCode.Core; namespace SearchAndReplace { - public enum SearchStrategyType { - Normal, - RegEx, - Wildcard - } - public static class SearchOptions { const string searchPropertyKey = "SearchAndReplaceProperties"; @@ -141,20 +136,20 @@ namespace SearchAndReplace } } - public static DocumentIteratorType DocumentIteratorType { + public static SearchTarget DocumentIteratorType { get { - return properties.Get("DocumentIteratorType", DocumentIteratorType.CurrentDocument); + return properties.Get("DocumentIteratorType", SearchTarget.CurrentDocument); } set { - if (!Enum.IsDefined(typeof(DocumentIteratorType), value)) + if (!Enum.IsDefined(typeof(SearchTarget), value)) throw new ArgumentException("invalid enum value"); properties.Set("DocumentIteratorType", value); } } - public static SearchStrategyType SearchStrategyType { + public static SearchMode SearchStrategyType { get { - return properties.Get("SearchStrategyType", SearchStrategyType.Normal); + return properties.Get("SearchStrategyType", SearchMode.Normal); } set { properties.Set("SearchStrategyType", value); diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/ISearchStrategy.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/ISearchStrategy.cs index 9930908dc1..b6c821eee0 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/ISearchStrategy.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/ISearchStrategy.cs @@ -18,7 +18,7 @@ namespace ICSharpCode.AvalonEdit.Search /// Finds all matches for a predicate in the given ITextSource. /// /// This method is thread-safe. - IEnumerable FindAll(ITextSource document); + IEnumerable FindAll(ITextSource document, ISegment selection = null); } /// @@ -32,7 +32,7 @@ namespace ICSharpCode.AvalonEdit.Search /// /// Defines supported search modes. /// - public enum SearchType + public enum SearchMode { /// /// Standard search diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/RegexSearchStrategy.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/RegexSearchStrategy.cs index a5ffa334b2..3b6484f7f7 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/RegexSearchStrategy.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/RegexSearchStrategy.cs @@ -18,10 +18,11 @@ namespace ICSharpCode.AvalonEdit.Search this.searchPattern = searchPattern; } - public IEnumerable FindAll(ITextSource document) + public IEnumerable FindAll(ITextSource document, ISegment selection = null) { foreach (Match result in searchPattern.Matches(document.Text)) { - yield return new SearchResult { StartOffset = result.Index, Length = result.Length }; + if (selection == null || (selection.Offset <= result.Index && selection.EndOffset >= (result.Index + result.Length))) + yield return new SearchResult { StartOffset = result.Index, Length = result.Length }; } } } diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml.cs index d0a96b2af3..62d02ba1c2 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml.cs @@ -52,7 +52,7 @@ namespace ICSharpCode.AvalonEdit.Search void UpdateSearch() { messageView.IsOpen = false; - strategy = SearchStrategyFactory.Create(searchPattern ?? "", !MatchCase, WholeWords, UseRegex ? SearchType.RegEx : SearchType.Normal); + strategy = SearchStrategyFactory.Create(searchPattern ?? "", !MatchCase, WholeWords, UseRegex ? SearchMode.RegEx : SearchMode.Normal); DoSearch(true); } diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchStrategyFactory.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchStrategyFactory.cs index 36c0540162..25c4661a62 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchStrategyFactory.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchStrategyFactory.cs @@ -5,6 +5,7 @@ using System; using System.Text; using System.Text.RegularExpressions; using System.Windows.Controls; +using ICSharpCode.AvalonEdit.Document; namespace ICSharpCode.AvalonEdit.Search { @@ -16,7 +17,7 @@ namespace ICSharpCode.AvalonEdit.Search /// /// Creates a default ISearchStrategy with the given parameters. /// - public static ISearchStrategy Create(string searchPattern, bool ignoreCase, bool matchWholeWords, SearchType searchType) + public static ISearchStrategy Create(string searchPattern, bool ignoreCase, bool matchWholeWords, SearchMode mode) { if (searchPattern == null) throw new ArgumentNullException("searchPattern"); @@ -24,11 +25,11 @@ namespace ICSharpCode.AvalonEdit.Search if (ignoreCase) options |= RegexOptions.IgnoreCase; - switch (searchType) { - case SearchType.Normal: + switch (mode) { + case SearchMode.Normal: searchPattern = Regex.Escape(searchPattern); break; - case SearchType.Wildcard: + case SearchMode.Wildcard: searchPattern = ConvertWildcardsToRegex(searchPattern); break; } diff --git a/src/Main/Base/Project/Src/Editor/Search/ISearchResultFactory.cs b/src/Main/Base/Project/Src/Editor/Search/ISearchResultFactory.cs index cb1762d138..9091780ab2 100644 --- a/src/Main/Base/Project/Src/Editor/Search/ISearchResultFactory.cs +++ b/src/Main/Base/Project/Src/Editor/Search/ISearchResultFactory.cs @@ -12,5 +12,7 @@ namespace ICSharpCode.SharpDevelop.Editor.Search public interface ISearchResultFactory { ISearchResult CreateSearchResult(string title, IEnumerable matches); + + ISearchResult CreateSearchResult(string title, IObservable matches); } } diff --git a/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs b/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs index f78b1748e8..53eb04f883 100644 --- a/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs +++ b/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs @@ -104,6 +104,11 @@ namespace ICSharpCode.SharpDevelop.Editor.Search ShowSearchResults(CreateSearchResult(title, matches)); } + public void ShowSearchResults(string title, IObservable matches) + { + ShowSearchResults(CreateSearchResult(title, matches)); + } + public event EventHandler SearchResultsShown; public static ISearchResult CreateSearchResult(string title, IEnumerable matches) @@ -120,6 +125,21 @@ namespace ICSharpCode.SharpDevelop.Editor.Search return new DummySearchResult { Text = title }; } + + public static ISearchResult CreateSearchResult(string title, IObservable matches) + { + if (title == null) + throw new ArgumentNullException("title"); + if (matches == null) + throw new ArgumentNullException("matches"); + foreach (ISearchResultFactory factory in AddInTree.BuildItems("/SharpDevelop/Pads/SearchResultPad/Factories", null, false)) { + ISearchResult result = factory.CreateSearchResult(title, matches); + if (result != null) + return result; + } + return new DummySearchResult { Text = title }; + } + sealed class DummySearchResult : ISearchResult { public string Text { diff --git a/src/Main/Base/Project/Src/Services/MimeTypeDetection.cs b/src/Main/Base/Project/Src/Services/MimeTypeDetection.cs index 36413253f6..4a2c97c7c3 100644 --- a/src/Main/Base/Project/Src/Services/MimeTypeDetection.cs +++ b/src/Main/Base/Project/Src/Services/MimeTypeDetection.cs @@ -2,12 +2,16 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.IO; using System.Runtime.InteropServices; +using System.Text; namespace ICSharpCode.SharpDevelop { public static class MimeTypeDetection { + const int BUFFER_SIZE = 4 * 1024; + [DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)] static extern unsafe int FindMimeFromData( IntPtr pBC, @@ -19,12 +23,15 @@ namespace ICSharpCode.SharpDevelop out IntPtr ppwzMimeOut, int dwReserved); - public static unsafe string FindMimeType(byte[] buffer) + static unsafe string FindMimeType(byte[] buffer, int offset, int length) { - fixed (byte *b = buffer) { + if (buffer.Length == 0) + return "text/plain"; + + fixed (byte *b = &buffer[offset]) { const int FMFD_ENABLEMIMESNIFFING = 0x00000002; IntPtr mimeout; - int result = FindMimeFromData(IntPtr.Zero, null, b, buffer.Length, null, FMFD_ENABLEMIMESNIFFING, out mimeout, 0); + int result = FindMimeFromData(IntPtr.Zero, null, b, length, null, FMFD_ENABLEMIMESNIFFING, out mimeout, 0); if (result != 0) throw Marshal.GetExceptionForHR(result); @@ -33,5 +40,31 @@ namespace ICSharpCode.SharpDevelop return mime; } } + + public static string FindMimeType(byte[] buffer) + { + if (buffer == null) + throw new ArgumentNullException("buffer"); + return FindMimeType(buffer, 0, buffer.Length); + } + + public static string FindMimeType(Stream stream) + { + if (stream == null) + throw new ArgumentNullException("stream"); + byte[] buffer = new byte[BUFFER_SIZE]; + stream.Position = 0; + return FindMimeType(buffer, 0, stream.Read(buffer, 0, buffer.Length)); + } + + public static string FindMimeType(ITextBuffer buffer) + { + if (buffer == null) + throw new ArgumentNullException("buffer"); + // TODO USE PROPER ENCODING! + // Maybe use Encoding detection from AvalonEdit? + byte[] bytes = Encoding.Default.GetBytes(buffer.TextLength > BUFFER_SIZE ? buffer.GetText(0, BUFFER_SIZE) : buffer.Text); + return FindMimeType(bytes, 0, bytes.Length); + } } }