Skip to content

Commit

Permalink
Fixed file name issue discussed in #7
Browse files Browse the repository at this point in the history
  • Loading branch information
heku committed May 31, 2022
1 parent 66f4b15 commit 73d5644
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Kool.VsDiff.Shared/Commands/DiffClipboardWithCodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ protected override void OnExecute()
{
var extension = Path.GetExtension(ActiveDocument.Name);

var selectionFile = TempFileHelper.CreateTempFile("Selection" + extension, _selectionText);
var clipboardFile = TempFileHelper.CreateTempFile("Clipboard" + extension, _clipboardText);
var selectionFile = TempFileHelper.CreateTempFile("Selection" + extension, _selectionText);

DiffToolFactory.CreateDiffTool().Diff(clipboardFile, selectionFile,
DiffToolFactory.CreateDiffTool().Diff("Clipboard", "Selection", clipboardFile, selectionFile,
(file1, file2) =>
{
TempFileHelper.RemoveTempFile(file1);
Expand Down
10 changes: 6 additions & 4 deletions Kool.VsDiff.Shared/Commands/DiffClipboardWithDocumentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal sealed class DiffClipboardWithDocumentCommand : BaseCommand
{
public static DiffClipboardWithDocumentCommand Instance { get; } = new();

private string _documentName;
private string _documentFile;
private string _clipboardText;

Expand All @@ -20,22 +21,23 @@ private DiffClipboardWithDocumentCommand() : base(Ids.DIFF_CLIPBOARD_WITH_DOCUME
protected override void OnBeforeQueryStatus()
{
Visible = ClipboardHelper.TryGetClipboardText(out _clipboardText)
&& TryGetDocumentActiveDocumentFile(out _documentFile);
&& TryGetDocumentActiveDocumentFile(out _documentName, out _documentFile);
}

protected override void OnExecute()
{
var extension = Path.GetExtension(_documentFile);
var clipboardFile = TempFileHelper.CreateTempFile("Clipboard" + extension, _clipboardText);

DiffToolFactory.CreateDiffTool().Diff(clipboardFile, _documentFile, (f, _) => TempFileHelper.RemoveTempFile(f));
DiffToolFactory.CreateDiffTool().Diff("Clipboard", _documentName, clipboardFile, _documentFile, (f, _) => TempFileHelper.RemoveTempFile(f));
}

private bool TryGetDocumentActiveDocumentFile(out string file)
private bool TryGetDocumentActiveDocumentFile(out string name, out string file)
{
file = null;
name = file = null;
try
{
name = IDE.ActiveWindow.Caption;
file = IDE.ActiveDocument.FullName;
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions Kool.VsDiff.Shared/Commands/DiffClipboardWithFileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal sealed class DiffClipboardWithFileCommand : BaseCommand
{
public static DiffClipboardWithFileCommand Instance { get; } = new();

private string _selectedName;
private string _selectedFile;
private string _clipboardText;

Expand All @@ -18,14 +19,14 @@ private DiffClipboardWithFileCommand() : base(Ids.DIFF_CLIPBOARD_WITH_FILE_CMD_I
protected override void OnBeforeQueryStatus()
{
Visible = ClipboardHelper.TryGetClipboardText(out _clipboardText)
&& SolutionExplorer.TryGetSingleSelectedFile(out _selectedFile);
&& SolutionExplorer.TryGetSingleSelectedFile(out _selectedName, out _selectedFile);
}

protected override void OnExecute()
{
var extension = Path.GetExtension(_selectedFile);
var clipboardFile = TempFileHelper.CreateTempFile("Clipboard" + extension, _clipboardText);

DiffToolFactory.CreateDiffTool().Diff(clipboardFile, _selectedFile, (f, _) => TempFileHelper.RemoveTempFile(f));
DiffToolFactory.CreateDiffTool().Diff("Clipboard", _selectedName, clipboardFile, _selectedFile, (f, _) => TempFileHelper.RemoveTempFile(f));
}
}
6 changes: 4 additions & 2 deletions Kool.VsDiff.Shared/Commands/DiffSelectedFilesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ internal sealed class DiffSelectedFilesCommand : BaseCommand
{
public static DiffSelectedFilesCommand Instance { get; } = new();

private string _name1;
private string _name2;
private string _file1;
private string _file2;

Expand All @@ -16,11 +18,11 @@ private DiffSelectedFilesCommand() : base(Ids.DIFF_SELECTED_FILES_CMD_ID)

protected override void OnBeforeQueryStatus()
{
Visible = SolutionExplorer.TryGetSelectedFiles(out _file1, out _file2);
Visible = SolutionExplorer.TryGetSelectedFiles(out _name1, out _name2, out _file1, out _file2);
}

protected override void OnExecute()
{
DiffToolFactory.CreateDiffTool().Diff(_file1, _file2, null);
DiffToolFactory.CreateDiffTool().Diff(_name1, _name2, _file1, _file2, null);
}
}
11 changes: 9 additions & 2 deletions Kool.VsDiff.Shared/Models/CustomDiffTool.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using System;
using System.Diagnostics;
using System.IO;
using static Kool.VsDiff.VsDiffPackage;

namespace Kool.VsDiff.Models;

internal sealed class CustomDiffTool : IDiffTool
{
public void Diff(string file1, string file2, Action<string, string> callback)
public void Diff(string name1, string name2, string file1, string file2, Action<string, string> callback)
{
var args = Options.CustomDiffToolArgs.Replace("$FILE1", file1).Replace("$FILE2", file2);
name1 ??= Path.GetFileName(file1);
name2 ??= Path.GetFileName(file2);

var args = Options.CustomDiffToolArgs.Replace("$FILE1", file1)
.Replace("$FILE2", file2)
.Replace("$NAME1", name1)
.Replace("$NAME2", name2);
var process = new Process
{
EnableRaisingEvents = true,
Expand Down
2 changes: 1 addition & 1 deletion Kool.VsDiff.Shared/Models/IDiffTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Kool.VsDiff.Models;

internal interface IDiffTool
{
void Diff(string file1, string file2, Action<string, string> callback);
void Diff(string name1, string name2, string file1, string file2, Action<string, string> callback);
}
39 changes: 24 additions & 15 deletions Kool.VsDiff.Shared/Models/VS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static class SolutionExplorer
{
private static readonly string SystemDirectorySeparator = Path.DirectorySeparatorChar.ToString();

public static bool TryGetSingleSelectedFile(out string file)
public static bool TryGetSingleSelectedFile(out string name, out string file)
{
file = null;
name = file = null;
try
{
var selectedItems = IDE.SelectedItems;
Expand All @@ -31,7 +31,8 @@ public static bool TryGetSingleSelectedFile(out string file)
var files = GetSelectedFiles(selectedItems);
if (files?.Count == 1)
{
file = files[0];
name = files[0].Name;
file = files[0].File;
return !string.IsNullOrEmpty(file);
}
}
Expand All @@ -43,9 +44,9 @@ public static bool TryGetSingleSelectedFile(out string file)
return false;
}

public static bool TryGetSelectedFiles(out string file1, out string file2)
public static bool TryGetSelectedFiles(out string name1, out string name2, out string file1, out string file2)
{
file1 = file2 = null;
name1 = name2 = file1 = file2 = null;
try
{
var selectedItems = IDE.SelectedItems;
Expand All @@ -57,8 +58,10 @@ public static bool TryGetSelectedFiles(out string file1, out string file2)
var files = GetSelectedFiles(selectedItems);
if (files?.Count == 2)
{
file1 = files[0];
file2 = files[1];
name1 = files[0].Name;
file1 = files[0].File;
name2 = files[1].Name;
file2 = files[1].File;
return !string.IsNullOrEmpty(file1) && !string.IsNullOrEmpty(file2);
}
}
Expand All @@ -70,9 +73,15 @@ public static bool TryGetSelectedFiles(out string file1, out string file2)
return false;
}

private static List<string> GetSelectedFiles(SelectedItems selectedItems)
private struct NameFile
{
public string Name;
public string File;
}

private static List<NameFile> GetSelectedFiles(SelectedItems selectedItems)
{
List<string> files = null;
List<NameFile> files = null;
foreach (SelectedItem item in selectedItems)
{
// The index of file names from 1 to FileCount for the project item
Expand All @@ -84,14 +93,14 @@ private static List<string> GetSelectedFiles(SelectedItems selectedItems)
}
if (files == null)
{
files = new List<string>();
files = new List<NameFile>();
}
files.Add(file);
files.Add(new NameFile { Name = item.ProjectItem.Name, File = file });
}
return files ?? GetSelectedFilesInsideFolderView();
}

private static List<string> GetSelectedFilesInsideFolderView()
private static List<NameFile> GetSelectedFilesInsideFolderView()
{
var hierarchyPtr = IntPtr.Zero;
var containerPtr = IntPtr.Zero;
Expand All @@ -100,15 +109,15 @@ private static List<string> GetSelectedFilesInsideFolderView()
if (MonitorSelection != null &&
MonitorSelection.GetCurrentSelection(out hierarchyPtr, out var itemid, out var multiSelect, out containerPtr) == VSConstants.S_OK)
{
var files = new List<string>();
var files = new List<NameFile>();
if (itemid != VSConstants.VSITEMID_SELECTION)
{
if (itemid != VSConstants.VSCOOKIE_NIL &&
hierarchyPtr != IntPtr.Zero &&
Marshal.GetObjectForIUnknown(hierarchyPtr) is IVsHierarchy hierarchy &&
TryGetFile(hierarchy, itemid, out var file))
{
files.Add(file);
files.Add(new NameFile { File = file });
}
}
else if (multiSelect != null)
Expand All @@ -123,7 +132,7 @@ private static List<string> GetSelectedFilesInsideFolderView()
{
if (TryGetFile(selection.pHier, selection.itemid, out var file))
{
files.Add(file);
files.Add(new NameFile { File = file });
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Kool.VsDiff.Shared/Models/VsDiffTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public VsDiffTool()
Assumes.Present(_diffService);
}

public void Diff(string file1, string file2, Action<string, string> callback)
public void Diff(string name1, string name2, string file1, string file2, Action<string, string> callback)
{
var name1 = Path.GetFileName(file1);
var name2 = Path.GetFileName(file2);
name1 ??= Path.GetFileName(file1);
name2 ??= Path.GetFileName(file2);
var caption = $"{name1} vs {name2}";
var tooltip = file1 + Environment.NewLine + file2;

Expand Down
2 changes: 1 addition & 1 deletion Kool.VsDiff.Shared/Pages/VsDiffOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void SetDefaults()
{
UseCustomDiffTool = false;
CustomDiffToolPath = @"%ProgramFiles(x86)%\WinMerge\WinMergeU.exe";
CustomDiffToolArgs = "-e -u \"$FILE1\" \"$FILE2\"";
CustomDiffToolArgs = "-e -u \"$FILE1\" \"$FILE2\" -dl \"$NAME1\" -dr \"$NAME2\"";
DiffClipboardWithCodeEnabled = true;
DiffClipboardWithFileEnabled = true;
DiffClipboardWithDocumentEnabled = true;
Expand Down
2 changes: 1 addition & 1 deletion Kool.VsDiff.Shared/Pages/VsDiffOptionsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void OnTestButtonClicked(object sender, RoutedEventArgs e)

try
{
new CustomDiffTool().Diff(file1, file2, (f1, f2) =>
new CustomDiffTool().Diff("NAME1", "NAME2", file1, file2, (f1, f2) =>
{
TempFileHelper.RemoveTempFile(f1);
TempFileHelper.RemoveTempFile(f2);
Expand Down

0 comments on commit 73d5644

Please sign in to comment.