8 changed files with 162 additions and 135 deletions
@ -1,58 +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.Text; |
|
||||||
using System.Text.RegularExpressions; |
|
||||||
using System.Windows; |
|
||||||
using System.Windows.Controls; |
|
||||||
using System.Windows.Controls.Primitives; |
|
||||||
using System.Windows.Data; |
|
||||||
using System.Windows.Documents; |
|
||||||
using System.Windows.Input; |
|
||||||
using System.Windows.Media; |
|
||||||
using System.Windows.Threading; |
|
||||||
using ICSharpCode.AvalonEdit.Document; |
|
||||||
using ICSharpCode.AvalonEdit.Editing; |
|
||||||
using ICSharpCode.AvalonEdit.Folding; |
|
||||||
using ICSharpCode.AvalonEdit.Rendering; |
|
||||||
|
|
||||||
namespace ICSharpCode.AvalonEdit.Search |
|
||||||
{ |
|
||||||
class DefaultSearchStrategy : ISearchStrategy |
|
||||||
{ |
|
||||||
readonly Regex searchPattern; |
|
||||||
|
|
||||||
public DefaultSearchStrategy(Regex searchPattern) |
|
||||||
{ |
|
||||||
this.searchPattern = searchPattern; |
|
||||||
} |
|
||||||
|
|
||||||
public static ISearchStrategy Create(string searchPattern, bool ignoreCase, bool useRegularExpressions, bool matchWholeWords) |
|
||||||
{ |
|
||||||
if (searchPattern == null) |
|
||||||
throw new ArgumentNullException("searchPattern"); |
|
||||||
RegexOptions options = RegexOptions.Compiled; |
|
||||||
if (ignoreCase) |
|
||||||
options |= RegexOptions.IgnoreCase; |
|
||||||
if (!useRegularExpressions) |
|
||||||
searchPattern = Regex.Escape(searchPattern); |
|
||||||
if (matchWholeWords) |
|
||||||
searchPattern = "\\b" + searchPattern + "\\b"; |
|
||||||
try { |
|
||||||
Regex pattern = new Regex(searchPattern, options); |
|
||||||
return new DefaultSearchStrategy(pattern); |
|
||||||
} catch (ArgumentException ex) { |
|
||||||
throw new SearchPatternException(ex.Message, ex); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public IEnumerable<ISearchResult> FindAll(ITextSource document) |
|
||||||
{ |
|
||||||
foreach (Match result in searchPattern.Matches(document.Text)) { |
|
||||||
yield return new SearchResult { StartOffset = result.Index, Length = result.Length }; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,32 @@ |
|||||||
|
// 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.Text.RegularExpressions; |
||||||
|
|
||||||
|
using ICSharpCode.AvalonEdit.Document; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.Search |
||||||
|
{ |
||||||
|
class RegexSearchStrategy : ISearchStrategy |
||||||
|
{ |
||||||
|
readonly Regex searchPattern; |
||||||
|
|
||||||
|
public RegexSearchStrategy(Regex searchPattern) |
||||||
|
{ |
||||||
|
this.searchPattern = searchPattern; |
||||||
|
} |
||||||
|
|
||||||
|
public IEnumerable<ISearchResult> FindAll(ITextSource document) |
||||||
|
{ |
||||||
|
foreach (Match result in searchPattern.Matches(document.Text)) { |
||||||
|
yield return new SearchResult { StartOffset = result.Index, Length = result.Length }; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class SearchResult : TextSegment, ISearchResult |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
@ -1,44 +1,41 @@ |
|||||||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||||||
<UserControl x:Class="ICSharpCode.AvalonEdit.Search.SearchPanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ICSharpCode.AvalonEdit.Search" FocusManager.IsFocusScope="True" PreviewKeyDown="SearchLayerKeyDown"> |
<UserControl x:Class="ICSharpCode.AvalonEdit.Search.SearchPanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ICSharpCode.AvalonEdit.Search" FocusManager.IsFocusScope="True" PreviewKeyDown="SearchLayerKeyDown"> |
||||||
<UserControl.Resources> |
|
||||||
<BitmapImage x:Key="PrevImage" UriSource="prev.png" /> |
|
||||||
<BitmapImage x:Key="NextImage" UriSource="next.png" /> |
|
||||||
</UserControl.Resources> |
|
||||||
<Border Background="White" BorderBrush="DimGray" BorderThickness="1" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="Arrow"> |
<Border Background="White" BorderBrush="DimGray" BorderThickness="1" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="Arrow"> |
||||||
<Grid> |
<StackPanel Orientation="Horizontal"> |
||||||
<Grid.RowDefinitions> |
<TextBox x:FieldModifier="private" Name="searchTextBox" Focusable="True" Width="100" Height="24" Margin="3,3,3,0" PreviewKeyDown="SearchLayerKeyDown" AcceptsTab="True"> |
||||||
<RowDefinition Height="Auto" /> |
<TextBox.Text> |
||||||
<RowDefinition Height="Auto" /> |
<Binding Path="SearchPattern" UpdateSourceTrigger="PropertyChanged"> |
||||||
</Grid.RowDefinitions> |
<Binding.ValidationRules> |
||||||
<StackPanel Orientation="Horizontal" Margin="3"> |
<ExceptionValidationRule /> |
||||||
<TextBox Name="searchTextBox" Focusable="True" Width="100" Height="24" Margin="3,3,3,0" TextChanged="SearchTextBoxTextChanged" PreviewKeyDown="SearchLayerKeyDown" /> |
</Binding.ValidationRules> |
||||||
<local:DropDownButton Height="24"> |
</Binding> |
||||||
<local:DropDownButton.DropDownContent> |
</TextBox.Text> |
||||||
<Popup StaysOpen="False"> |
</TextBox> |
||||||
<Border Background="White" BorderBrush="DimGray" BorderThickness="1"> |
<local:DropDownButton Height="24"> |
||||||
<StackPanel Orientation="Vertical"> |
<local:DropDownButton.DropDownContent> |
||||||
<CheckBox x:FieldModifier="private" x:Name="matchCase" Content="Match case" Margin="3" /> |
<Popup StaysOpen="False"> |
||||||
<CheckBox x:FieldModifier="private" x:Name="wholeWords" Content="Match whole words" Margin="3" /> |
<Border Background="White" BorderBrush="DimGray" BorderThickness="1"> |
||||||
<CheckBox x:FieldModifier="private" x:Name="useRegex" Content="Use Regular Expressions" Margin="3" /> |
<StackPanel Orientation="Vertical"> |
||||||
</StackPanel> |
<CheckBox x:FieldModifier="private" x:Name="matchCase" Content="Match case" Margin="3" /> |
||||||
</Border> |
<CheckBox x:FieldModifier="private" x:Name="wholeWords" Content="Match whole words" Margin="3" /> |
||||||
</Popup> |
<CheckBox x:FieldModifier="private" x:Name="useRegex" Content="Use Regular Expressions" Margin="3" /> |
||||||
</local:DropDownButton.DropDownContent> |
</StackPanel> |
||||||
</local:DropDownButton> |
</Border> |
||||||
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindPrevious" ToolTip="Find Next (Ctrl+F3)"> |
</Popup> |
||||||
<Image Width="16" Height="16" Stretch="Fill" Source="{StaticResource PrevImage}" /> |
</local:DropDownButton.DropDownContent> |
||||||
</Button> |
</local:DropDownButton> |
||||||
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindNext" ToolTip="Find Next (F3)"> |
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindPrevious" ToolTip="Find Next (Shift+F3)"> |
||||||
<Image Width="16" Height="16" Stretch="Fill" Source="{StaticResource NextImage}" /> |
<Image Width="16" Height="16" Stretch="Fill" Source="prev.png" /> |
||||||
</Button> |
</Button> |
||||||
<Button Click="CloseClick" Margin="3" Height="16" Width="16" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"> |
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindNext" ToolTip="Find Next (F3)"> |
||||||
<Grid> |
<Image Width="16" Height="16" Stretch="Fill" Source="next.png" /> |
||||||
<Line X1="2" Y1="2" X2="8" Y2="8" Stroke="Black" StrokeThickness="1" /> |
</Button> |
||||||
<Line X1="8" Y1="2" X2="2" Y2="8" Stroke="Black" StrokeThickness="1" /> |
<Button Click="CloseClick" HorizontalAlignment="Right" VerticalAlignment="Top" Height="16" Width="16"> |
||||||
</Grid> |
<Grid> |
||||||
</Button> |
<Line X1="2" Y1="2" X2="8" Y2="8" Stroke="Black" StrokeThickness="1" /> |
||||||
</StackPanel> |
<Line X1="8" Y1="2" X2="2" Y2="8" Stroke="Black" StrokeThickness="1" /> |
||||||
<TextBlock Name="messageView" Grid.Row="1" Margin="3" Visibility="Collapsed" TextWrapping="Wrap" /> |
</Grid> |
||||||
</Grid> |
</Button> |
||||||
|
</StackPanel> |
||||||
</Border> |
</Border> |
||||||
</UserControl> |
</UserControl> |
@ -0,0 +1,30 @@ |
|||||||
|
// 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.Text.RegularExpressions; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.Search |
||||||
|
{ |
||||||
|
public class SearchStrategyFactory |
||||||
|
{ |
||||||
|
public static ISearchStrategy Create(string searchPattern, bool ignoreCase, bool useRegularExpressions, bool matchWholeWords) |
||||||
|
{ |
||||||
|
if (searchPattern == null) |
||||||
|
throw new ArgumentNullException("searchPattern"); |
||||||
|
RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline; |
||||||
|
if (ignoreCase) |
||||||
|
options |= RegexOptions.IgnoreCase; |
||||||
|
if (!useRegularExpressions) |
||||||
|
searchPattern = Regex.Escape(searchPattern); |
||||||
|
if (matchWholeWords) |
||||||
|
searchPattern = "\\b" + searchPattern + "\\b"; |
||||||
|
try { |
||||||
|
Regex pattern = new Regex(searchPattern, options); |
||||||
|
return new RegexSearchStrategy(pattern); |
||||||
|
} catch (ArgumentException ex) { |
||||||
|
throw new SearchPatternException(ex.Message, ex); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue