Skip to content

Commit e07af91

Browse files
Refactoring: use embedded avalon search box.
1 parent af4cc60 commit e07af91

File tree

5 files changed

+162
-226
lines changed

5 files changed

+162
-226
lines changed

Nitra.Visualizer/MainWindow.xaml

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
<RoutedUICommand x:Key="UpdateTest" Text="Update Test" />
1414
<RoutedUICommand x:Key="RemoveTest" Text="Remove Test" />
1515
<RoutedUICommand x:Key="RunTests" Text="Run Tests" />
16-
<RoutedUICommand x:Key="FindNext" Text="Find Next" />
17-
<RoutedUICommand x:Key="FindNextSelected" Text="Find Next Selected" />
18-
<RoutedUICommand x:Key="FindPrevSelected" Text="Find Previos Selected" />
19-
<RoutedUICommand x:Key="FindPrev" Text="Find Prev" />
2016
<RoutedUICommand x:Key="CopyNodeText" Text="Copy" />
2117
<RoutedUICommand x:Key="RunTest" Text="Run test" />
2218
<RoutedUICommand x:Key="Repars" Text="Repars code" />
@@ -37,25 +33,12 @@
3733
<CommandBinding Command="{StaticResource RemoveTest}" Executed="OnRemoveTest" CanExecute="CommandBinding_CanRemoveTest" />
3834
<CommandBinding Command="{StaticResource CopyNodeText}" Executed="CopyReflectionNodeText" />
3935
<CommandBinding Command="{StaticResource RunTest}" Executed="OnRunTest" CanExecute="CommandBinding_CanRunTest" />
40-
<CommandBinding Command="{StaticResource FindNext}" Executed="FindNext_Executed" CanExecute="CommandBinding_CanExecute" />
41-
<CommandBinding Command="{StaticResource FindPrev}" Executed="FindPrev_Executed" CanExecute="CommandBinding_CanExecute" />
42-
<CommandBinding Command="{StaticResource FindNextSelected}" Executed="FindNext_Executed_Selected" CanExecute="CommandBinding_CanExecute" />
43-
<CommandBinding Command="{StaticResource FindPrevSelected}" Executed="FindPrev_Executed_Selected" CanExecute="CommandBinding_CanExecute" />
44-
<CommandBinding Command="Open" Executed="FileOpenExecuted" />
45-
<CommandBinding Command="Find" Executed="FindExecuted" CanExecute="FindCanExecute"/>
4636
</Window.CommandBindings>
4737

4838
<Window.InputBindings>
49-
<KeyBinding Command="{StaticResource FindNext}" Key="Enter" />
50-
<KeyBinding Command="{StaticResource FindNext}" Key="F3" />
51-
<KeyBinding Command="{StaticResource FindNextSelected}" Key="F3" Modifiers="Control" />
52-
<KeyBinding Command="{StaticResource FindPrevSelected}" Key="F3" Modifiers="Control + Shift" />
53-
<KeyBinding Command="{StaticResource FindPrev}" Key="F3" Modifiers="Shift" />
54-
<KeyBinding Command="{StaticResource FindPrev}" Key="Enter" Modifiers="Shift"/>
5539
<KeyBinding Command="{StaticResource Repars}" Key="R" Modifiers="Control"/>
5640
</Window.InputBindings>
5741

58-
5942
<Grid>
6043
<Grid.RowDefinitions>
6144
<RowDefinition Height="Auto" />
@@ -163,22 +146,6 @@
163146
<GridSplitter Grid.Row="1" Grid.Column="0" Width="9" ResizeDirection="Columns" HorizontalAlignment="Right" VerticalAlignment="Stretch" Background="#5700FF00" Margin="0,0,-9,0" />
164147
<nitra:NitraTextEditor x:Name="_text" Grid.Row="1" Grid.Column="2" TextChanged="textBox1_TextChanged" MouseHover="textBox1_MouseHover" MouseHoverStopped="textBox1_MouseHoverStopped" FontFamily="Consolas" FontSize="16" LostFocus="textBox1_LostFocus" GotFocus="textBox1_GotFocus" HighlightLine="textBox1_HighlightLine" KeyDown="_control_KeyDown_resize" />
165148
</Grid>
166-
167-
<Grid Grid.Row="1" Visibility="Visible" Name="_findGrid">
168-
<Grid.ColumnDefinitions>
169-
<ColumnDefinition Width="191*" />
170-
<ColumnDefinition Width="617*" />
171-
<ColumnDefinition Width="Auto" />
172-
</Grid.ColumnDefinitions>
173-
<TextBox Name="_findText" HorizontalAlignment="Stretch" Grid.ColumnSpan="2" />
174-
<StackPanel Orientation="Horizontal" Grid.Column="2">
175-
<Button Content="Prev" Height="23" Name="_findPrev" Margin="3,2,3,2" Command="{StaticResource FindPrev}" />
176-
<Button Content="Next" Height="23" Name="_findNext" Margin="3,2,3,2" Command="{StaticResource FindNext}" />
177-
<CheckBox Content="Match case" Height="16" Name="_findMatchCase" Margin="3,2,3,2" />
178-
<CheckBox Content="Match whole word" Height="16" Name="_findMatchWholeWord" Margin="3,2,3,2" />
179-
<Button Content="Close" Height="23" Name="_findClose" Margin="3,2,3,2" Click="_findClose_Click" />
180-
</StackPanel>
181-
</Grid>
182149
</Grid>
183150
<GridSplitter Grid.Row="1" Height="8" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Background="#5700FF00" />
184151
<TabControl Name="_tabControl" Grid.Row="2" SelectionChanged="_tabControl_SelectionChanged">

Nitra.Visualizer/MainWindow.xaml.cs

Lines changed: 0 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public MainWindow()
8181
_configComboBox.SelectedItem = config == "Release" ? "Release" : "Debug";
8282

8383
_tabControl.SelectedIndex = _settings.ActiveTabIndex;
84-
_findGrid.Visibility = System.Windows.Visibility.Collapsed;
8584
_foldingStrategy = new NitraFoldingStrategy();
8685
_textBox1Tooltip = new ToolTip { PlacementTarget = _text };
8786
_parseTimer = new Timer { AutoReset = false, Enabled = false, Interval = 300 };
@@ -560,116 +559,12 @@ private void textBox1_MouseHoverStopped(object sender, MouseEventArgs e)
560559
_textBox1Tooltip.IsOpen = false;
561560
}
562561

563-
private void FindCanExecute(object sender, CanExecuteRoutedEventArgs e)
564-
{
565-
e.CanExecute = true;
566-
e.Handled = true;
567-
}
568-
569-
private void FindExecuted(object sender, ExecutedRoutedEventArgs e)
570-
{
571-
_findGrid.Visibility = System.Windows.Visibility.Visible;
572-
_findText.Focus();
573-
e.Handled = true;
574-
}
575-
576-
private void _findClose_Click(object sender, RoutedEventArgs e)
577-
{
578-
_findGrid.Visibility = System.Windows.Visibility.Collapsed;
579-
}
580-
581562
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
582563
{
583564
e.CanExecute = true;
584565
e.Handled = true;
585566
}
586567

587-
private void FindNext_Executed(object sender, ExecutedRoutedEventArgs e)
588-
{
589-
FindNext();
590-
}
591-
592-
private void FindPrev_Executed(object sender, ExecutedRoutedEventArgs e)
593-
{
594-
FindPrev();
595-
}
596-
597-
private void FindNext()
598-
{
599-
var option = GetMatchCaseOption();
600-
var toFind = _findText.Text;
601-
var text = _text.Text;
602-
var index = _text.SelectionStart + _text.SelectionLength - 1;
603-
604-
do
605-
{
606-
if (index < text.Length)
607-
index++;
608-
index = text.IndexOf(toFind, index, option);
609-
}
610-
while (index >= 0 && !IsWholeWord(text, index, toFind.Length));
611-
612-
var found = index >= 0;
613-
if (found)
614-
{
615-
_text.Select(index, toFind.Length);
616-
_text.ScrollToLine(_text.TextArea.Caret.Line);
617-
}
618-
else
619-
_status.Text = "Can't find '" + toFind + "'.";
620-
}
621-
622-
private void FindPrev()
623-
{
624-
var option = GetMatchCaseOption();
625-
var toFind = _findText.Text;
626-
var text = _text.Text;
627-
var index = _text.SelectionStart;
628-
629-
do
630-
{
631-
if (index > 0)
632-
index--;
633-
index = text.LastIndexOf(toFind, index, option);
634-
}
635-
while (index >= 0 && !IsWholeWord(text, index, toFind.Length));
636-
637-
var found = index >= 0;
638-
639-
if (found)
640-
{
641-
_text.Select(index, toFind.Length);
642-
_text.ScrollToLine(_text.TextArea.Caret.Line);
643-
}
644-
else
645-
_status.Text = "Can't find '" + toFind + "'.";
646-
}
647-
648-
private bool IsWholeWord(string text, int start, int length)
649-
{
650-
if (!(_findMatchWholeWord.IsChecked ?? false))
651-
return true;
652-
653-
var end = start + length - 1;
654-
655-
if (end < text.Length)
656-
if (char.IsLetterOrDigit(text[end]) || text[end] == '_')
657-
if (char.IsLetterOrDigit(text[end + 1]) || text[end + 1] == '_')
658-
return false;
659-
660-
if (start > 0)
661-
if (char.IsLetterOrDigit(text[start]) || text[start] == '_')
662-
if (char.IsLetterOrDigit(text[start - 1]) || text[start - 1] == '_')
663-
return false;
664-
665-
return true;
666-
}
667-
668-
private StringComparison GetMatchCaseOption()
669-
{
670-
return _findMatchCase.IsChecked ?? false ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase;
671-
}
672-
673568
private void _tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
674569
{
675570
UpdateInfo();
@@ -1240,94 +1135,6 @@ private static bool IsSplicable(ParseResult parseResult)
12401135
{
12411136
return parseResult.RuleParser.Descriptor.Grammar.IsSplicable;
12421137
}
1243-
1244-
private void FindNext_Executed_Selected(object sender, ExecutedRoutedEventArgs e)
1245-
{
1246-
FindSelected(next: true);
1247-
}
1248-
1249-
private void FindPrev_Executed_Selected(object sender, ExecutedRoutedEventArgs e)
1250-
{
1251-
FindSelected(next: false);
1252-
}
1253-
1254-
private void FindSelected(bool next)
1255-
{
1256-
if (_text.SelectionLength > 0)
1257-
{
1258-
_findText.Text = _text.SelectedText;
1259-
1260-
if (next)
1261-
FindNext();
1262-
else
1263-
FindPrev();
1264-
}
1265-
else
1266-
{
1267-
var line = _text.Document.Lines[_text.TextArea.Caret.Line - 1];
1268-
var text = _text.Document.GetText(line.Offset, line.Length);
1269-
var startIndex = Math.Min(_text.TextArea.Caret.Column - 1, text.Length - 1);
1270-
var firstCh = text[startIndex];
1271-
int patternStartIndex;
1272-
string searchPattern = IsIdentifier(firstCh)
1273-
? ExtractIdentifier(text, startIndex, out patternStartIndex)
1274-
: ExtractNotEmpty(text, startIndex, out patternStartIndex);
1275-
1276-
if (!string.IsNullOrWhiteSpace(searchPattern))
1277-
{
1278-
_findText.Text = searchPattern;
1279-
_text.Select(line.Offset + patternStartIndex, searchPattern.Length);
1280-
1281-
if (next)
1282-
FindNext();
1283-
else
1284-
{
1285-
_text.TextArea.Caret.Column = patternStartIndex + 1;
1286-
FindPrev();
1287-
}
1288-
}
1289-
}
1290-
}
1291-
1292-
public static string ExtractNotEmpty(string text, int startIndex, out int patternStartIndex)
1293-
{
1294-
return ExtractString(text, startIndex, char.IsWhiteSpace, out patternStartIndex);
1295-
}
1296-
1297-
public static string ExtractIdentifier(string text, int startIndex, out int patternStartIndex)
1298-
{
1299-
return ExtractString(text, startIndex, ch => !IsIdentifier(ch), out patternStartIndex);
1300-
}
1301-
1302-
public static string ExtractString(string text, int startIndex, Func<char, bool> predicate, out int patternStartIndex)
1303-
{
1304-
int i = startIndex;
1305-
for (; i > 0; i--)
1306-
{
1307-
var ch = text[i];
1308-
if (predicate(ch))
1309-
{
1310-
i++;
1311-
break;
1312-
}
1313-
}
1314-
1315-
int j = startIndex;
1316-
for (; j < text.Length; j++)
1317-
{
1318-
var ch = text[j];
1319-
if (predicate(ch))
1320-
break;
1321-
}
1322-
1323-
patternStartIndex = i;
1324-
return text.Substring(i, j - i);
1325-
}
1326-
1327-
private static bool IsIdentifier(char ch)
1328-
{
1329-
return char.IsLetterOrDigit(ch) || ch == '_';
1330-
}
13311138

13321139
private void _control_KeyDown_resize(object sender, KeyEventArgs e)
13331140
{

Nitra.Visualizer/Nitra.Visualizer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<DependentUpon>AddTest.xaml</DependentUpon>
118118
<SubType>Code</SubType>
119119
</Compile>
120+
<Compile Include="NitraSearchInputHandler.cs" />
120121
<Compile Include="Rendering\ReflectionStructColorConverter.cs" />
121122
<Compile Include="Rendering\GrammarRenderer.cs" />
122123
<Compile Include="TreeViewItemBehavior.cs" />

0 commit comments

Comments
 (0)