Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line sorting to string utils #835

Merged
merged 6 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/dev/impl/DevToys/LanguageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,11 @@ public class StringUtilitiesStrings : ObservableObject
/// </summary>
public string Convert => _resources.GetString("Convert");

/// <summary>
/// Gets the resource Sort.
/// </summary>
public string Sort => _resources.GetString("Sort");

/// <summary>
/// Gets the resource MenuDisplayName.
/// </summary>
Expand All @@ -3015,6 +3020,36 @@ public class StringUtilitiesStrings : ObservableObject
/// </summary>
public string KebabCase => _resources.GetString("KebabCase");

/// <summary>
/// Gets the resource Alphabetize.
/// </summary>
public string Alphabetize => _resources.GetString("Alphabetize");

/// <summary>
/// Gets the resource ReverseAlphabetize.
/// </summary>
public string ReverseAlphabetize => _resources.GetString("ReverseAlphabetize");

/// <summary>
/// Gets the resource AlphabetizeLast.
/// </summary>
public string AlphabetizeLast => _resources.GetString("AlphabetizeLast");

/// <summary>
/// Gets the resource ReverseAlphabetizeLast.
/// </summary>
public string ReverseAlphabetizeLast => _resources.GetString("ReverseAlphabetizeLast");

/// <summary>
/// Gets the resource Reverse.
/// </summary>
public string Reverse => _resources.GetString("Reverse");

/// <summary>
/// Gets the resource Randomize.
/// </summary>
public string Randomize => _resources.GetString("Randomize");

/// <summary>
/// Gets the resource Line.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/dev/impl/DevToys/Strings/en-US/StringUtilities.resw
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<data name="Convert" xml:space="preserve">
<value>Convert</value>
</data>
<data name="Sort" xml:space="preserve">
<value>Sort Lines</value>
</data>
<data name="MenuDisplayName" xml:space="preserve">
<value>Inspector &amp; Case Converter</value>
</data>
Expand All @@ -156,6 +159,24 @@
<data name="KebabCase" xml:space="preserve">
<value>kebab-case</value>
</data>
<data name="Alphabetize" xml:space="preserve">
<value>Alphabetize</value>
</data>
<data name="ReverseAlphabetize" xml:space="preserve">
<value>Reverse alphabetize</value>
</data>
<data name="AlphabetizeLast" xml:space="preserve">
<value>Alphabetize by last word</value>
</data>
<data name="ReverseAlphabetizeLast" xml:space="preserve">
<value>Reverse alphabetize by last word</value>
</data>
<data name="Reverse" xml:space="preserve">
<value>Reverse</value>
</data>
<data name="Randomize" xml:space="preserve">
<value>Randomize</value>
</data>
<data name="Line" xml:space="preserve">
<value>Line:</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed class StringUtilitiesToolViewModel : QueueWorkerViewModelBase<stri
private static readonly object _lockObject = new();

private readonly IMarketingService _marketingService;
private readonly Random _random;

private bool _toolSuccessfullyWorked;
private bool _forbidBackup;
Expand Down Expand Up @@ -143,6 +144,7 @@ internal string? CharacterDistribution
public StringUtilitiesToolViewModel(IMarketingService marketingService)
{
_marketingService = marketingService;
_random = new Random();
OriginalCaseCommand = new RelayCommand(ExecuteOriginalCaseCommand, CanExecuteOriginalCaseCommand);
SentenceCaseCommand = new RelayCommand(ExecuteSentenceCaseCommand);
LowerCaseCommand = new RelayCommand(ExecuteLowerCaseCommand);
Expand All @@ -157,6 +159,12 @@ public StringUtilitiesToolViewModel(IMarketingService marketingService)
TrainCaseCommand = new RelayCommand(ExecuteTrainCaseCommand);
AlternatingCaseCommand = new RelayCommand(ExecuteAlternatingCaseCommand);
InverseCaseCommand = new RelayCommand(ExecuteInverseCaseCommand);
AlphabetizeCommand = new RelayCommand(ExecuteAlphabetizeCommand);
ReverseAlphabetizeCommand = new RelayCommand(ExecuteReverseAlphabetizeCommand);
AlphabetizeLastCommand = new RelayCommand(ExecuteAlphabetizeLastCommand);
ReverseAlphabetizeLastCommand = new RelayCommand(ExecuteReverseAlphabetizeLastCommand);
ReverseCommand = new RelayCommand(ExecuteReverseCommand);
RandomizeCommand = new RelayCommand(ExecuteRandomizeCommand);

QueueSelectionStatisticCalculation();
QueueTextStatisticCalculation();
Expand Down Expand Up @@ -626,6 +634,189 @@ private void ExecuteInverseCaseCommand()

#endregion

#region AlphabetizeCommand

public IRelayCommand AlphabetizeCommand { get; }

private void ExecuteAlphabetizeCommand()
{
string? text = _textBackup;
if (text is null)
veler marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

var lines = text.Split('\r').ToList();
lines.Sort(StringComparer.CurrentCulture);
text = string.Join('\r', lines);

lock (_lockObject)
micahmo marked this conversation as resolved.
Show resolved Hide resolved
{
_forbidBackup = true;
Text = text;
_forbidBackup = false;
}
}

#endregion

#region ReverseAlphabetizeCommand

public IRelayCommand ReverseAlphabetizeCommand { get; }

private void ExecuteReverseAlphabetizeCommand()
{
string? text = _textBackup;
if (text is null)
{
return;
}

var lines = text.Split('\r').ToList();
lines.Sort(StringComparer.CurrentCulture);
micahmo marked this conversation as resolved.
Show resolved Hide resolved
lines.Reverse();
text = string.Join('\r', lines);

lock (_lockObject)
{
_forbidBackup = true;
Text = text;
_forbidBackup = false;
}
}

#endregion

#region AlphabetizeLastCommand

public IRelayCommand AlphabetizeLastCommand { get; }

private void ExecuteAlphabetizeLastCommand()
{
string? text = _textBackup;
if (text is null)
{
return;
}

var lines = text.Split('\r').ToList();
lines.Sort((line1, line2) =>
micahmo marked this conversation as resolved.
Show resolved Hide resolved
{
string? line1LastWord = line1.Split(' ', ',').LastOrDefault();
string? line2LastWord = line2.Split(' ', ',').LastOrDefault();

if (string.Equals(line1LastWord, line2LastWord, StringComparison.CurrentCulture))
{
return 0;
}

var newList = new List<string> { line1LastWord, line2LastWord };
newList.Sort(StringComparer.CurrentCulture);
return newList.IndexOf(line1LastWord) == 0 ? -1 : 1;
});
text = string.Join('\r', lines);

lock (_lockObject)
{
_forbidBackup = true;
Text = text;
_forbidBackup = false;
}
}

#endregion

#region ReverseAlphabetizeLastCommand

public IRelayCommand ReverseAlphabetizeLastCommand { get; }

private void ExecuteReverseAlphabetizeLastCommand()
{
string? text = _textBackup;
if (text is null)
{
return;
}

var lines = text.Split('\r').ToList();
lines.Sort((line1, line2) =>
{
string? line1LastWord = line1.Split(' ', ',').LastOrDefault();
micahmo marked this conversation as resolved.
Show resolved Hide resolved
string? line2LastWord = line2.Split(' ', ',').LastOrDefault();

if (string.Equals(line1LastWord, line2LastWord, StringComparison.CurrentCulture))
{
return 0;
}

var newList = new List<string> { line1LastWord, line2LastWord };
newList.Sort(StringComparer.CurrentCulture);
return newList.IndexOf(line1LastWord) == 0 ? -1 : 1;
});
lines.Reverse();
text = string.Join('\r', lines);

lock (_lockObject)
{
_forbidBackup = true;
Text = text;
_forbidBackup = false;
}
}

#endregion

#region ReverseCommand

public IRelayCommand ReverseCommand { get; }

private void ExecuteReverseCommand()
{
string? text = _textBackup;
if (text is null)
{
return;
}

var lines = text.Split('\r').ToList();
lines.Reverse();
text = string.Join('\r', lines);

lock (_lockObject)
{
_forbidBackup = true;
Text = text;
_forbidBackup = false;
}
}

#endregion

#region RandomizeCommand

public IRelayCommand RandomizeCommand { get; }

private void ExecuteRandomizeCommand()
{
string? text = _textBackup;
if (text is null)
{
return;
}

var lines = text.Split('\r').OrderBy(_ => _random.Next());
text = string.Join('\r', lines);

lock (_lockObject)
{
_forbidBackup = true;
Text = text;
_forbidBackup = false;
}
}

#endregion

private string SnakeConstantKebabCobolCaseConverter(string text, char spaceReplacement, bool isUpperCase)
{
var snakeCaseStringBuilder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</Grid.ChildrenTransitions>

<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
Expand All @@ -52,7 +53,7 @@
</Grid.ColumnDefinitions>

<controls:CustomTextBox
Grid.Row="1"
Grid.Row="2"
Grid.Column="0"
Header="{x:Bind ViewModel.Strings.String}"
AcceptsReturn="True"
Expand Down Expand Up @@ -86,10 +87,27 @@
</toolkit:WrapPanel>
</StackPanel>

<StackPanel
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Spacing="4">
<TextBlock Style="{StaticResource SubTitleTextBlock}" Text="{x:Bind ViewModel.Strings.Sort}" />

<toolkit:WrapPanel VerticalSpacing="4" HorizontalSpacing="4">
<Button Content="{x:Bind ViewModel.Strings.Alphabetize}" Command="{x:Bind ViewModel.AlphabetizeCommand}"/>
<Button Content="{x:Bind ViewModel.Strings.ReverseAlphabetize}" Command="{x:Bind ViewModel.ReverseAlphabetizeCommand}"/>
<Button Content="{x:Bind ViewModel.Strings.AlphabetizeLast}" Command="{x:Bind ViewModel.AlphabetizeLastCommand}"/>
<Button Content="{x:Bind ViewModel.Strings.ReverseAlphabetizeLast}" Command="{x:Bind ViewModel.ReverseAlphabetizeLastCommand}"/>
<Button Content="{x:Bind ViewModel.Strings.Reverse}" Command="{x:Bind ViewModel.ReverseCommand}"/>
<Button Content="{x:Bind ViewModel.Strings.Randomize}" Command="{x:Bind ViewModel.RandomizeCommand}"/>
</toolkit:WrapPanel>
</StackPanel>

<Grid
x:Name="StatisticsGrid"
Style="{StaticResource CardStyle}"
Grid.Row="1"
Grid.Row="2"
Grid.Column="1"
Margin="0,42,0,0">
<StackPanel
Expand Down
Loading