Skip to content

Commit 67540cf

Browse files
committed
support file path for text check
1 parent 9444941 commit 67540cf

File tree

10 files changed

+65
-9
lines changed

10 files changed

+65
-9
lines changed

docs/diff-tool.custom.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var resolvedTool = DiffTools.AddTool(
2424
exePath: diffToolPath,
2525
binaryExtensions: [".jpg"])!;
2626
```
27-
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L15-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddTool' title='Start of snippet'>anchor</a></sup>
27+
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L17-L31' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddTool' title='Start of snippet'>anchor</a></sup>
2828
<!-- endSnippet -->
2929

3030
Add a tool based on existing resolved tool:
@@ -39,7 +39,7 @@ var resolvedTool = DiffTools.AddToolBasedOn(
3939
Left: (temp, target) => $"\"custom args \"{target}\" \"{temp}\"",
4040
Right: (temp, target) => $"\"custom args \"{temp}\" \"{target}\""))!;
4141
```
42-
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L62-L71' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolBasedOn' title='Start of snippet'>anchor</a></sup>
42+
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L64-L73' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolBasedOn' title='Start of snippet'>anchor</a></sup>
4343
<!-- endSnippet -->
4444

4545

@@ -69,7 +69,7 @@ var resolvedTool = DiffTools.AddToolBasedOn(
6969

7070
await DiffRunner.LaunchAsync(resolvedTool!, "PathToTempFile", "PathToTargetFile");
7171
```
72-
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L82-L93' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolAndLaunch' title='Start of snippet'>anchor</a></sup>
72+
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L84-L95' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolAndLaunch' title='Start of snippet'>anchor</a></sup>
7373
<!-- endSnippet -->
7474

7575

docs/diff-tool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ This value can also be set using [the DiffEngineTray options dialog](/docs/tray.
6363
```cs
6464
DiffRunner.MaxInstancesToLaunch(10);
6565
```
66-
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L6-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-MaxInstancesToLaunch' title='Start of snippet'>anchor</a></sup>
66+
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L8-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-MaxInstancesToLaunch' title='Start of snippet'>anchor</a></sup>
6767
<!-- endSnippet -->
6868

6969

docs/diff-tool.order.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ For example `VisualStudio,Meld` will result in VisualStudio then Meld then all o
5151
```cs
5252
DiffTools.UseOrder(DiffTool.VisualStudio, DiffTool.AraxisMerge);
5353
```
54-
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L139-L143' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseOrder' title='Start of snippet'>anchor</a></sup>
54+
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L147-L151' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseOrder' title='Start of snippet'>anchor</a></sup>
5555
<!-- endSnippet -->

src/DiffEngine.Tests/DiffEngine.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
1717
<PackageReference Include="XunitContext" />
1818
<ProjectReference Include="..\DiffEngine\DiffEngine.csproj" />
19-
<Compile Remove="DefinitionsTest.cs" Condition="'$(TargetFramework)' != 'net8.0'" />
19+
<Compile Remove="DefinitionsTest.cs" Condition="'$(TargetFramework)' == 'net9.0'" />
2020
</ItemGroup>
2121
</Project>

src/DiffEngine.Tests/DiffToolsTest.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ public void OrderShouldNotMessWithAddTool()
5555
Assert.Equal("MyCustomDiffTool", forExtension.Name);
5656
}
5757

58+
[Fact]
59+
public void TextConvention()
60+
{
61+
var diffToolPath = FakeDiffTool.Exe;
62+
var resolvedTool = DiffTools.AddTool(
63+
name: "MyCustomDiffTool",
64+
autoRefresh: true,
65+
isMdi: false,
66+
supportsText: true,
67+
requiresTarget: true,
68+
launchArguments: new(
69+
Left: (tempFile, targetFile) => $"\"{targetFile}\" \"{tempFile}\"",
70+
Right: (tempFile, targetFile) => $"\"{tempFile}\" \"{targetFile}\""),
71+
exePath: diffToolPath,
72+
binaryExtensions: [])!;
73+
DiffTools.UseOrder(DiffTool.VisualStudio, DiffTool.AraxisMerge);
74+
Assert.Equal("MyCustomDiffTool", resolvedTool.Name);
75+
Assert.True(DiffTools.TryFindByExtension(".txtConvention", out var forExtension));
76+
Assert.Equal("MyCustomDiffTool", forExtension.Name);
77+
}
78+
5879
#if DEBUG
5980
[Fact]
6081
public void AddToolBasedOn()
@@ -129,6 +150,12 @@ public Task LaunchSpecificTextDiff() =>
129150
Path.Combine(SourceDirectory, "input.target.txt"));
130151
**/
131152

153+
[Fact]
154+
public Task TextFileConvention()
155+
{
156+
FileExtensions.AddTextFileConvention(_ => _.EndsWith(".txtConvention".AsSpan()));
157+
return DiffRunner.LaunchAsync(Path.Combine(SourceDirectory, "input.temp.txtConvention"), Path.Combine(SourceDirectory, "input.target.txtConvention"));
158+
}
132159
//todo: re enable tests with fake diff tool.
133160

134161
/**
@@ -167,8 +194,7 @@ public void TryFindByName()
167194
}
168195
#endif
169196
**/
170-
public DiffToolsTest(ITestOutputHelper output)
171-
:
197+
public DiffToolsTest(ITestOutputHelper output) :
172198
base(output) =>
173199
DiffTools.Reset();
174200
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
global using System.Collections.Immutable;
2-
global using System.Management;
2+
global using System.Management;
3+
global using EmptyFiles;

src/DiffEngine.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static class ModuleInitializer
55
[ModuleInitializer]
66
public static void Initialize()
77
{
8+
FileExtensions.AddTextFileConvention(_ => _.EndsWith(".txtConvention".AsSpan()));
89
Logging.Enable();
910
DiffRunner.Disabled = false;
1011
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
temp

src/DiffEngine/DiffTools_TryFind.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ public static bool TryFindByExtension(
2020
return ExtensionLookup.TryGetValue(extension, out tool);
2121
}
2222

23+
public static bool TryFindForInputFilePath(
24+
string path,
25+
[NotNullWhen(true)] out ResolvedTool? tool)
26+
{
27+
if (FileExtensions.IsTextFile(path))
28+
{
29+
tool = resolved.FirstOrDefault(_ => _.SupportsText);
30+
return tool != null;
31+
}
32+
33+
return ExtensionLookup.TryGetValue(Path.GetExtension(path), out tool);
34+
}
35+
public static bool TryFindForInputFilePath(
36+
CharSpan path,
37+
[NotNullWhen(true)] out ResolvedTool? tool)
38+
{
39+
if (FileExtensions.IsTextFile(path))
40+
{
41+
tool = resolved.FirstOrDefault(_ => _.SupportsText);
42+
return tool != null;
43+
}
44+
45+
var extension = Path.GetExtension(path).ToString();
46+
return ExtensionLookup.TryGetValue(extension, out tool);
47+
}
48+
2349
public static bool TryFindByName(
2450
DiffTool tool,
2551
[NotNullWhen(true)] out ResolvedTool? resolvedTool)

0 commit comments

Comments
 (0)