Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 84f4847

Browse files
committed
Moved file checks to a filter
Moved the checks for the existance of the file and having a non-readonly nature to an actual IFormattingFilter instance.
1 parent ed9e9ab commit 84f4847

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.CodeAnalysis;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel.Composition;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Filters
11+
{
12+
[Export(typeof(IFormattingFilter))]
13+
internal sealed class UsableFileFilter : IFormattingFilter
14+
{
15+
private readonly Options _options;
16+
17+
[ImportingConstructor]
18+
internal UsableFileFilter(Options options)
19+
{
20+
_options = options;
21+
}
22+
23+
public bool ShouldBeProcessed(Document document)
24+
{
25+
if (document.FilePath == null)
26+
{
27+
return true;
28+
}
29+
30+
var fileInfo = new FileInfo(document.FilePath);
31+
if (!fileInfo.Exists || fileInfo.IsReadOnly)
32+
{
33+
_options.FormatLogger.WriteLine("warning: skipping document '{0}' because it {1}.",
34+
document.FilePath,
35+
fileInfo.IsReadOnly ? "is read-only" : "does not exist");
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
}
42+
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,6 @@ private async Task SaveChanges(Solution solution, Solution originalSolution, Can
127127

128128
private bool ShouldBeProcessed(Document document)
129129
{
130-
if (document.FilePath != null)
131-
{
132-
var fileInfo = new FileInfo(document.FilePath);
133-
if (!fileInfo.Exists || fileInfo.IsReadOnly)
134-
{
135-
FormatLogger.WriteLine("warning: skipping document '{0}' because it {1}.",
136-
document.FilePath,
137-
fileInfo.IsReadOnly ? "is read-only" : "does not exist");
138-
return false;
139-
}
140-
}
141-
142130
foreach (var filter in _filters)
143131
{
144132
var shouldBeProcessed = filter.ShouldBeProcessed(document);

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
</ItemGroup>
7373
<ItemGroup>
7474
<Compile Include="Filters\FilenameFilter.cs" />
75+
<Compile Include="Filters\UsableFileFilter.cs" />
7576
<Compile Include="FormattingEngine.cs" />
7677
<Compile Include="FormattingEngineImplementation.cs" />
7778
<Compile Include="IFormatLogger.cs" />

0 commit comments

Comments
 (0)