Browse Source

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
shortcuts
Daniel Grunwald 20 years ago
parent
commit
516507117d
  1. 3
      src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceInFilesManager.cs
  2. 49
      src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceManager.cs
  3. 3
      src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/BruteForceSearchStrategy.cs
  4. 2
      src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/ISearchStrategy.cs
  5. 27
      src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/RegExSearchStrategy.cs
  6. 3
      src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/WildcardSearchStrategy.cs
  7. 8
      src/Main/StartUp/Project/Dialogs/ExceptionBox.cs

3
src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceInFilesManager.cs

@ -49,7 +49,8 @@ namespace SearchAndReplace
SetSearchOptions(); SetSearchOptions();
find.Reset(); find.Reset();
find.SearchStrategy.CompilePattern(); if (!find.SearchStrategy.CompilePattern())
return false;
currentFileName = String.Empty; currentFileName = String.Empty;
return true; return true;

49
src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchReplaceManager.cs

@ -36,21 +36,28 @@ namespace SearchAndReplace
find.DocumentIterator = SearchReplaceUtilities.CreateDocumentIterator(SearchOptions.DocumentIteratorType); find.DocumentIterator = SearchReplaceUtilities.CreateDocumentIterator(SearchOptions.DocumentIteratorType);
} }
// TODO: Transform Replace Pattern
public static void Replace() public static void Replace()
{ {
SetSearchOptions(); SetSearchOptions();
if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) { if (lastResult != null && WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) {
TextEditorControl textarea = ((ITextEditorControlProvider)WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent).TextEditorControl; ITextEditorControlProvider provider = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent as ITextEditorControlProvider;
string text = textarea.ActiveTextAreaControl.TextArea.SelectionManager.SelectedText; if (provider != null) {
if (text == SearchOptions.FindPattern) { TextEditorControl textarea = provider.TextEditorControl;
int offset = textarea.ActiveTextAreaControl.TextArea.SelectionManager.SelectionCollection[0].Offset; SelectionManager selectionManager = textarea.ActiveTextAreaControl.TextArea.SelectionManager;
textarea.BeginUpdate(); if (selectionManager.SelectionCollection.Count == 1
textarea.ActiveTextAreaControl.TextArea.SelectionManager.RemoveSelectedText(); && selectionManager.SelectionCollection[0].Offset == lastResult.Offset
textarea.Document.Insert(offset, SearchOptions.ReplacePattern); && selectionManager.SelectionCollection[0].Length == lastResult.Length
textarea.ActiveTextAreaControl.Caret.Position = textarea.Document.OffsetToPosition(offset + SearchOptions.ReplacePattern.Length); && lastResult.FileName == textarea.FileName)
textarea.EndUpdate(); {
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(); FindNext();
@ -65,7 +72,8 @@ namespace SearchAndReplace
textArea.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection(); textArea.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection();
} }
find.Reset(); find.Reset();
find.SearchStrategy.CompilePattern(); if (!find.SearchStrategy.CompilePattern())
return;
while (true) { while (true) {
SearchResult result = SearchReplaceManager.find.FindNext(); SearchResult result = SearchReplaceManager.find.FindNext();
@ -91,11 +99,14 @@ namespace SearchAndReplace
{ {
SetSearchOptions(); SetSearchOptions();
if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) { if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) {
TextEditorControl textArea = ((ITextEditorControlProvider)WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent).TextEditorControl; ITextEditorControlProvider provider = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ViewContent as ITextEditorControlProvider;
textArea.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection(); if (provider != null) {
provider.TextEditorControl.ActiveTextAreaControl.TextArea.SelectionManager.ClearSelection();
}
} }
find.Reset(); find.Reset();
find.SearchStrategy.CompilePattern(); if (!find.SearchStrategy.CompilePattern())
return;
List<TextEditorControl> textAreas = new List<TextEditorControl>(); List<TextEditorControl> textAreas = new List<TextEditorControl>();
while (true) { while (true) {
@ -136,7 +147,11 @@ namespace SearchAndReplace
return; return;
} }
find.SearchStrategy.CompilePattern(); if (!find.SearchStrategy.CompilePattern()) {
find.Reset();
lastResult = null;
return;
}
SearchResult result = find.FindNext(); SearchResult result = find.FindNext();
if (result == null) { if (result == null) {

3
src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/BruteForceSearchStrategy.cs

@ -58,9 +58,10 @@ namespace SearchAndReplace
return -1; return -1;
} }
public void CompilePattern() public bool CompilePattern()
{ {
searchPattern = SearchOptions.MatchCase ? SearchOptions.FindPattern : SearchOptions.FindPattern.ToUpper(); searchPattern = SearchOptions.MatchCase ? SearchOptions.FindPattern : SearchOptions.FindPattern.ToUpper();
return true;
} }
public SearchResult FindNext(ITextIterator textIterator) public SearchResult FindNext(ITextIterator textIterator)

2
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 /// update their pattern information. This method will be called
/// before the FindNext function. /// before the FindNext function.
/// </remarks> /// </remarks>
void CompilePattern(); bool CompilePattern();
/// <remarks> /// <remarks>
/// The find next method should search the next occurrence of the /// The find next method should search the next occurrence of the

27
src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/RegExSearchStrategy.cs

@ -17,13 +17,19 @@ namespace SearchAndReplace
{ {
Regex regex = null; Regex regex = null;
public void CompilePattern() public bool CompilePattern()
{ {
RegexOptions regexOptions = RegexOptions.Compiled; RegexOptions regexOptions = RegexOptions.Compiled;
if (!SearchOptions.MatchCase) { if (!SearchOptions.MatchCase) {
regexOptions |= RegexOptions.IgnoreCase; 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) public SearchResult FindNext(ITextIterator textIterator)
@ -37,7 +43,7 @@ namespace SearchAndReplace
} else { } else {
int delta = m.Index - textIterator.Position; int delta = m.Index - textIterator.Position;
if (delta <= 0 || textIterator.MoveAhead(delta)) { if (delta <= 0 || textIterator.MoveAhead(delta)) {
return new SearchResult(m.Index, m.Length); return new RegexSearchResult(m);
} else { } else {
return null; return null;
} }
@ -46,5 +52,20 @@ namespace SearchAndReplace
return null; 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);
}
}
} }
} }

3
src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/WildcardSearchStrategy.cs

@ -144,9 +144,10 @@ namespace SearchAndReplace
return -1; return -1;
} }
public void CompilePattern() public bool CompilePattern()
{ {
CompilePattern(SearchOptions.FindPattern, !SearchOptions.MatchCase); CompilePattern(SearchOptions.FindPattern, !SearchOptions.MatchCase);
return true;
} }
public SearchResult FindNext(ITextIterator textIterator) public SearchResult FindNext(ITextIterator textIterator)

8
src/Main/StartUp/Project/Dialogs/ExceptionBox.cs

@ -91,9 +91,12 @@ namespace ICSharpCode.SharpDevelop
{ {
CopyInfoToClipboard(); CopyInfoToClipboard();
// open IE via process.start to our bug reporting forum StartUrl("http://community.sharpdevelop.net/forums/23/ShowForum.aspx");
//Process.Start("http://www.icsharpcode.net/OpenSource/SD/Forum/forum.asp?FORUM_ID=5");
//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, " + string text = "This version of SharpDevelop is an internal build, " +
"not a released version.\n" + "not a released version.\n" +
"Please report problems in the internal builds to the " + "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."); + Uri.EscapeDataString("Write an English description on how to reproduce the error and paste the exception text.");
StartUrl(url); StartUrl(url);
} }
*/
} }
static void StartUrl(string url) static void StartUrl(string url)

Loading…
Cancel
Save