Skip to content

Commit

Permalink
Feature: Diff List sorting options
Browse files Browse the repository at this point in the history
Defaults to file path sorting as it was pre 3.0 and was changed by accident.
Context menu in a diff list will provide you with the ability to select the sorting method. Changing one will affect all others and is persisted across processes.
Added ImageIndexListSorter to sort by change type according to the image they have then by file path.
Added GitItemStatusFileExtensionComparer to sort by the file extension then by the file path.
  • Loading branch information
glen-nicol committed Feb 25, 2019
1 parent dd029a5 commit 0b80258
Show file tree
Hide file tree
Showing 16 changed files with 666 additions and 0 deletions.
46 changes: 46 additions & 0 deletions GitCommands/DiffListSortService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GitCommands
{
public class DiffListSortService : IDiffListSortService
{
private static readonly Lazy<DiffListSortService> _lazyDiffListSorting = new Lazy<DiffListSortService>(() => new DiffListSortService());
public static DiffListSortService Instance => _lazyDiffListSorting.Value;
private DiffListSortType _diffListSorting;
public event EventHandler DiffListSortingChanged;

public DiffListSortType DiffListSorting
{
get { return _diffListSorting; }
set
{
var previous = _diffListSorting;
if (previous != value)
{
_diffListSorting = value;
AppSettings.DiffListSorting = value;
OnDiffListSortingChanged();
}
}
}

public DiffListSortService()
{
DiffListSorting = GetSettingValueOrDefault();
}

private DiffListSortType GetSettingValueOrDefault()
{
return AppSettings.DiffListSorting;
}

protected void OnDiffListSortingChanged()
{
DiffListSortingChanged?.Invoke(this, EventArgs.Empty);
}
}
}
55 changes: 55 additions & 0 deletions GitCommands/Git/GitItemStatusFileExtensionComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GitCommands.Git
{
/// <summary>
/// Compares the file extension of <see cref="GitItemStatus.Name"/> and then by <see cref="GitItemStatus.CompareTo(GitItemStatus)"/>.
/// </summary>
public class GitItemStatusFileExtensionComparer : Comparer<GitItemStatus>
{
public override int Compare(GitItemStatus x, GitItemStatus y)
{
if (ReferenceEquals(x, y))
{
return 0;
}

if (x == null)
{
return -1;
}

if (y == null)
{
return 1;
}

var lhsPath = GetPrimarySortingPath(x);
var rhsPath = GetPrimarySortingPath(y);
var lhsExt = Path.GetExtension(lhsPath);
var rhsExt = Path.GetExtension(rhsPath);

var comparisonResult = StringComparer.InvariantCulture.Compare(lhsExt, rhsExt);
if (comparisonResult == 0)
{
// originally used Comparer<GitItemStatus>.Default.Compare(x, y) but this would
// produce incorrect results when the oldName was the only available path on an item.
return StringComparer.InvariantCulture.Compare(lhsPath, rhsPath);
}

return comparisonResult;
}

private static string GetPrimarySortingPath(GitItemStatus itemStatus)
{
return !string.IsNullOrEmpty(itemStatus.Name)
? itemStatus.Name
: itemStatus.OldName;
}
}
}
3 changes: 3 additions & 0 deletions GitCommands/GitCommands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="DateTimeExtensions.cs" />
<Compile Include="DateTimeUtils.cs" />
<Compile Include="DeconstructionExtensions.cs" />
<Compile Include="DiffListSortService.cs" />
<Compile Include="EnvironmentAbstraction.cs" />
<Compile Include="EnvironmentPathsProvider.cs" />
<Compile Include="ExceptionUtils.cs" />
Expand Down Expand Up @@ -101,6 +102,7 @@
<Compile Include="Git\GitBranchNameOptions.cs" />
<Compile Include="Git\Extensions\GitRevisionExtensions.cs" />
<Compile Include="Git\Executable.cs" />
<Compile Include="Git\GitItemStatusFileExtensionComparer.cs" />
<Compile Include="Git\GitModuleEventArgs.cs" />
<Compile Include="Git\GitRevisionTester.cs" />
<Compile Include="Git\GitRefName.cs" />
Expand All @@ -114,6 +116,7 @@
<Compile Include="Git\Tag\GitTagController.cs" />
<Compile Include="Git\Tag\TagOperation.cs" />
<Compile Include="Git\Tag\TagOperationExtensions.cs" />
<Compile Include="IDiffListSortService.cs" />
<Compile Include="Plink.cs" />
<Compile Include="Remotes\GitRemoteManager.cs" />
<Compile Include="Remotes\RepoNameExtractor.cs" />
Expand Down
52 changes: 52 additions & 0 deletions GitCommands/IDiffListSortService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GitCommands
{
public enum DiffListSortType
{
/// <summary>
/// Sorts by file path alphanumerically
/// </summary>
FilePath,

/// <summary>
/// Sorts by file extension then by path
/// </summary>
FileExtension,

/// <summary>
/// Sorts by git change type. Addition, Deletions, edits, etc. then by path
/// </summary>
FileStatus
}

public interface IDiffListSortService
{
DiffListSortType DiffListSorting { get; set; }

event EventHandler DiffListSortingChanged;
}

public static class DiffListSortServiceExtensions
{
/// <summary>
/// Provides the <see cref="IDiffListSortService.DiffListSorting"/> immediately and then an element for each <see cref="IDiffListSortService.DiffListSortingChanged"/>.
/// </summary>
/// <param name="diffListSortService">The diff list service</param>
/// <returns>A hot stream with one immediate cold element.</returns>
public static IObservable<DiffListSortType> CurrentAndFutureSorting(this IDiffListSortService diffListSortService)
{
return Observable.Return(diffListSortService.DiffListSorting)
.Concat(Observable.FromEventPattern(
h => diffListSortService.DiffListSortingChanged += h,
h => diffListSortService.DiffListSortingChanged -= h)
.Select(_ => diffListSortService.DiffListSorting))
.DistinctUntilChanged();
}
}
}
6 changes: 6 additions & 0 deletions GitCommands/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,12 @@ public static bool UseConsoleEmulatorForCommands
set => SetBool("UseConsoleEmulatorForCommands", value);
}

public static DiffListSortType DiffListSorting
{
get => GetEnum("DiffListSortType", DiffListSortType.FilePath);
set => SetEnum("DiffListSortType", value);
}

public static string GetGitExtensionsFullPath()
{
return Application.ExecutablePath;
Expand Down
3 changes: 3 additions & 0 deletions GitUI/GitUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@
<Compile Include="UserControls\RevisionGrid\RevisionGridRefRenderer.cs" />
<Compile Include="UserControls\RevisionGrid\SuperProjectInfo.cs" />
<Compile Include="UserControls\RevisionGrid\VisibleRowRange.cs" />
<Compile Include="UserControls\SortDiffListContextMenuItem.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="UserControls\TextEventArgs.cs" />
<Compile Include="UserControls\RepoStateVisualiser.cs" />
<Compile Include="CommandsDialogs\BrowseDialog\GitStatusMonitor.cs" />
Expand Down
10 changes: 10 additions & 0 deletions GitUI/Properties/Images.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions GitUI/Properties/Images.resx
Original file line number Diff line number Diff line change
Expand Up @@ -799,4 +799,7 @@
<data name="DocumentTree" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Icons\DocumentTree.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="SortBy" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Icons\SortBy.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added GitUI/Resources/Icons/SortBy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0b80258

Please sign in to comment.