Skip to content

Commit

Permalink
Completed Find & Fix Selection Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chenguanzhou committed Feb 18, 2016
1 parent 4a202a2 commit 76e86c4
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 38 deletions.
40 changes: 20 additions & 20 deletions MarkDownEditor/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,25 @@
</Controls:MetroContentControl>
<Grid Grid.Row="1" Margin="10">
<view:MvvmTextEditor
SyntaxHighlighting="MarkDown"
Document="{Binding SourceCode}"
SelectionStart="{Binding SelectionStart,Mode=TwoWay}"
SelectionLength="{Binding SelectionLength,Mode=TwoWay}"
CaretOffset="{Binding CaretOffset,Mode=TwoWay}"
ScrollOffsetRatio="{Binding ScrollOffsetRatio,Mode=TwoWay}"
CanUndo="{Binding CanUndo,Mode=OneWayToSource}"
CanRedo="{Binding CanRedo,Mode=OneWayToSource}"
ShowLineNumbers="{Binding SettingsViewModel.ShowLineNumbers}"
WordWrap="{Binding SettingsViewModel.WordWrap}"
ShowTabs="{Binding SettingsViewModel.ShowTabs}"
ShowSpaces="{Binding SettingsViewModel.ShowSpaces}"
ShowEndOfLine="{Binding SettingsViewModel.ShowEndOfLine}"
ShowColumnRuler="{Binding SettingsViewModel.ShowColumnRuler}"
RulerPosition="{Binding SettingsViewModel.RulerPosition}"
HighlightCurrentLine="{Binding SettingsViewModel.HighlightCurrentLine}"
LineNumbersForeground="{DynamicResource AccentColorBrush}"
FontFamily="{Binding SettingsViewModel.EditorFont}"
FontSize="{Binding SettingsViewModel.EditorFontSize}">
SyntaxHighlighting="MarkDown"
Document="{Binding SourceCode}"
SelectionStart="{Binding SelectionStart,Mode=TwoWay}"
SelectionLength="{Binding SelectionLength,Mode=TwoWay}"
CaretOffset="{Binding CaretOffset,Mode=TwoWay}"
ScrollOffsetRatio="{Binding ScrollOffsetRatio,Mode=OneWayToSource}"
CanUndo="{Binding CanUndo,Mode=OneWayToSource}"
CanRedo="{Binding CanRedo,Mode=OneWayToSource}"
ShowLineNumbers="{Binding SettingsViewModel.ShowLineNumbers}"
WordWrap="{Binding SettingsViewModel.WordWrap}"
ShowTabs="{Binding SettingsViewModel.ShowTabs}"
ShowSpaces="{Binding SettingsViewModel.ShowSpaces}"
ShowEndOfLine="{Binding SettingsViewModel.ShowEndOfLine}"
ShowColumnRuler="{Binding SettingsViewModel.ShowColumnRuler}"
RulerPosition="{Binding SettingsViewModel.RulerPosition}"
HighlightCurrentLine="{Binding SettingsViewModel.HighlightCurrentLine}"
LineNumbersForeground="{DynamicResource AccentColorBrush}"
FontFamily="{Binding SettingsViewModel.EditorFont}"
FontSize="{Binding SettingsViewModel.EditorFontSize}">
<view:MvvmTextEditor.ContextMenu>
<ContextMenu>
<MenuItem Header="{x:Static p:Resources.Undo}" Command="Undo"></MenuItem>
Expand Down Expand Up @@ -154,7 +154,7 @@
<view:PreviewToolBarControl Margin="5" HorizontalAlignment="Right">
</view:PreviewToolBarControl>
<view:MvvmChromiumWebBrowser
Grid.Row="1" Focusable="False" Margin="10"
Grid.Row="1" Margin="10"
ShouldReload="{Binding ShouldReload,Mode=TwoWay}"
ScrollOffsetRatio="{Binding ScrollOffsetRatio,Mode=TwoWay}"
Address="{Binding PreviewSource}" IsBrowserInitialized="{Binding IsBrowserInitialized,Mode=OneWayToSource}"></view:MvvmChromiumWebBrowser>
Expand Down
26 changes: 20 additions & 6 deletions MarkDownEditor/View/MvvmTextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public double ScrollOffsetRatio
DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.SelectionLength = (int)args.NewValue;
TextEditor editor = (TextEditor)obj;
if (editor.SelectionLength == (int)args.NewValue)
return;
editor.SelectionLength = (int)args.NewValue;
}));

/// <summary>
Expand All @@ -126,7 +128,12 @@ public double ScrollOffsetRatio
public new int SelectionLength
{
get { return base.SelectionLength; }
set { SetValue(SelectionLengthProperty, value); }
set
{
if ((int)GetValue(SelectionLengthProperty) == value)
return;
SetValue(SelectionLengthProperty, value);
}
}

/// <summary>
Expand All @@ -136,8 +143,10 @@ public double ScrollOffsetRatio
DependencyProperty.Register("SelectionStart", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.SelectionStart = (int)args.NewValue;
TextEditor editor = (TextEditor)obj;
if (editor.SelectionStart == (int)args.NewValue)
return;
((TextEditor)obj).SelectionStart = (int)args.NewValue;
}));

/// <summary>
Expand All @@ -146,7 +155,12 @@ public double ScrollOffsetRatio
public new int SelectionStart
{
get { return base.SelectionStart; }
set { SetValue(SelectionStartProperty, value); }
set
{
if ((int)GetValue(SelectionStartProperty) == value)
return;
SetValue(SelectionStartProperty,value);
}
}
#endregion // Selection.

Expand Down
82 changes: 70 additions & 12 deletions MarkDownEditor/ViewModel/FindReplaceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public bool IsMatchCase
return;
isMatchCase = value;
RaisePropertyChanged("IsMatchCase");
UpdateCommandsState();
}
}

Expand All @@ -134,6 +135,7 @@ public bool IsMatchWholeWord
return;
isMatchWholeWord = value;
RaisePropertyChanged("IsMatchWholeWord");
UpdateCommandsState();
}
}

Expand All @@ -147,6 +149,7 @@ public bool UseRegExp
return;
useRegExp = value;
RaisePropertyChanged("UseRegExp");
UpdateCommandsState();
}
}

Expand All @@ -160,6 +163,7 @@ public bool UseWildcards
return;
useWildcards = value;
RaisePropertyChanged("UseWildcards");
UpdateCommandsState();
}
}

Expand Down Expand Up @@ -190,7 +194,31 @@ public string ReplaceText
UpdateCommandsState();
}
}

public MatchCollection MatchedCollection
{
get; set;
}

public int currentMatch = 0;
public int CurrentMatch
{
get { return currentMatch; }
set
{
currentMatch = value;
if (MatchedCollection == null || MatchedCollection.Count == 0)
return;

IsFindPreviousEnabled = CurrentMatch > 0;
IsFindNextEnabled = CurrentMatch < MatchedCollection.Count - 1;

var match = MatchedCollection[currentMatch];
ViewModelLocator.Main.SelectionStart = match.Index;
ViewModelLocator.Main.SelectionLength = match.Length;
}
}

#region Commands
public ICommand SwitchFindReplaceCommand => new RelayCommand(() => ShowFindReplaceControl = !ShowFindReplaceControl);
public ICommand ShowFindReplaceCommand => new RelayCommand(() => ShowFindReplaceControl = true);
Expand All @@ -199,12 +227,12 @@ public string ReplaceText

public ICommand FindPreviousCommand => new RelayCommand(() =>
{
CurrentMatch--;
});

public ICommand FindNextCommand => new RelayCommand(() =>
{
CurrentMatch++;
});

public ICommand ReplaceNextCommand => new RelayCommand(() =>
Expand All @@ -223,30 +251,60 @@ public string ReplaceText
private void UpdateCommandsState()
{
if (string.IsNullOrEmpty(SearchText))
{
MatchedCollection = null;
ClearFindState();
}
else
{
var mainVM = ViewModelLocator.Main;
RegexOptions options = RegexOptions.None;
if (!IsMatchCase)
options |= RegexOptions.IgnoreCase;
Regex regex = new Regex(SearchText, options);
var collections = regex.Matches(mainVM.SourceCode.Text);
if (collections.Count == 0)//nothing matched
Regex regex = GetRegEx(SearchText);
MatchedCollection = regex.Matches(ViewModelLocator.Main.SourceCode.Text);

if (MatchedCollection.Count == 0)//nothing matched
{
ClearFindState();
ViewModelLocator.Main.SelectionLength = 0;
return;
}

else
{
CurrentMatch = 0;
IsFindPreviousEnabled = false;
IsFindNextEnabled = MatchedCollection.Count > 1;
IsReplaceNextEnabled = !string.IsNullOrEmpty(ReplaceText);
IsReplaceAllEnabled = !string.IsNullOrEmpty(ReplaceText);
}
}
}


private Regex GetRegEx(string textToFind)
{
RegexOptions options = RegexOptions.None;
if (!IsMatchCase)
options |= RegexOptions.IgnoreCase;

if (UseRegExp)
{
return new Regex(textToFind, options);
}
else
{
string pattern = Regex.Escape(textToFind);
if (UseWildcards)
pattern = pattern.Replace("\\*", ".*").Replace("\\?", ".");
if (IsMatchWholeWord)
pattern = "\\b" + pattern + "\\b";
return new Regex(pattern, options);
}
}

private void ClearFindState()
{
IsFindPreviousEnabled = false;
IsFindNextEnabled = false;
IsReplaceNextEnabled = false;
IsReplaceAllEnabled = false;
IsReplaceAllEnabled = false;
CurrentMatch = 0;
}
#endregion //Functions
}
Expand Down

0 comments on commit 76e86c4

Please sign in to comment.