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. 47
      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 @@ -49,7 +49,8 @@ namespace SearchAndReplace
SetSearchOptions();
find.Reset();
find.SearchStrategy.CompilePattern();
if (!find.SearchStrategy.CompilePattern())
return false;
currentFileName = String.Empty;
return true;

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

@ -36,21 +36,28 @@ namespace SearchAndReplace @@ -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 @@ -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 @@ -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<TextEditorControl> textAreas = new List<TextEditorControl>();
while (true) {
@ -136,7 +147,11 @@ namespace SearchAndReplace @@ -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) {

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

@ -58,9 +58,10 @@ namespace SearchAndReplace @@ -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)

2
src/Main/Base/Project/Src/TextEditor/SearchAndReplace/Engine/SearchStrategy/ISearchStrategy.cs

@ -24,7 +24,7 @@ namespace SearchAndReplace @@ -24,7 +24,7 @@ namespace SearchAndReplace
/// update their pattern information. This method will be called
/// before the FindNext function.
/// </remarks>
void CompilePattern();
bool CompilePattern();
/// <remarks>
/// 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 @@ -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 @@ -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 @@ -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);
}
}
}
}

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

@ -144,9 +144,10 @@ namespace SearchAndReplace @@ -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)

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

@ -91,9 +91,12 @@ namespace ICSharpCode.SharpDevelop @@ -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 @@ -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)

Loading…
Cancel
Save