Skip to content

Commit de8263a

Browse files
author
Sébastien Geiser
committed
Refactoring Search and combine selection and search highlight
1 parent 0f8b327 commit de8263a

17 files changed

+658
-73
lines changed

RegexDialog/BracketColorizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override void ColorizeLine(DocumentLine line)
2020
{
2121
if (StartOffset >= line.Offset && EndOffset <= line.Offset + line.Length)
2222
{
23-
ChangeLinePart(StartOffset, EndOffset, delegate(VisualLineElement element)
23+
ChangeLinePart(StartOffset, EndOffset, delegate (VisualLineElement element)
2424
{
2525
element.TextRunProperties.SetBackgroundBrush(Brushes.LightGray);
2626
});

RegexDialog/Converters/CustomBoolToVisibilityConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
4343
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()) && InDesigner != null) return InDesigner;
4444
else if (value == null) return OnNullValue;
4545
else if (value == DependencyProperty.UnsetValue) return OnUnsetValue;
46-
46+
4747

4848
return (value is bool && (bool)value ? TrueValue : FalseValue);
4949
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Windows.Data;
6+
using System.Windows.Markup;
7+
8+
namespace RegexDialog.Converters
9+
{
10+
/// <summary>
11+
/// MultiBinding Converter that use a string mathematical or pseudo C# expression to make the conversion.
12+
/// Use <c>bindings</c> as an array of object to inject bindings values in the expression (example <c>Abs(bindings[0]) + bindings[1]</c>)
13+
/// </summary>
14+
[ContentProperty("Expression")]
15+
public class ExpressionEvalMultiBindingConverter : BaseConverter, IMultiValueConverter
16+
{
17+
/// <summary>
18+
/// To specify a list of namespaces separated by the ; character to add as usings for the evaluator
19+
/// </summary>
20+
public string NamespacesToAdd { get; set; } = string.Empty;
21+
22+
/// <summary>
23+
/// The expression to evaluate to make the conversion. Use <c>bindings</c> as an array of object to inject bindings values in the expression. By default just <c>bindings</c>
24+
/// </summary>
25+
public string Expression { get; set; } = "bindings";
26+
27+
/// <summary>
28+
/// The expression to evaluate to make the back conversion. Use <c>binding</c> to inject the binding value in the expression. By default just <c>binding</c>
29+
/// Must return an array of object
30+
/// </summary>
31+
public string ExpressionForConvertBack { get; set; } = "binding";
32+
33+
/// <summary>
34+
/// If <c>>= 0</c> evaluate the binding at corresponding index as an expression, if just inject the binding in the Expression, By default : <c>-1</c>
35+
/// </summary>
36+
public int EvaluateBindingAtIndexAsAnExpression { get; set; } = -1;
37+
38+
/// <summary>
39+
/// If <c>true</c> evaluate a string binding as an expression, if <c>false</c> just inject the binding in the ExpressionForConvertBack, By default : <c>false</c>
40+
/// </summary>
41+
public bool EvaluateBindingAsAnExpressionForConvertBack { get; set; }
42+
43+
/// <summary>
44+
/// If <c>true</c> Evaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
45+
/// By default : false for security
46+
/// </summary>
47+
public bool OptionEvaluateFunctionActive { get; set; }
48+
49+
/// <summary>
50+
/// if true all evaluation are case sensitives, if false evaluations are case insensitive.
51+
/// By default = true
52+
/// </summary>
53+
public bool OptionCaseSensitiveEvaluationActive { get; set; } = true;
54+
55+
/// <summary>
56+
/// If <c>true</c> throw up all evaluate exceptions, if <c>false</c> just return the exception message as a string, By default <c>false</c>
57+
/// </summary>
58+
public bool ThrowExceptions { get; set; }
59+
60+
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
61+
{
62+
Dictionary<string, object> variables = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
63+
64+
try
65+
{
66+
ExpressionEvaluator evaluator = new ExpressionEvaluator()
67+
{
68+
OptionCaseSensitiveEvaluationActive = OptionCaseSensitiveEvaluationActive
69+
};
70+
71+
evaluator.Namespaces.NamespacesListForWPFConverters();
72+
73+
NamespacesToAdd.Split(';').ToList().ForEach(namespaceName =>
74+
{
75+
if (!string.IsNullOrWhiteSpace(namespaceName))
76+
{
77+
evaluator.Namespaces.Add(namespaceName);
78+
}
79+
});
80+
81+
evaluator.OptionEvaluateFunctionActive = OptionEvaluateFunctionActive;
82+
83+
variables["bindings"] = values;
84+
85+
evaluator.Variables = variables;
86+
87+
if (EvaluateBindingAtIndexAsAnExpression >= 0)
88+
{
89+
return evaluator.Evaluate(values[EvaluateBindingAtIndexAsAnExpression].ToString());
90+
}
91+
else
92+
{
93+
return evaluator.Evaluate(Expression);
94+
}
95+
}
96+
catch (Exception ex)
97+
{
98+
if (ThrowExceptions)
99+
{
100+
throw;
101+
}
102+
else
103+
{
104+
return ex.Message;
105+
}
106+
}
107+
}
108+
109+
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
110+
{
111+
Dictionary<string, object> variables = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
112+
113+
try
114+
{
115+
ExpressionEvaluator evaluator = new ExpressionEvaluator()
116+
{
117+
OptionCaseSensitiveEvaluationActive = OptionCaseSensitiveEvaluationActive
118+
};
119+
120+
evaluator.Namespaces.NamespacesListForWPFConverters();
121+
122+
evaluator.OptionEvaluateFunctionActive = OptionEvaluateFunctionActive;
123+
124+
if (EvaluateBindingAsAnExpressionForConvertBack)
125+
{
126+
variables["binding"] = evaluator.Evaluate(value.ToString());
127+
}
128+
else
129+
{
130+
variables["binding"] = value;
131+
}
132+
133+
evaluator.Variables = variables;
134+
135+
return (object[])evaluator.Evaluate(ExpressionForConvertBack);
136+
}
137+
catch (Exception ex)
138+
{
139+
if (ThrowExceptions)
140+
{
141+
throw;
142+
}
143+
else
144+
{
145+
return new object[] { ex.Message };
146+
}
147+
}
148+
}
149+
}
150+
}

RegexDialog/Model/RegExOptionModel.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
11
using System;
2-
using System.ComponentModel;
3-
using System.Runtime.CompilerServices;
42
using System.Text.RegularExpressions;
53

64
namespace RegexDialog
75
{
8-
internal class RegExOptionViewModel : INotifyPropertyChanged
6+
internal class RegExOptionViewModel : NotifyPropertyChangedBaseClass
97
{
108
/// <summary>
119
/// Sélectionné
1210
/// </summary>
1311
public bool Selected
1412
{
15-
get
16-
{
17-
return Config.Instance.RegexOptionsSelection.ContainsKey(Name) && Config.Instance.RegexOptionsSelection[Name];
13+
get
14+
{
15+
return Config.Instance.RegexOptionsSelection.ContainsKey(Name) && Config.Instance.RegexOptionsSelection[Name];
1816
}
19-
set
17+
set
2018
{
2119
Config.Instance.RegexOptionsSelection[Name] = value;
2220
Config.Instance.Save();
2321
NotifyPropertyChanged();
2422
}
2523
}
2624

27-
private RegexOptions regexOptions = RegexOptions.None;
25+
private RegexOptions regexOptions;
2826

2927
/// <summary>
3028
/// L'option d'expression régulière représentée
3129
/// </summary>
3230
public RegexOptions RegexOptions
3331
{
3432
get { return regexOptions; }
35-
set
33+
set
3634
{
3735
regexOptions = value;
3836
NotifyPropertyChanged();
@@ -44,26 +42,10 @@ public RegexOptions RegexOptions
4442
/// </summary>
4543
public string Name
4644
{
47-
get
45+
get
4846
{
4947
return Enum.GetName(typeof(RegexOptions), regexOptions);
5048
}
5149
}
52-
53-
#region INotifyPropertyChanged Membres
54-
55-
/// <summary>
56-
/// Génère l'évènement PropertyChanged pour la propriété spécifiée
57-
/// </summary>
58-
/// <param name="propertyName"></param>
59-
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
60-
{
61-
if (PropertyChanged != null)
62-
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
63-
}
64-
65-
public event PropertyChangedEventHandler PropertyChanged;
66-
67-
#endregion
6850
}
6951
}

RegexDialog/Model/RegexCaptureResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RegexDialog
55
internal class RegexCaptureResult : RegexResult
66
{
77
public RegexCaptureResult(Regex regex, Capture capture, int captureNb, string fileName = "", int selectionIndex = 0) : base(regex, capture, captureNb, fileName, selectionIndex)
8-
{}
8+
{ }
99

1010
public override string ElementType
1111
{

RegexDialog/Model/RegexFileResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RegexDialog
55
internal class RegexFileResult : RegexResult
66
{
77
public RegexFileResult(Regex regex, Capture regexElement, int regexElementNb, string fileName) : base(regex, regexElement, regexElementNb, fileName, 0)
8-
{}
8+
{ }
99

1010
public override string Name => $"File {RegexElementNb}: {Children.Count} matches found in \"{FileName}\"";
1111

RegexDialog/Model/RegexGroupResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public RegexGroupResult(Regex regex, Group group, int groupNb, string fileName =
1212
Children = group.Captures
1313
.Cast<Capture>()
1414
.ToList()
15-
.ConvertAll(delegate(Capture c)
15+
.ConvertAll(delegate (Capture c)
1616
{
1717
RegexResult result = new RegexCaptureResult(regex, c, i, fileName, selectionIndex)
1818
{

RegexDialog/Model/RegexMatchResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RegexDialog
55
{
66
internal class RegexMatchResult : RegexResult
77
{
8-
public RegexMatchResult(Regex regex, Match match, int matchNb, string fileName = "", int selectionIndex = 0) : base(regex, match, matchNb, fileName, selectionIndex)
8+
public RegexMatchResult(Regex regex, Match match, int matchNb, string fileName = "", int selectionIndex = 0) : base(regex, match, matchNb, fileName, selectionIndex)
99
{
1010
int i = 0;
1111

@@ -23,8 +23,8 @@ public RegexMatchResult(Regex regex, Match match, int matchNb, string fileName =
2323

2424
return result;
2525
});
26-
27-
if(Children.Count > 0)
26+
27+
if (Children.Count > 0)
2828
Children.RemoveAt(0);
2929

3030
IsExpanded = Config.Instance.MatchesShowLevel > 1;

0 commit comments

Comments
 (0)