Skip to content

Commit

Permalink
Rework look and feel and the way
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentkempe committed Feb 6, 2013
1 parent 39dd90b commit fb7c8b7
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 129 deletions.
8 changes: 4 additions & 4 deletions GitDiffMargin.Unit.Tests/Git/GitDiffParserTests.cs
Expand Up @@ -158,7 +158,7 @@ public void Parse_EmptyGitDiff_Expect0HunkRangeInfos()
var gitDiffParser = new GitDiffParser(EmptyGitDiff);

//Act
var hunkRangeInfos = gitDiffParser.Parse().ToList();
var hunkRangeInfos = gitDiffParser.Parse(null).ToList();

//Assert
hunkRangeInfos.Count.ShouldBe(0);
Expand All @@ -171,7 +171,7 @@ public void Parse_WithOneHunk_ExpectHunkRanges()
var gitDiffParser = new GitDiffParser(FirstGitDiff);

//Act
var hunkRanges = gitDiffParser.Parse().ToList();
var hunkRanges = gitDiffParser.Parse(null).ToList();

//Assert
hunkRanges[0].OriginalHunkRange.StartingLineNumber.ShouldBe(40);
Expand All @@ -187,7 +187,7 @@ public void Parse_WithOneHunkWithoutLineCount_ExpectHunkRanges()
var gitDiffParser = new GitDiffParser(ThirdGitDiff);

//Act
var hunkRanges = gitDiffParser.Parse().ToList();
var hunkRanges = gitDiffParser.Parse(null).ToList();

//Assert
hunkRanges[0].OriginalHunkRange.StartingLineNumber.ShouldBe(0);
Expand All @@ -203,7 +203,7 @@ public void Parse_WithThreeHunk_ExpectHunkRanges()
var gitDiffParser = new GitDiffParser(SecondGitDiff);

//Act
var hunkRanges = gitDiffParser.Parse().ToList();
var hunkRanges = gitDiffParser.Parse(null).ToList();

//Assert
hunkRanges[0].OriginalHunkRange.StartingLineNumber.ShouldBe(67);
Expand Down
14 changes: 7 additions & 7 deletions GitDiffMargin.Unit.Tests/Git/HunkRangeInfoTests.cs
Expand Up @@ -18,7 +18,7 @@ public class HunkRangeInfoTests
public void IsAddition_AllDiffLinesWithStartsWithPlusSign_ExpectedTrue()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "+ " }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "+ " }.ToArray(), null);

//Act
bool isAddition = hunkRangeInfo.IsAddition;
Expand All @@ -31,7 +31,7 @@ public void IsAddition_AllDiffLinesWithStartsWithPlusSign_ExpectedTrue()
public void IsAddition_NotAllDiffLinesStartsWithPlusSign_ExpectedFalse()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "- " }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "- " }.ToArray(), null);

//Act
bool isAddition = hunkRangeInfo.IsAddition;
Expand All @@ -44,7 +44,7 @@ public void IsAddition_NotAllDiffLinesStartsWithPlusSign_ExpectedFalse()
public void IsModification_DiffLinesStartsWithPlusSignAndWithMinus_ExpectedTrue()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "- " }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "- " }.ToArray(), null);

//Act
bool isModification = hunkRangeInfo.IsModification;
Expand All @@ -57,7 +57,7 @@ public void IsModification_DiffLinesStartsWithPlusSignAndWithMinus_ExpectedTrue(
public void OriginalText_1NewLineAnd1OriginalLine_ExpectedOriginalText()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+New Text", "-Original Text" }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+New Text", "-Original Text" }.ToArray(), null);

//Act
string originalText = hunkRangeInfo.OriginalText[0];
Expand All @@ -70,7 +70,7 @@ public void OriginalText_1NewLineAnd1OriginalLine_ExpectedOriginalText()
public void OriginalText_1NewLineAnd1OriginalLineWithLeadingSpaces_ExpectedOriginalText()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ New Text", "- Original Text" }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ New Text", "- Original Text" }.ToArray(), null);

//Act
string originalText = hunkRangeInfo.OriginalText[0];
Expand All @@ -83,7 +83,7 @@ public void OriginalText_1NewLineAnd1OriginalLineWithLeadingSpaces_ExpectedOrigi
public void OriginalText_1NewLineAnd1OriginalLineWithLeadingSpacesAndInvertedOrder_ExpectedOriginalText()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-18"), new HunkRange("+18"), new List<string> { "- it++; // this is just a comment", "+ it--;" }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-18"), new HunkRange("+18"), new List<string> { "- it++; // this is just a comment", "+ it--;" }.ToArray(), null);

//Act
var originalText = hunkRangeInfo.OriginalText;
Expand All @@ -96,7 +96,7 @@ public void OriginalText_1NewLineAnd1OriginalLineWithLeadingSpacesAndInvertedOrd
public void IsDeletion_3DeletedLines_ExpectTrue()
{
//Arrange
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3"), new HunkRange("+6,0"), new List<string> { "-using Microsoft.VisualStudio.Shell;", "-using Microsoft.VisualStudio.Text;", "-using Microsoft.VisualStudio.Text.Editor;" }.ToArray());
var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3"), new HunkRange("+6,0"), new List<string> { "-using Microsoft.VisualStudio.Shell;", "-using Microsoft.VisualStudio.Text;", "-using Microsoft.VisualStudio.Text.Editor;" }.ToArray(), null);

//Act
var isDeletion = hunkRangeInfo.IsDeletion;
Expand Down
1 change: 1 addition & 0 deletions GitDiffMargin.Unit.Tests/GitDiffMargin.Unit.Tests.csproj
Expand Up @@ -36,6 +36,7 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.Text.Data, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions GitDiffMargin.ncrunchsolution
Expand Up @@ -2,6 +2,7 @@
<FileVersion>1</FileVersion>
<AutoEnableOnStartup>True</AutoEnableOnStartup>
<AllowParallelTestExecution>true</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>UseStaticAnalysis</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>UseStaticAnalysis</FrameworkUtilisationTypeForMSpec>
Expand Down
21 changes: 21 additions & 0 deletions GitDiffMargin/DiffAdditionEditorFormatDefinition.cs
@@ -0,0 +1,21 @@
using System.ComponentModel.Composition;
using System.Windows.Media;
using GitDiffMargin.Git;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;

namespace GitDiffMargin
{
[Export(typeof(EditorFormatDefinition))]
[Name(DiffFormatNames.Addition)]
[UserVisible(true)]
internal sealed class DiffAdditionEditorFormatDefinition : EditorFormatDefinition
{
public DiffAdditionEditorFormatDefinition()
{
BackgroundColor = Color.FromRgb(180, 255, 180);
ForegroundCustomizable = false;
DisplayName = "Git Diff Addition";
}
}
}
21 changes: 21 additions & 0 deletions GitDiffMargin/DiffModificationEditorFormatDefinition.cs
@@ -0,0 +1,21 @@
using System.ComponentModel.Composition;
using System.Windows.Media;
using GitDiffMargin.Git;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;

namespace GitDiffMargin
{
[Export(typeof(EditorFormatDefinition))]
[Name(DiffFormatNames.Modification)]
[UserVisible(true)]
internal sealed class DiffModificationEditorFormatDefinition : EditorFormatDefinition
{
public DiffModificationEditorFormatDefinition()
{
BackgroundColor = Color.FromRgb(160, 200, 255);
ForegroundCustomizable = false;
DisplayName = "Git Diff Modification";
}
}
}
21 changes: 21 additions & 0 deletions GitDiffMargin/DiffRemovedEditorFormatDefinition.cs
@@ -0,0 +1,21 @@
using GitDiffMargin.Git;
using System.ComponentModel.Composition;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;

namespace GitDiffMargin
{
[Export(typeof(EditorFormatDefinition))]
[Name(DiffFormatNames.Removed)]
[UserVisible(true)]
internal sealed class DiffRemovedEditorFormatDefinition : EditorFormatDefinition
{
public DiffRemovedEditorFormatDefinition()
{
BackgroundColor = Color.FromRgb(255, 160, 180);
ForegroundCustomizable = false;
DisplayName = "Git Diff Removed";
}
}
}
2 changes: 1 addition & 1 deletion GitDiffMargin/DiffUpdateBackgroundParser.cs
Expand Up @@ -38,7 +38,7 @@ protected override void ReParseImpl()
{
textDocument = null;
}
var diff = textDocument != null ? _commands.GetGitDiffFor(textDocument.FilePath) : Enumerable.Empty<HunkRangeInfo>();
var diff = textDocument != null ? _commands.GetGitDiffFor(textDocument.FilePath, snapshot) : Enumerable.Empty<HunkRangeInfo>();

var result = new DiffParseResultEventArgs(snapshot, stopwatch.Elapsed, diff.ToList());
OnParseComplete(result);
Expand Down
9 changes: 3 additions & 6 deletions GitDiffMargin/Git/GitCommands.cs
Expand Up @@ -3,18 +3,17 @@
using System.Diagnostics;
using System.IO;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;

namespace GitDiffMargin.Git
{
public class GitCommands : IGitCommands
{
public IEnumerable<HunkRangeInfo> GetGitDiffFor(string filename)
public IEnumerable<HunkRangeInfo> GetGitDiffFor(string filename, ITextSnapshot snapshot)
{
var p = GetProcess(filename);
p.StartInfo.Arguments = String.Format(@" diff --unified=0 {0}", filename);

ActivityLog.LogInformation("GitDiffMargin", "Command:" + p.StartInfo.Arguments);

p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
Expand All @@ -23,10 +22,8 @@ public IEnumerable<HunkRangeInfo> GetGitDiffFor(string filename)
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

ActivityLog.LogInformation("GitDiffMargin", "Git Diff output:" + output);

var gitDiffParser = new GitDiffParser(output);
return gitDiffParser.Parse();
return gitDiffParser.Parse(snapshot);
}

public void StartExternalDiff(string filename)
Expand Down
7 changes: 4 additions & 3 deletions GitDiffMargin/Git/GitDiffParser.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Text;

namespace GitDiffMargin.Git
{
Expand All @@ -13,11 +14,11 @@ public GitDiffParser(string gitDiff)
_gitDiff = gitDiff;
}

public IEnumerable<HunkRangeInfo> Parse()
public IEnumerable<HunkRangeInfo> Parse(ITextSnapshot snapshot)
{
return from hunkLine in GetUnifiedFormatHunkLines()
where !string.IsNullOrEmpty(hunkLine.Item1)
select new HunkRangeInfo(new HunkRange(GetHunkOriginalFile(hunkLine.Item1)), new HunkRange(GetHunkNewFile(hunkLine.Item1)), hunkLine.Item2);
where !string.IsNullOrEmpty(hunkLine.Item1)
select new HunkRangeInfo(new HunkRange(GetHunkOriginalFile(hunkLine.Item1)), new HunkRange(GetHunkNewFile(hunkLine.Item1)), hunkLine.Item2, snapshot);
}

public IEnumerable<Tuple<string, IEnumerable<string>>> GetUnifiedFormatHunkLines()
Expand Down
10 changes: 5 additions & 5 deletions GitDiffMargin/Git/HunkRange.cs
Expand Up @@ -7,18 +7,18 @@ public HunkRange(string hunkRange)
if (hunkRange.Contains(","))
{
var hunkParts = hunkRange.Split(',');
StartingLineNumber = long.Parse(hunkParts[0]) - 1;
NumberOfLines = long.Parse(hunkParts[1]);
StartingLineNumber = int.Parse(hunkParts[0]) - 1;
NumberOfLines = int.Parse(hunkParts[1]);
}
else
{
StartingLineNumber = long.Parse(hunkRange) - 1;
StartingLineNumber = int.Parse(hunkRange) - 1;
NumberOfLines = 1;
}

}

public long StartingLineNumber { get; private set; }
public long NumberOfLines { get; private set; }
public int StartingLineNumber { get; private set; }
public int NumberOfLines { get; private set; }
}
}
5 changes: 4 additions & 1 deletion GitDiffMargin/Git/HunkRangeInfo.cs
Expand Up @@ -3,17 +3,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Text;

#endregion

namespace GitDiffMargin.Git
{
public class HunkRangeInfo
{
private readonly ITextSnapshot _snapshot;
private List<string> DiffLines { get; set; }

public HunkRangeInfo(HunkRange originaleHunkRange, HunkRange newHunkRange, IEnumerable<string> diffLines)
public HunkRangeInfo(HunkRange originaleHunkRange, HunkRange newHunkRange, IEnumerable<string> diffLines, ITextSnapshot snapshot)
{
_snapshot = snapshot;
OriginalHunkRange = originaleHunkRange;
NewHunkRange = newHunkRange;
DiffLines = diffLines.ToList();
Expand Down
3 changes: 2 additions & 1 deletion GitDiffMargin/Git/IGitCommands.cs
@@ -1,14 +1,15 @@
#region using

using System.Collections.Generic;
using Microsoft.VisualStudio.Text;

#endregion

namespace GitDiffMargin.Git
{
public interface IGitCommands
{
IEnumerable<HunkRangeInfo> GetGitDiffFor(string filename);
IEnumerable<HunkRangeInfo> GetGitDiffFor(string filename, ITextSnapshot snapshot);
void StartExternalDiff(string filename);
}
}
3 changes: 3 additions & 0 deletions GitDiffMargin/GitDiffMargin.csproj
Expand Up @@ -103,7 +103,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BackgroundParser.cs" />
<Compile Include="DiffAdditionEditorFormatDefinition.cs" />
<Compile Include="DiffModificationEditorFormatDefinition.cs" />
<Compile Include="DiffParseResultEventArgs.cs" />
<Compile Include="DiffRemovedEditorFormatDefinition.cs" />
<Compile Include="DiffUpdateBackgroundParser.cs" />
<Compile Include="GitDiffMargin.cs" />
<Compile Include="GitDiffMarginFactory.cs" />
Expand Down
2 changes: 1 addition & 1 deletion GitDiffMargin/GitDiffMarginFactory.cs
Expand Up @@ -12,7 +12,7 @@ namespace GitDiffMargin
{
[Export(typeof (IWpfTextViewMarginProvider))]
[Name(GitDiffMargin.MarginName)]
[Order(After = PredefinedMarginNames.Spacer)]
[Order(Before = PredefinedMarginNames.LineNumber)]
[MarginContainer(PredefinedMarginNames.LeftSelection)]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Interactive)]
Expand Down
20 changes: 9 additions & 11 deletions GitDiffMargin/View/GitDiffBarControl.xaml
Expand Up @@ -5,6 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModel="clr-namespace:GitDiffMargin.ViewModel"
Cursor="Hand"
ClipToBounds="True"
d:DataContext="{d:DesignInstance ViewModel:DiffMarginViewModel}"
d:DesignHeight="300"
d:DesignWidth="9"
Expand Down Expand Up @@ -70,18 +71,15 @@
</Setter>
</Style>
<ControlTemplate x:Key="ChangeTemplate" d:DataContext="{d:DesignInstance ViewModel:DiffViewModel}">
<Border Width="9"
BorderBrush="#E0FFE0"
BorderThickness="0,1,1,1">
<Rectangle Fill="{Binding DiffBrush}">
<Rectangle.ToolTip>
<ToolTip Content="{Binding Coordinates, Mode=OneWay}" />
</Rectangle.ToolTip>
</Rectangle>
</Border>
<Rectangle Width="{Binding Width, Mode=OneWay}" HorizontalAlignment="Left" Margin="{Binding Margin, Mode=OneWay}" Fill="{Binding DiffBrush}">
<Rectangle.ToolTip>
<ToolTip Content="{Binding Coordinates, Mode=OneWay}" />
</Rectangle.ToolTip>
</Rectangle>

</ControlTemplate>
<ControlTemplate x:Key="DeletionTemplate" d:DataContext="{d:DesignInstance ViewModel:DiffViewModel}">
<Polygon Points="0,0 1,1 0,2 0,0" Fill="Gray" Width="9" Height="{Binding Height}" Stretch="Fill">
<Polygon Points="0,0 1,1 0,2 0,0" Fill="{Binding DiffBrush}" Width="9" Height="{Binding Height}" Stretch="Fill">
<Polygon.ToolTip>
<ToolTip Content="{Binding Coordinates, Mode=OneWay}" />
</Polygon.ToolTip>
Expand All @@ -95,7 +93,7 @@
<ItemsControl Grid.Column="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding DiffViewModels, Mode=TwoWay}">
ItemsSource="{Binding DiffViewModels, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
Expand Down
1 change: 0 additions & 1 deletion GitDiffMargin/ViewModel/DiffMarginViewModel.cs
Expand Up @@ -116,7 +116,6 @@ private void HandleParseComplete(object sender, ParseResultEventArgs e)
var diffResult = e as DiffParseResultEventArgs;
if (diffResult == null) return;
foreach (var diffViewModel in diffResult.Diff.Select(hunkRangeInfo => new DiffViewModel(hunkRangeInfo, _textView)))
{
DiffViewModels.Add(diffViewModel);
Expand Down

0 comments on commit fb7c8b7

Please sign in to comment.