Browse Source

Code cleanup

pull/23/head
Siegfried Pammer 14 years ago
parent
commit
297d2d2fb1
  1. 3
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj
  2. 58
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/DefaultSearchStrategy.cs
  3. 4
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/ISearchStrategy.cs
  4. 32
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/RegexSearchStrategy.cs
  5. 4
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchCommands.cs
  6. 31
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml
  7. 69
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml.cs
  8. 30
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchStrategyFactory.cs

3
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj

@ -290,7 +290,7 @@ @@ -290,7 +290,7 @@
<Compile Include="Rendering\VisualYPosition.cs">
<DependentUpon>VisualLine.cs</DependentUpon>
</Compile>
<Compile Include="Search\DefaultSearchStrategy.cs" />
<Compile Include="Search\RegexSearchStrategy.cs" />
<Compile Include="Search\DropDownButton.cs" />
<Compile Include="Search\ISearchStrategy.cs" />
<Compile Include="Search\SearchCommands.cs" />
@ -299,6 +299,7 @@ @@ -299,6 +299,7 @@
<DependentUpon>SearchPanel.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Search\SearchStrategyFactory.cs" />
<Compile Include="Snippets\IActiveElement.cs" />
<Compile Include="Snippets\SnippetAnchorElement.cs" />
<Compile Include="Snippets\SnippetEventArgs.cs" />

58
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/DefaultSearchStrategy.cs

@ -1,58 +0,0 @@ @@ -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 };
}
}
}
}

4
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/ISearchStrategy.cs

@ -20,9 +20,11 @@ namespace ICSharpCode.AvalonEdit.Search @@ -20,9 +20,11 @@ namespace ICSharpCode.AvalonEdit.Search
}
[Serializable]
public class SearchPatternException : Exception
{
public SearchPatternException(string message, Exception exception) : base(message, exception)
public SearchPatternException(string message, Exception exception)
: base(message, exception)
{
}
}

32
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/RegexSearchStrategy.cs

@ -0,0 +1,32 @@ @@ -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
{
}
}

4
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchCommands.cs

@ -25,8 +25,4 @@ namespace ICSharpCode.AvalonEdit.Search @@ -25,8 +25,4 @@ namespace ICSharpCode.AvalonEdit.Search
new InputGestureCollection { new KeyGesture(Key.F3, ModifierKeys.Shift) }
);
}
class SearchResult : TextSegment, ISearchResult
{
}
}

31
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml

@ -1,17 +1,16 @@ @@ -1,17 +1,16 @@
<?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.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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="3">
<TextBox Name="searchTextBox" Focusable="True" Width="100" Height="24" Margin="3,3,3,0" TextChanged="SearchTextBoxTextChanged" PreviewKeyDown="SearchLayerKeyDown" />
<StackPanel Orientation="Horizontal">
<TextBox x:FieldModifier="private" Name="searchTextBox" Focusable="True" Width="100" Height="24" Margin="3,3,3,0" PreviewKeyDown="SearchLayerKeyDown" AcceptsTab="True">
<TextBox.Text>
<Binding Path="SearchPattern" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<local:DropDownButton Height="24">
<local:DropDownButton.DropDownContent>
<Popup StaysOpen="False">
@ -25,20 +24,18 @@ @@ -25,20 +24,18 @@
</Popup>
</local:DropDownButton.DropDownContent>
</local:DropDownButton>
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindPrevious" ToolTip="Find Next (Ctrl+F3)">
<Image Width="16" Height="16" Stretch="Fill" Source="{StaticResource PrevImage}" />
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindPrevious" ToolTip="Find Next (Shift+F3)">
<Image Width="16" Height="16" Stretch="Fill" Source="prev.png" />
</Button>
<Button Margin="3" Height="24" Width="24" Command="local:SearchCommands.FindNext" ToolTip="Find Next (F3)">
<Image Width="16" Height="16" Stretch="Fill" Source="{StaticResource NextImage}" />
<Image Width="16" Height="16" Stretch="Fill" Source="next.png" />
</Button>
<Button Click="CloseClick" Margin="3" Height="16" Width="16" VerticalContentAlignment="Top" HorizontalContentAlignment="Left">
<Button Click="CloseClick" HorizontalAlignment="Right" VerticalAlignment="Top" Height="16" Width="16">
<Grid>
<Line X1="2" Y1="2" X2="8" Y2="8" Stroke="Black" StrokeThickness="1" />
<Line X1="8" Y1="2" X2="2" Y2="8" Stroke="Black" StrokeThickness="1" />
</Grid>
</Button>
</StackPanel>
<TextBlock Name="messageView" Grid.Row="1" Margin="3" Visibility="Collapsed" TextWrapping="Wrap" />
</Grid>
</Border>
</UserControl>

69
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchPanel.xaml.cs

@ -34,12 +34,35 @@ namespace ICSharpCode.AvalonEdit.Search @@ -34,12 +34,35 @@ namespace ICSharpCode.AvalonEdit.Search
public bool MatchCase { get { return matchCase.IsChecked == true; } }
public bool WholeWords { get { return wholeWords.IsChecked == true; } }
string searchPattern;
ISearchStrategy strategy;
public string SearchPattern {
get { return searchPattern; }
set {
searchPattern = value;
UpdateSearch();
}
}
void UpdateSearch(bool throwException = true)
{
if (!string.IsNullOrEmpty(searchPattern)) {
try {
strategy = SearchStrategyFactory.Create(searchPattern, !MatchCase, UseRegex, WholeWords);
DoSearch(true);
} catch (SearchPatternException ex) {
if (throwException) throw;
}
}
}
public SearchPanel(TextArea textArea)
{
if (textArea == null)
throw new ArgumentNullException("textArea");
this.textArea = textArea;
DataContext = this;
InitializeComponent();
textArea.TextView.Layers.Add(this);
@ -50,18 +73,13 @@ namespace ICSharpCode.AvalonEdit.Search @@ -50,18 +73,13 @@ namespace ICSharpCode.AvalonEdit.Search
textArea.Document.TextChanged += delegate { DoSearch(false); };
this.Loaded += delegate { searchTextBox.Focus(); };
useRegex.Checked += delegate { DoSearch(true); };
matchCase.Checked += delegate { DoSearch(true); };
wholeWords.Checked += delegate { DoSearch(true); };
useRegex.Unchecked += delegate { DoSearch(true); };
matchCase.Unchecked += delegate { DoSearch(true); };
wholeWords.Unchecked += delegate { DoSearch(true); };
}
useRegex.Checked += delegate { UpdateSearch(false); };
matchCase.Checked += delegate { UpdateSearch(false); };
wholeWords.Checked += delegate { UpdateSearch(false); };
void SearchTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
DoSearch(true);
useRegex.Unchecked += delegate { UpdateSearch(false); };
matchCase.Unchecked += delegate { UpdateSearch(false); };
wholeWords.Unchecked += delegate { UpdateSearch(false); };
}
/// <summary>
@ -99,14 +117,13 @@ namespace ICSharpCode.AvalonEdit.Search @@ -99,14 +117,13 @@ namespace ICSharpCode.AvalonEdit.Search
}
}
ToolTip messageView = new ToolTip { Placement = PlacementMode.Bottom };
void DoSearch(bool changeSelection)
{
renderer.CurrentResults.Clear();
currentResult = null;
messageView.Visibility = Visibility.Collapsed;
if (!string.IsNullOrEmpty(searchTextBox.Text)) {
try {
ISearchStrategy strategy = DefaultSearchStrategy.Create(searchTextBox.Text, !MatchCase, UseRegex, WholeWords);
if (!string.IsNullOrEmpty(SearchPattern)) {
int offset = textArea.Caret.Offset;
if (changeSelection) {
textArea.Selection = SimpleSelection.Empty;
@ -120,10 +137,6 @@ namespace ICSharpCode.AvalonEdit.Search @@ -120,10 +137,6 @@ namespace ICSharpCode.AvalonEdit.Search
}
renderer.CurrentResults.Add(result);
}
} catch (SearchPatternException ex) {
messageView.Text = "Error: " + ex.Message;
messageView.Visibility = Visibility.Visible;
}
}
textArea.TextView.InvalidateLayer(KnownLayer.Selection);
}
@ -140,8 +153,21 @@ namespace ICSharpCode.AvalonEdit.Search @@ -140,8 +153,21 @@ namespace ICSharpCode.AvalonEdit.Search
void SearchLayerKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) {
switch (e.Key) {
case Key.Enter:
try {
messageView.IsOpen = false;
messageView.Content = null;
UpdateSearch();
} catch (SearchPatternException ex) {
messageView.Content = "Error: " + ex.Message;
messageView.PlacementTarget = searchTextBox;
messageView.IsOpen = true;
}
break;
case Key.Escape:
CloseClick(sender, e);
break;
}
}
@ -149,6 +175,7 @@ namespace ICSharpCode.AvalonEdit.Search @@ -149,6 +175,7 @@ namespace ICSharpCode.AvalonEdit.Search
{
textArea.TextView.Layers.Remove(this);
textArea.TextView.BackgroundRenderers.Remove(renderer);
messageView.IsOpen = false;
}
}
}

30
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Search/SearchStrategyFactory.cs

@ -0,0 +1,30 @@ @@ -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…
Cancel
Save