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 whitespace ignoring #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 54 additions & 2 deletions GitDiffMargin.Unit.Tests/Git/HunkRangeInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace GitDiffMargin.Unit.Tests.Git
{
// ReSharper disable InconsistentNaming
// ReSharper disable InconsistentNaming

[TestFixture(0)]
public class HunkRangeInfoTests
Expand Down Expand Up @@ -111,7 +111,59 @@ public void IsDeletion_3DeletedLines_ExpectTrue()
//Assert
isDeletion.ShouldBe(true);
}

[Test]
public void IsWhiteSpaceChange_NonPrintableCharsAddedOrRemoved_ExpectTrue()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3", _contextLines), new HunkRange("+6,0", _contextLines), new [] { "-a test text", "-a test text\r\n", "+a t e s t t e x t", "+a\ttest\ttext" });

//Act
var isWhiteSpaceChange = hunkRangeInfo.IsWhiteSpaceChange;

//Assert
isWhiteSpaceChange.ShouldBe(true);
}

[Test]
public void IsWhiteSpaceChange_PrintableCharsAddedOrRemoved_ExpectFalse()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3", _contextLines), new HunkRange("+6,0", _contextLines), new [] { "-a test text", "-a test text\r\n", "+a test text", "+a\ttest\ttextchanged" });

//Act
var isWhiteSpaceChange = hunkRangeInfo.IsWhiteSpaceChange;

//Assert
isWhiteSpaceChange.ShouldBe(false);
}

[Test]
public void IsWhiteSpaceChange_EmptyLineRemoved_ExpectTrue()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3", _contextLines), new HunkRange("+6,0", _contextLines), new[] { "- "});

//Act
var isWhiteSpaceChange = hunkRangeInfo.IsWhiteSpaceChange;

//Assert
isWhiteSpaceChange.ShouldBe(true);
}

[Test]
public void IsWhiteSpaceChange_EmptyLineAdded_ExpectTrue()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3", _contextLines), new HunkRange("+6,0", _contextLines), new[] { "+ " });

//Act
var isWhiteSpaceChange = hunkRangeInfo.IsWhiteSpaceChange;

//Assert
isWhiteSpaceChange.ShouldBe(true);
}
}

// ReSharper restore InconsistentNaming
// ReSharper restore InconsistentNaming
}
5 changes: 5 additions & 0 deletions GitDiffMargin/Core/DiffUpdateBackgroundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public override string Name
}
}

public void Restart()
{
MarkDirty(true);
}

protected override void ReParseImpl()
{
try
Expand Down
2 changes: 2 additions & 0 deletions GitDiffMargin/Core/IMarginCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ internal interface IMarginCore
double EditorChangeLeft { get; }
double EditorChangeWidth { get; }
double ScrollChangeWidth { get; }
bool IgnoreWhiteSpaces { get; }
void MoveToChange(int lineNumber);
bool RollBack(HunkRangeInfo hunkRangeInfo);
ITextDocument GetTextDocument();
void ToggleIgnoreWhiteSpace();
}
}
11 changes: 10 additions & 1 deletion GitDiffMargin/Core/MarginCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public double ScrollChangeWidth

public event EventHandler<HunksChangedEventArgs> HunksChanged;

public bool IgnoreWhiteSpaces { get; private set; }

public void MoveToChange(int lineNumber)
{
var diffLine = _textView.TextSnapshot.GetLineFromLineNumber(lineNumber);
Expand All @@ -206,7 +208,7 @@ private void CheckBeginInvokeOnUi(Action action)
else
{
_textView.VisualElement.Dispatcher.BeginInvoke(action);
}
}
}

private void HandleFormatMappingChanged(object sender, FormatItemsEventArgs e)
Expand Down Expand Up @@ -318,6 +320,13 @@ public ITextDocument GetTextDocument()
return document;
}

public void ToggleIgnoreWhiteSpace()
{
IgnoreWhiteSpaces = !IgnoreWhiteSpaces;

_parser.Restart();
}

private void HandleParseComplete(object sender, ParseResultEventArgs e)
{
var diffResult = e as DiffParseResultEventArgs;
Expand Down
53 changes: 38 additions & 15 deletions GitDiffMargin/Git/HunkRangeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region using

using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -9,37 +10,59 @@ namespace GitDiffMargin.Git
{
public class HunkRangeInfo
{
private List<string> DiffLines { get; set; }
private readonly List<string> _diffLines;
private readonly Lazy<bool> _isWhiteSpaceChange;

public HunkRangeInfo(HunkRange originaleHunkRange, HunkRange newHunkRange, IEnumerable<string> diffLines)
: this(originaleHunkRange, newHunkRange, diffLines, false)
{
}

public HunkRangeInfo(HunkRange originaleHunkRange, HunkRange newHunkRange, IEnumerable<string> diffLines, bool suppressRollback)
public HunkRangeInfo(HunkRange originaleHunkRange, HunkRange newHunkRange, IEnumerable<string> diffLines,
bool suppressRollback = false)
{
OriginalHunkRange = originaleHunkRange;
NewHunkRange = newHunkRange;
DiffLines = diffLines.ToList();
_diffLines = diffLines.ToList();
SuppressRollback = suppressRollback;
IsAddition = DiffLines.All(s => s.StartsWith("+") || s.StartsWith("\\") || string.IsNullOrWhiteSpace(s));
IsDeletion = DiffLines.All(s => s.StartsWith("-") || s.StartsWith("\\") || string.IsNullOrWhiteSpace(s));

IsAddition = _diffLines.All(s => s.StartsWith("+") || s.StartsWith("\\") || string.IsNullOrWhiteSpace(s));
IsDeletion = _diffLines.All(s => s.StartsWith("-") || s.StartsWith("\\") || string.IsNullOrWhiteSpace(s));
IsModification = !IsAddition && !IsDeletion;

if (IsDeletion || IsModification)
{
OriginalText = DiffLines.Where(s => s.StartsWith("-")).Select(s => s.Remove(0, 1).TrimEnd('\n').TrimEnd('\r')).ToList();
OriginalText = _diffLines.Where(s => s.StartsWith("-"))
.Select(s => s.Remove(0, 1).TrimEnd('\n').TrimEnd('\r'))
.ToList();
}
else
{
OriginalText = new List<string>();
}

_isWhiteSpaceChange = new Lazy<bool>(IsWhiteSpaceChangeFunc);
}

public HunkRange OriginalHunkRange { get; private set; }
public HunkRange NewHunkRange { get; private set; }
public List<string> OriginalText { get; private set; }
public bool SuppressRollback { get; private set; }

public bool IsAddition { get; private set; }
public bool IsModification { get; private set; }
public bool IsDeletion { get; private set; }
public bool IsAddition { get; }
public bool IsModification { get; }
public bool IsDeletion { get; }
public bool IsWhiteSpaceChange => _isWhiteSpaceChange.Value;

private bool IsWhiteSpaceChangeFunc()
{
var oldText = TrimAll(string.Join(string.Empty, OriginalText));

var newText = TrimAll(string.Join(string.Empty, _diffLines.Where(s => s.StartsWith("+"))
.Select(s => s.Remove(0, 1))
.ToList()));

return oldText == newText;
}

private static string TrimAll(string value)
{
return new string(value.Where(t => !char.IsWhiteSpace(t)).ToArray());
}
}
}
1 change: 1 addition & 0 deletions GitDiffMargin/GitDiffMargin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
<Link>LICENSE.md</Link>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="Resources\Ignore_whitespaces.png" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
16 changes: 15 additions & 1 deletion GitDiffMargin/GitDiffMargin.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@
<ToolTipText>Copy Old Text</ToolTipText>
</Strings>
</Button>

<Button guid="GitDiffMarginCommand" id="IgnoreWhitespaces" priority="0x106" type="Button">
<Parent guid="GitDiffMarginCommand" id="GitDiffToolbarGroup" />
<Icon guid="IgnoreWhitespaces" id="1" />
<CommandFlag>DefaultDisabled</CommandFlag>
<Strings>
<ButtonText>Ignore whitespaces</ButtonText>
<MenuText>Ignore whitespaces</MenuText>
<ToolTipText>Ignore whitespaces</ToolTipText>
</Strings>
</Button>
</Buttons>

<Bitmaps>
Expand All @@ -88,6 +99,7 @@
<Bitmap guid="IconRollback" href="Resources\Rollback.png"/>
<Bitmap guid="IconShowDifference" href="Resources\ShowDifference.png"/>
<Bitmap guid="IconCopyOldText" href="Resources\CopyOldText.png"/>
<Bitmap guid="IgnoreWhitespaces" href="Resources\Ignore_whitespaces.png" />
</Bitmaps>
</Commands>

Expand All @@ -100,6 +112,7 @@
<IDSymbol name="RollbackChange" value="2" />
<IDSymbol name="ShowDiff" value="3" />
<IDSymbol name="CopyOldText" value="4" />
<IDSymbol name="IgnoreWhitespaces" value="5" />

<IDSymbol name="GitDiffToolbar" value="100"/>
<IDSymbol name="GitDiffToolbarGroup" value="150"/>
Expand All @@ -110,5 +123,6 @@
<GuidSymbol name="IconRollback" value="{E4584E6D-3BB3-4141-80A7-9E22D028C41E}" />
<GuidSymbol name="IconShowDifference" value="{E8011DB3-8E2C-4EEA-8C3D-0D33FB66EB12}" />
<GuidSymbol name="IconCopyOldText" value="{269FF401-0A36-4E4B-8301-66FB58AA9835}" />
<GuidSymbol name="IgnoreWhitespaces" value="{72992790-BB19-4EC3-A4CE-7C2E6D9E539D}" />
</Symbols>
</CommandTable>
</CommandTable>
1 change: 1 addition & 0 deletions GitDiffMargin/GitDiffMarginCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum GitDiffMarginCommand
RollbackChange = 2,
ShowDiff = 3,
CopyOldText = 4,
IgnoreWhiteSpaces = 5,

GitDiffToolbar = 100,

Expand Down
21 changes: 21 additions & 0 deletions GitDiffMargin/GitDiffMarginCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ protected override OLECMDF QueryCommandStatus(ref Guid commandGroup, uint comman
// these aren't actually commands, but IDs of the command bars and groups
break;

case GitDiffMarginCommand.IgnoreWhiteSpaces:
{
EditorDiffMarginViewModel viewModel;
if (!TryGetMarginViewModel(out viewModel) || !viewModel.HasDiffs)
return 0;

if (viewModel.IgnoreWhiteSpaces)
return OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_LATCHED;
else
return OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED;
}
break;

default:
break;
}
Expand Down Expand Up @@ -166,6 +179,14 @@ protected override bool HandlePreExec(ref Guid commandGroup, uint commandId, OLE
// these aren't actually commands, but IDs of the command bars and groups
break;

case GitDiffMarginCommand.IgnoreWhiteSpaces:
{
ICommand command = viewModel.ToggleIgnoreWhiteSpacesCommand;
command.Execute(null);
return true;
}
break;

default:
break;
}
Expand Down
Binary file added GitDiffMargin/Resources/Ignore_whitespaces.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion GitDiffMargin/ViewModel/DiffMarginViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protected DiffMarginViewModelBase(IMarginCore marginCore)
MarginCore.HunksChanged += HandleHunksChanged;
}

public bool HasDiffs { get; private set; }

public ObservableCollection<DiffViewModel> DiffViewModels { get; private set; }

public void RefreshDiffViewModelPositions()
Expand All @@ -38,7 +40,16 @@ protected virtual void HandleHunksChanged(object sender, HunksChangedEventArgs e
{
DiffViewModels.Clear();

foreach (var diffViewModel in e.Hunks.Select(CreateDiffViewModel))
HasDiffs = e.Hunks.Any();

var hunks = e.Hunks;

if (MarginCore.IgnoreWhiteSpaces)
{
hunks = hunks.Where(hunk => !hunk.IsWhiteSpaceChange);
}

foreach (var diffViewModel in hunks.Select(CreateDiffViewModel))
{
DiffViewModels.Add(diffViewModel);
}
Expand Down
18 changes: 18 additions & 0 deletions GitDiffMargin/ViewModel/EditorDiffMarginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class EditorDiffMarginViewModel : DiffMarginViewModelBase
private readonly Action<DiffViewModel, HunkRangeInfo> _updateDiffDimensions;
private RelayCommand<DiffViewModel> _previousChangeCommand;
private RelayCommand<DiffViewModel> _nextChangeCommand;
private RelayCommand _toggleIgnoreWhiteSpacesCommand;

internal EditorDiffMarginViewModel(IMarginCore marginCore, Action<DiffViewModel, HunkRangeInfo> updateDiffDimensions) :
base(marginCore)
Expand All @@ -25,6 +26,8 @@ internal EditorDiffMarginViewModel(IMarginCore marginCore, Action<DiffViewModel,
_updateDiffDimensions = updateDiffDimensions;
}

public bool IgnoreWhiteSpaces => MarginCore.IgnoreWhiteSpaces;

public RelayCommand<DiffViewModel> PreviousChangeCommand
{
get { return _previousChangeCommand ?? (_previousChangeCommand = new RelayCommand<DiffViewModel>(PreviousChange, PreviousChangeCanExecute)); }
Expand All @@ -35,6 +38,11 @@ public RelayCommand<DiffViewModel> NextChangeCommand
get { return _nextChangeCommand ?? (_nextChangeCommand = new RelayCommand<DiffViewModel>(NextChange, NextChangeCanExecute)); }
}

public RelayCommand ToggleIgnoreWhiteSpacesCommand
{
get { return _toggleIgnoreWhiteSpacesCommand ?? (_toggleIgnoreWhiteSpacesCommand = new RelayCommand(ToggleIgnoreWhiteSpaces, CanExecuteToggleIgnoreWhiteSpaces)); }
}

private bool PreviousChangeCanExecute(DiffViewModel currentEditorDiffViewModel)
{
return DiffViewModels.IndexOf(currentEditorDiffViewModel) > 0;
Expand All @@ -55,6 +63,16 @@ private void NextChange(DiffViewModel currentEditorDiffViewModel)
MoveToChange(currentEditorDiffViewModel, +1);
}

private void ToggleIgnoreWhiteSpaces()
{
MarginCore.ToggleIgnoreWhiteSpace();
}

private bool CanExecuteToggleIgnoreWhiteSpaces()
{
return true;
}

public void MoveToChange(DiffViewModel currentDiffViewModel, int indexModifier)
{
var diffViewModelIndex = DiffViewModels.IndexOf(currentDiffViewModel) + indexModifier;
Expand Down