From 516507117d73cf8e4d59399e27dfef33466a6c24 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 30 Oct 2005 12:30:59 +0000 Subject: [PATCH] RegExSearchStrategy now supports replacing "$1" with the group value. Fixed exception when using invalid RegEx. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@655 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Engine/SearchReplaceInFilesManager.cs | 3 +- .../Engine/SearchReplaceManager.cs | 47 ++++++++++++------- .../BruteForceSearchStrategy.cs | 3 +- .../Engine/SearchStrategy/ISearchStrategy.cs | 2 +- .../SearchStrategy/RegExSearchStrategy.cs | 27 +++++++++-- .../SearchStrategy/WildcardSearchStrategy.cs | 3 +- .../StartUp/Project/Dialogs/ExceptionBox.cs | 8 +++- 7 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceInFilesManager.cs b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceInFilesManager.cs index 5dc4cedc19..f15e77dced 100644 --- a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceInFilesManager.cs +++ b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceInFilesManager.cs @@ -49,7 +49,8 @@ namespace SearchAndReplace SetSearchOptions(); find.Reset(); - find.SearchStrategy.CompilePattern(); + if (!find.SearchStrategy.CompilePattern()) + return false; currentFileName = String.Empty; return true; diff --git a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceManager.cs b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceManager.cs index 17a6c77dbf..42fa92302c 100644 --- a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceManager.cs +++ b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceManager.cs @@ -36,21 +36,28 @@ namespace SearchAndReplace find.DocumentIterator = SearchReplaceUtilities.CreateDocumentIterator(SearchOptions.DocumentIteratorType); } - // TODO: Transform Replace Pattern public static void Replace() { SetSearchOptions(); - if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) { - TextEditorControl textarea = ((ITextEditorControlProvider)WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent).TextEditorControl; - string text = textarea.ActiveTextAreaControl.TextArea.SelectionManager.SelectedText; - if (text == SearchOptions.FindPattern) { - int offset = textarea.ActiveTextAreaControl.TextArea.SelectionManager.SelectionCollection[0].Offset; + if (lastResult != null && WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) { + ITextEditorControlProvider provider = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent as ITextEditorControlProvider; + if (provider != null) { + TextEditorControl textarea = provider.TextEditorControl; + SelectionManager selectionManager = textarea.ActiveTextAreaControl.TextArea.SelectionManager; - textarea.BeginUpdate(); - textarea.ActiveTextAreaControl.TextArea.SelectionManager.RemoveSelectedText(); - textarea.Document.Insert(offset, SearchOptions.ReplacePattern); - textarea.ActiveTextAreaControl.Caret.Position = textarea.Document.OffsetToPosition(offset + SearchOptions.ReplacePattern.Length); - textarea.EndUpdate(); + if (selectionManager.SelectionCollection.Count == 1 + && selectionManager.SelectionCollection[0].Offset == lastResult.Offset + && selectionManager.SelectionCollection[0].Length == lastResult.Length + && lastResult.FileName == textarea.FileName) + { + string replacePattern = lastResult.TransformReplacePattern(SearchOptions.ReplacePattern); + + textarea.BeginUpdate(); + selectionManager.ClearSelection(); + textarea.Document.Replace(lastResult.Offset, lastResult.Length, replacePattern); + textarea.ActiveTextAreaControl.Caret.Position = textarea.Document.OffsetToPosition(lastResult.Offset + replacePattern.Length); + textarea.EndUpdate(); + } } } FindNext(); @@ -65,7 +72,8 @@ namespace SearchAndReplace textArea.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection(); } find.Reset(); - find.SearchStrategy.CompilePattern(); + if (!find.SearchStrategy.CompilePattern()) + return; while (true) { SearchResult result = SearchReplaceManager.find.FindNext(); @@ -91,11 +99,14 @@ namespace SearchAndReplace { SetSearchOptions(); if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) { - TextEditorControl textArea = ((ITextEditorControlProvider)WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent).TextEditorControl; - textArea.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection(); + ITextEditorControlProvider provider = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent as ITextEditorControlProvider; + if (provider != null) { + provider.TextEditorControl.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection(); + } } find.Reset(); - find.SearchStrategy.CompilePattern(); + if (!find.SearchStrategy.CompilePattern()) + return; List textAreas = new List(); while (true) { @@ -136,7 +147,11 @@ namespace SearchAndReplace return; } - find.SearchStrategy.CompilePattern(); + if (!find.SearchStrategy.CompilePattern()) { + find.Reset(); + lastResult = null; + return; + } SearchResult result = find.FindNext(); if (result == null) { diff --git a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/BruteForceSearchStrategy.cs b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/BruteForceSearchStrategy.cs index a6d41a0c10..7e6a026234 100644 --- a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/BruteForceSearchStrategy.cs +++ b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/BruteForceSearchStrategy.cs @@ -58,9 +58,10 @@ namespace SearchAndReplace return -1; } - public void CompilePattern() + public bool CompilePattern() { searchPattern = SearchOptions.MatchCase ? SearchOptions.FindPattern : SearchOptions.FindPattern.ToUpper(); + return true; } public SearchResult FindNext(ITextIterator textIterator) diff --git a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/ISearchStrategy.cs b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/ISearchStrategy.cs index e2eba2e755..f9046d78d2 100644 --- a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/ISearchStrategy.cs +++ b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/ISearchStrategy.cs @@ -24,7 +24,7 @@ namespace SearchAndReplace /// update their pattern information. This method will be called /// before the FindNext function. /// - void CompilePattern(); + bool CompilePattern(); /// /// The find next method should search the next occurrence of the diff --git a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/RegExSearchStrategy.cs b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/RegExSearchStrategy.cs index 046ae92d91..3b8526b488 100644 --- a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/RegExSearchStrategy.cs +++ b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/RegExSearchStrategy.cs @@ -17,13 +17,19 @@ namespace SearchAndReplace { Regex regex = null; - public void CompilePattern() + public bool CompilePattern() { RegexOptions regexOptions = RegexOptions.Compiled; if (!SearchOptions.MatchCase) { regexOptions |= RegexOptions.IgnoreCase; } - regex = new Regex(SearchOptions.FindPattern, regexOptions); + try { + regex = new Regex(SearchOptions.FindPattern, regexOptions); + return true; + } catch (ArgumentException ex) { + MessageService.ShowError("Error parsing regular expression:\n" + ex.Message); + return false; + } } public SearchResult FindNext(ITextIterator textIterator) @@ -37,7 +43,7 @@ namespace SearchAndReplace } else { int delta = m.Index - textIterator.Position; if (delta <= 0 || textIterator.MoveAhead(delta)) { - return new SearchResult(m.Index, m.Length); + return new RegexSearchResult(m); } else { return null; } @@ -46,5 +52,20 @@ namespace SearchAndReplace return null; } + + private class RegexSearchResult : SearchResult + { + 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/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/WildcardSearchStrategy.cs b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/WildcardSearchStrategy.cs index 7432ea7af2..a315398b56 100644 --- a/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/WildcardSearchStrategy.cs +++ b/src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/WildcardSearchStrategy.cs @@ -144,9 +144,10 @@ namespace SearchAndReplace return -1; } - public void CompilePattern() + public bool CompilePattern() { CompilePattern(SearchOptions.FindPattern, !SearchOptions.MatchCase); + return true; } public SearchResult FindNext(ITextIterator textIterator) diff --git a/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs b/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs index 43de914381..7b9c8a5871 100644 --- a/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs +++ b/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs @@ -91,9 +91,12 @@ namespace ICSharpCode.SharpDevelop { CopyInfoToClipboard(); - // open IE via process.start to our bug reporting forum - //Process.Start("http://www.icsharpcode.net/OpenSource/SD/Forum/forum.asp?FORUM_ID=5"); + StartUrl("http://community.sharpdevelop.net/forums/23/ShowForum.aspx"); + //Version v = Assembly.GetEntryAssembly().GetName().Version; + //StartUrl("http://www.icsharpcode.net/OpenSource/SD/BugReporting.aspx?version=" + v.Major + "." + v.Minor + "." + v.Revision + "." + v.Build); + + /* string text = "This version of SharpDevelop is an internal build, " + "not a released version.\n" + "Please report problems in the internal builds to the " + @@ -119,6 +122,7 @@ namespace ICSharpCode.SharpDevelop + Uri.EscapeDataString("Write an English description on how to reproduce the error and paste the exception text."); StartUrl(url); } + */ } static void StartUrl(string url)