Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Peek] Add support for previewing AutoHotKey files, CSV, and other plaintext files #34824

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Extra file extensions to preview with Monaco.

.ahk # AutoHotKey scripts

Check failure on line 3 in src/common/FilePreviewCommon/Assets/Monaco/monacoExtraExtensions.txt

View workflow job for this annotation

GitHub Actions / Spell checking

`ahk` is not a recognized word. (unrecognized-spelling)
.csv # comma-separated values
.tsv # tab-separated values

Check failure on line 5 in src/common/FilePreviewCommon/Assets/Monaco/monacoExtraExtensions.txt

View workflow job for this annotation

GitHub Actions / Spell checking

`tsv` is not a recognized word. (unrecognized-spelling)
3 changes: 3 additions & 0 deletions src/common/FilePreviewCommon/FilePreviewCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<None Update="Assets\Monaco\index.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Assets\Monaco\monacoExtraExtensions.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Assets\Monaco\monacoSpecialLanguages.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,14 @@ public void Clear()
}
}

public static bool IsExtensionSupported(string extension)
{
return !string.IsNullOrEmpty(GetPreviewHandlerGuid(extension));
}

public static bool IsItemSupported(IFileSystemItem item)
{
return !string.IsNullOrEmpty(GetPreviewHandlerGuid(item.Extension));
return IsExtensionSupported(item.Extension);
}

private static string? GetPreviewHandlerGuid(string fileExt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using Common.UI;
using ManagedCommon;

namespace Peek.FilePreviewer.Previewers
{
public class MonacoHelper
public partial class MonacoHelper
{
public static readonly HashSet<string> SupportedMonacoFileTypes = GetExtensions();
public static readonly HashSet<string> SupportedMonacoFileTypes;

static MonacoHelper()
{
SupportedMonacoFileTypes = GetExtensions().Union(GetExtraSupportedExtensions()).ToHashSet();
}

public static HashSet<string> GetExtensions()
{
HashSet<string> set = new HashSet<string>();
HashSet<string> set = [];

try
{
JsonDocument languageListDocument = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguages();
using JsonDocument languageListDocument = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguages();
JsonElement languageList = languageListDocument.RootElement.GetProperty("list");
foreach (JsonElement e in languageList.EnumerateArray())
{
Expand All @@ -43,6 +50,39 @@ public static HashSet<string> GetExtensions()
return set;
}

/// <summary>
/// Get the file extensions which are not present in the JSON languages document but which
/// may still be previewed by Monaco.
/// </summary>
/// <remarks>Filters out extensions handled by Shell preview handlers, so Office will still
/// handle rendering .csv and others if it is installed.</remarks>
private static IEnumerable<string> GetExtraSupportedExtensions()
{
try
{
string path = Path.Combine(
Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetRuntimeMonacoDirectory(),
"monacoExtraExtensions.txt");

return File.Exists(path) ?
File.ReadAllLines(path)
.Select(line => ExtensionsFileRegex().Match(line))
.Where(match => match.Success &&
!ShellPreviewHandlerPreviewer.IsExtensionSupported(match.Groups[1].Value))
.Select(match => match.Groups[1].Value) : [];
}
catch
{
return [];
}
}

/// <summary>
/// Extracts the extensions from the extra supported extensions file, ignoring comments.
/// </summary>
[GeneratedRegex(@"^\s*(\.\w+)\s*(#.*)?$")]
private static partial Regex ExtensionsFileRegex();

/// <summary>
/// Prepares temp html for the previewing
/// </summary>
Expand Down
Loading