Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.
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
39 changes: 25 additions & 14 deletions src/Elders.VSE-FormatDocumentOnSave/AllowDenyDocumentFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.IO;
using EnvDTE;
using Microsoft.VisualStudio.Shell;

Expand All @@ -17,27 +18,37 @@ public class AllowDenyDocumentFilter : IDocumentFilter
/// </summary>
public AllowDenyDocumentFilter() { }

public AllowDenyDocumentFilter(IEnumerable<string> allowedExtensions, IEnumerable<string> deniedExtensions)
public bool MatchesList(IEnumerable<string> list, string filePath)
{
ThreadHelper.ThrowIfNotOnUIThread();
string fileName = Path.GetFileName(filePath);
string extension = Path.GetExtension(filePath);

IEnumerable<string> bannedExtensions = deniedExtensions
.Except(allowedExtensions)
.Where(ext => ext.Equals(".*") == false && string.IsNullOrEmpty(ext) == false);
foreach (string element in list)
{
if(string.IsNullOrEmpty(element))
continue;
if (element.Equals(".*"))
return true;
if (fileName.EndsWith(element, StringComparison.OrdinalIgnoreCase))
return true;
if (extension.EndsWith(element, StringComparison.OrdinalIgnoreCase))
return true;
}

IEnumerable<string> approvedExtensions = allowedExtensions
.Except(deniedExtensions)
.Where(ext => ext.Equals(".*") == false && string.IsNullOrEmpty(ext) == false);

bool areAllExtensionsAllowed = allowedExtensions.Where(ext => ext.Equals(".*")).Any();
bool areAllExtensionsDenied = deniedExtensions.Where(ext => ext.Equals(".*")).Any();
return false;
}

public AllowDenyDocumentFilter(IEnumerable<string> allowedExtensions, IEnumerable<string> deniedExtensions)
{
ThreadHelper.ThrowIfNotOnUIThread();

isAllowed = doc =>
{
ThreadHelper.ThrowIfNotOnUIThread();
return areAllExtensionsAllowed ||
approvedExtensions.Any(ext => { ThreadHelper.ThrowIfNotOnUIThread(); return doc.FullName.EndsWith(ext, StringComparison.OrdinalIgnoreCase); }) ||
(bannedExtensions.Any(ext => { ThreadHelper.ThrowIfNotOnUIThread(); return doc.FullName.EndsWith(ext, StringComparison.OrdinalIgnoreCase); }) == false && areAllExtensionsDenied == false);
if(MatchesList(allowedExtensions, doc.FullName))
if(!MatchesList(deniedExtensions, doc.FullName))
return true;
return false;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public class VisualStudioConfiguration : DialogPage, IConfiguration
public bool IsEnable { get; set; } = true;

[Category("Format Document On Save")]
[DisplayName("Allowed extensions")]
[Description("Space separated list. For example: [.cs .html .cshtml .vb] Overrides extensions listed in Denied section. When you use [.*] the extensions listed in Denied section will be ignored. Empty value respects all extensions listed in Denied section.")]
[DisplayName("Allowed extensions or filenames")]
[Description("Space separated list. For example: [.cs .html .cshtml .vb .h .cpp]. The file extension (.cs) or filename (somefile.cs) needs to match an entry in the list to get formatted. The allow list is checked prior to the denie list.")]
public string Allowed { get; set; } = ".*";

[Category("Format Document On Save")]
[DisplayName("Denied extensions")]
[Description("Space separated list. For example: [.cs .html .cshtml .vb]")]
[DisplayName("Denied extensions or filenames")]
[Description("Space separated list. For example: [resource.h]. The file extension (.cs) or filename (somefile.cs) needs to match an entry in the list to NOT get formatted")]
public string Denied { get; set; } = "";

[Category("Format Document On Save")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Metadata>
<Identity Id="2f4fac85-be4e-4d7a-8c74-93cc4389b427" Version="3.13" Language="en-US" Publisher="Elders" />
<DisplayName>Format document on Save</DisplayName>
<Description xml:space="preserve">Enables auto formatting of the code when you save a file. Visual Studio supports auto formatting of the code with the CTRL+E,D or CTRL+E,F key shortcuts but with this extension the VS command 'Format Document' is executed on Save.
<Description xml:space="preserve">Enables auto formatting of the code when you save a file. Visual Studio supports auto formatting of the code with the CTRL+E,D or CTRL+E,F key shortcuts but with this extension the VS command 'Format Document' is executed on Save. Original by Elders (https://github.com/Elders/VSE-FormatDocumentOnSave). Changed allow deny list handling (filename or extension needs to be in allowed but not in denied)

You can find the source here: https://github.com/Elders/VSE-FormatDocumentOnSave</Description>
</Metadata>
Expand Down