mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-10-21 19:25:14 +00:00
Use format detector feature for TextViewer
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
// Copyright © 2017-2025 QL-Win Contributors
|
||||||
|
//
|
||||||
|
// This file is part of QuickLook program.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System.ComponentModel.Composition;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using UtfUnknown;
|
||||||
|
|
||||||
|
namespace QuickLook.Plugin.TextViewer.Detectors;
|
||||||
|
|
||||||
|
[Export]
|
||||||
|
public class EncodingDetector
|
||||||
|
{
|
||||||
|
public static Encoding DetectFromBytes(byte[] bytes)
|
||||||
|
{
|
||||||
|
var result = CharsetDetector.DetectFromBytes(bytes);
|
||||||
|
var encoding = result.DoubleDetectFromResult(bytes); // Fix issues
|
||||||
|
|
||||||
|
return encoding;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file static class DetectionExtensions
|
||||||
|
{
|
||||||
|
public static Encoding DoubleDetectFromResult(this DetectionResult result, byte[] buffer)
|
||||||
|
{
|
||||||
|
// Determine the highest confidence encoding, or fallback to ANSI
|
||||||
|
var encoding = result.Detected?.Encoding ?? Encoding.Default;
|
||||||
|
|
||||||
|
// When mixing encodings, one of the encodings may gain higher confidence
|
||||||
|
// In this case, we should return to encodings UTF8 / UTF32 / ANSI
|
||||||
|
// https://github.com/QL-Win/QuickLook/issues/769
|
||||||
|
if (encoding != Encoding.UTF8 && encoding != Encoding.UTF32 && encoding != Encoding.Default)
|
||||||
|
{
|
||||||
|
if (result.Details.Any(detail => detail.Encoding == Encoding.UTF8))
|
||||||
|
{
|
||||||
|
encoding = Encoding.UTF8;
|
||||||
|
}
|
||||||
|
else if (result.Details.Any(detail => detail.Encoding == Encoding.UTF32))
|
||||||
|
{
|
||||||
|
encoding = Encoding.UTF32;
|
||||||
|
}
|
||||||
|
else if (result.Details.Any(detail => detail.Encoding == Encoding.Default))
|
||||||
|
{
|
||||||
|
encoding = Encoding.Default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the text is too short and lacks a BOM
|
||||||
|
// In this case, we should fallback to an encoding if it is not recognized as UTF8 / UTF32 / ANSI
|
||||||
|
// https://github.com/QL-Win/QuickLook/issues/471
|
||||||
|
// https://github.com/QL-Win/QuickLook/issues/600
|
||||||
|
// https://github.com/QL-Win/QuickLook/issues/954
|
||||||
|
if (buffer.Length <= 50)
|
||||||
|
{
|
||||||
|
if (encoding != Encoding.UTF8 && encoding != Encoding.UTF32 && encoding != Encoding.Default)
|
||||||
|
{
|
||||||
|
if (!Encoding.UTF8.GetString(buffer).Contains("\uFFFD"))
|
||||||
|
{
|
||||||
|
encoding = Encoding.UTF8;
|
||||||
|
}
|
||||||
|
else if (!Encoding.UTF32.GetString(buffer).Contains("\uFFFD"))
|
||||||
|
{
|
||||||
|
encoding = Encoding.UTF32;
|
||||||
|
}
|
||||||
|
else if (!Encoding.Default.GetString(buffer).Contains("\uFFFD"))
|
||||||
|
{
|
||||||
|
encoding = Encoding.Default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return encoding;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,48 @@
|
|||||||
|
// Copyright © 2017-2025 QL-Win Contributors
|
||||||
|
//
|
||||||
|
// This file is part of QuickLook program.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace QuickLook.Plugin.TextViewer.Detectors;
|
||||||
|
|
||||||
|
public class FormatDetector
|
||||||
|
{
|
||||||
|
public static FormatDetector Instance { get; } = new();
|
||||||
|
|
||||||
|
internal IFormatDetector[] TextDetectors =
|
||||||
|
[
|
||||||
|
new XMLDetector(),
|
||||||
|
new JSONDetector(),
|
||||||
|
];
|
||||||
|
|
||||||
|
public static IFormatDetector Detect(string text)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(text)) return null;
|
||||||
|
|
||||||
|
return Instance.TextDetectors.Where(detector => detector.Detect(text))
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IFormatDetector
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public string Extension { get; }
|
||||||
|
|
||||||
|
public bool Detect(string text);
|
||||||
|
}
|
@@ -0,0 +1,52 @@
|
|||||||
|
// Copyright © 2017-2025 QL-Win Contributors
|
||||||
|
//
|
||||||
|
// This file is part of QuickLook program.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace QuickLook.Plugin.TextViewer.Detectors;
|
||||||
|
|
||||||
|
public class JSONDetector : IFormatDetector
|
||||||
|
{
|
||||||
|
public string Name => "JSON";
|
||||||
|
|
||||||
|
public string Extension => ".json";
|
||||||
|
|
||||||
|
public bool Detect(string text)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(text)) return false;
|
||||||
|
|
||||||
|
// TODO: Use AsSpan to improve
|
||||||
|
|
||||||
|
var trimmedStart = text.TrimStart();
|
||||||
|
|
||||||
|
if (trimmedStart.StartsWith("{") || trimmedStart.StartsWith("["))
|
||||||
|
{
|
||||||
|
var trimmedEnd = text.TrimEnd();
|
||||||
|
|
||||||
|
if (trimmedEnd.EndsWith("}") || trimmedEnd.EndsWith("]"))
|
||||||
|
{
|
||||||
|
// At least one key exists
|
||||||
|
if (Regex.IsMatch(text, @"""[^""]+""\s*:"))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,34 @@
|
|||||||
|
// Copyright © 2017-2025 QL-Win Contributors
|
||||||
|
//
|
||||||
|
// This file is part of QuickLook program.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace QuickLook.Plugin.TextViewer.Detectors;
|
||||||
|
|
||||||
|
public class XMLDetector : IFormatDetector
|
||||||
|
{
|
||||||
|
internal Regex Signature { get; } = new(@"<\?xml\b[^>]*\bversion\s*=\s*""[^""]*""[^\?>]*\?>", RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
public string Name => "XML";
|
||||||
|
|
||||||
|
public string Extension => ".xml";
|
||||||
|
|
||||||
|
public bool Detect(string text)
|
||||||
|
{
|
||||||
|
return Signature.IsMatch(text);
|
||||||
|
}
|
||||||
|
}
|
@@ -16,20 +16,14 @@
|
|||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using ICSharpCode.AvalonEdit;
|
using ICSharpCode.AvalonEdit;
|
||||||
using ICSharpCode.AvalonEdit.Highlighting;
|
|
||||||
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
|
|
||||||
using QuickLook.Common.Helpers;
|
|
||||||
using QuickLook.Common.Plugin;
|
using QuickLook.Common.Plugin;
|
||||||
|
using QuickLook.Plugin.TextViewer.Themes;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Media;
|
|
||||||
using System.Xml;
|
|
||||||
|
|
||||||
namespace QuickLook.Plugin.TextViewer;
|
namespace QuickLook.Plugin.TextViewer;
|
||||||
|
|
||||||
@@ -37,9 +31,6 @@ public class Plugin : IViewer
|
|||||||
{
|
{
|
||||||
private TextViewerPanel _tvp;
|
private TextViewerPanel _tvp;
|
||||||
|
|
||||||
private static HighlightingManager _hlmLight;
|
|
||||||
private static HighlightingManager _hlmDark;
|
|
||||||
|
|
||||||
public int Priority => -5;
|
public int Priority => -5;
|
||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
@@ -47,9 +38,7 @@ public class Plugin : IViewer
|
|||||||
// pre-load
|
// pre-load
|
||||||
var _ = new TextEditor();
|
var _ = new TextEditor();
|
||||||
|
|
||||||
InitHighlightingManager();
|
HighlightingThemeManager.Initialize();
|
||||||
AddHighlightingManager(_hlmLight, "Light");
|
|
||||||
AddHighlightingManager(_hlmDark, "Dark");
|
|
||||||
|
|
||||||
// Implementation of the Search Panel Styled with Fluent Theme
|
// Implementation of the Search Panel Styled with Fluent Theme
|
||||||
{
|
{
|
||||||
@@ -71,7 +60,7 @@ public class Plugin : IViewer
|
|||||||
if (Directory.Exists(path))
|
if (Directory.Exists(path))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (new[] { ".txt", ".rtf" }.Any(path.ToLower().EndsWith))
|
if (new[] { ".txt", ".rtf" }.Any(ext => path.EndsWith(ext, StringComparison.OrdinalIgnoreCase)))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// if there is a matched highlighting scheme (by file extension), treat it as a plain text file
|
// if there is a matched highlighting scheme (by file extension), treat it as a plain text file
|
||||||
@@ -94,7 +83,7 @@ public class Plugin : IViewer
|
|||||||
|
|
||||||
public void View(string path, ContextObject context)
|
public void View(string path, ContextObject context)
|
||||||
{
|
{
|
||||||
if (path.ToLower().EndsWith(".rtf"))
|
if (path.EndsWith(".rtf", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
var rtfBox = new RichTextBox();
|
var rtfBox = new RichTextBox();
|
||||||
FileStream fs = File.OpenRead(path);
|
FileStream fs = File.OpenRead(path);
|
||||||
@@ -108,9 +97,8 @@ public class Plugin : IViewer
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_tvp = new TextViewerPanel(path, context);
|
_tvp = new TextViewerPanel();
|
||||||
AssignHighlightingManager(path, _tvp, context);
|
_tvp.LoadFileAsync(path, context);
|
||||||
|
|
||||||
context.ViewerContent = _tvp;
|
context.ViewerContent = _tvp;
|
||||||
}
|
}
|
||||||
context.Title = $"{Path.GetFileName(path)}";
|
context.Title = $"{Path.GetFileName(path)}";
|
||||||
@@ -130,123 +118,4 @@ public class Plugin : IViewer
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddHighlightingManager(HighlightingManager hlm, string dirName)
|
|
||||||
{
|
|
||||||
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
||||||
if (string.IsNullOrEmpty(assemblyPath))
|
|
||||||
return;
|
|
||||||
|
|
||||||
var syntaxPath = Path.Combine(assemblyPath, "Syntax", dirName);
|
|
||||||
if (!Directory.Exists(syntaxPath))
|
|
||||||
return;
|
|
||||||
|
|
||||||
foreach (var file in Directory.EnumerateFiles(syntaxPath, "*.xshd").OrderBy(f => f))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Debug.WriteLine(file);
|
|
||||||
var ext = Path.GetFileNameWithoutExtension(file);
|
|
||||||
using Stream s = File.OpenRead(Path.GetFullPath(file));
|
|
||||||
using var reader = new XmlTextReader(s);
|
|
||||||
var xshd = HighlightingLoader.LoadXshd(reader);
|
|
||||||
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
|
|
||||||
if (xshd.Extensions.Count > 0)
|
|
||||||
hlm.RegisterHighlighting(ext, [.. xshd.Extensions], highlightingDefinition);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
ProcessHelper.WriteLog(e.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitHighlightingManager()
|
|
||||||
{
|
|
||||||
_hlmLight = new HighlightingManager();
|
|
||||||
_hlmDark = new HighlightingManager();
|
|
||||||
|
|
||||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
||||||
string[] resourceNames = assembly.GetManifestResourceNames();
|
|
||||||
|
|
||||||
foreach (var resourceName in resourceNames.Where(name => name.Contains(".Syntax.")))
|
|
||||||
{
|
|
||||||
using Stream s = assembly.GetManifestResourceStream(resourceName);
|
|
||||||
|
|
||||||
if (s == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Debug.WriteLine(resourceName);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var hlm = resourceName.Contains(".Syntax.Dark.") ? _hlmDark : _hlmLight;
|
|
||||||
var name = EmbeddedResource.GetFileNameWithoutExtension(resourceName);
|
|
||||||
using var reader = new XmlTextReader(s);
|
|
||||||
var xshd = HighlightingLoader.LoadXshd(reader);
|
|
||||||
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
|
|
||||||
if (xshd.Extensions.Count > 0)
|
|
||||||
hlm.RegisterHighlighting(name, [.. xshd.Extensions], highlightingDefinition);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
ProcessHelper.WriteLog(e.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AssignHighlightingManager(string path, TextViewerPanel tvp, ContextObject context)
|
|
||||||
{
|
|
||||||
var def = _hlmDark.GetDefinitionByExtension(Path.GetExtension(path));
|
|
||||||
var darkThemeAllowed = SettingHelper.Get("AllowDarkTheme", def != null, "QuickLook.Plugin.TextViewer");
|
|
||||||
var isDark = darkThemeAllowed && OSThemeHelper.AppsUseDarkTheme();
|
|
||||||
|
|
||||||
tvp.HighlightingManager = isDark ? _hlmDark : _hlmLight;
|
|
||||||
if (isDark)
|
|
||||||
{
|
|
||||||
tvp.Background = Brushes.Transparent;
|
|
||||||
tvp.SetResourceReference(Control.ForegroundProperty, "WindowTextForeground");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// if os dark mode, but not AllowDarkTheme, make background light
|
|
||||||
tvp.Background = OSThemeHelper.AppsUseDarkTheme()
|
|
||||||
? new SolidColorBrush(Color.FromArgb(175, 255, 255, 255))
|
|
||||||
: Brushes.Transparent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
file static class EmbeddedResource
|
|
||||||
{
|
|
||||||
public static string GetFileNameWithoutExtension(string resourceName)
|
|
||||||
{
|
|
||||||
// Requires the embedded resource file name
|
|
||||||
// must have a file extension and have only one '.' character
|
|
||||||
int start = int.MinValue, end = int.MinValue;
|
|
||||||
|
|
||||||
for (int i = resourceName.Length - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (resourceName[i] == '.')
|
|
||||||
{
|
|
||||||
if (end == int.MinValue)
|
|
||||||
{
|
|
||||||
end = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start == int.MinValue)
|
|
||||||
{
|
|
||||||
start = i + 1; // Exinclude '.' character
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((start != int.MinValue) && (end != int.MinValue))
|
|
||||||
{
|
|
||||||
return resourceName.Substring(start, end - start);
|
|
||||||
}
|
|
||||||
return resourceName;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -62,6 +62,7 @@
|
|||||||
<PackageReference Include="AvalonEdit" Version="6.3.1.120" />
|
<PackageReference Include="AvalonEdit" Version="6.3.1.120" />
|
||||||
<PackageReference Include="UTF.Unknown" Version="2.5.1" />
|
<PackageReference Include="UTF.Unknown" Version="2.5.1" />
|
||||||
<Reference Include="WindowsBase" />
|
<Reference Include="WindowsBase" />
|
||||||
|
<Reference Include="System.ComponentModel.Composition" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
<SyntaxDefinition name="XML" extensions=".xml;.xsl;.xslt;.xsd;.syn;.lang;.manifest;.config;.addin;.xshd;.wxs;.wxi;.wxl;.proj;.csproj;.vbproj;.resx;.user;.ilproj;.booproj;.build;.xfrm;.targets;.axaml;.xaml;.xpt;.xft;.map;.wsdl;.disco;.ascx;.atom;.bpmn;.cpt;.csl;.props">
|
<SyntaxDefinition name="XML" extensions=".xml;.xsl;.xslt;.xsd;.syn;.lang;.manifest;.config;.addin;.xshd;.wxs;.wxi;.wxl;.proj;.csproj;.vbproj;.resx;.user;.ilproj;.booproj;.build;.xfrm;.targets;.axaml;.xaml;.xpt;.xft;.map;.wsdl;.disco;.ascx;.atom;.bpmn;.cpt;.csl;.props;">
|
||||||
|
|
||||||
<Environment>
|
<Environment>
|
||||||
<Default color="Black" bgcolor="#FFFFFF"/>
|
<Default color="Black" bgcolor="#FFFFFF"/>
|
||||||
|
@@ -17,35 +17,29 @@
|
|||||||
|
|
||||||
using ICSharpCode.AvalonEdit;
|
using ICSharpCode.AvalonEdit;
|
||||||
using ICSharpCode.AvalonEdit.Document;
|
using ICSharpCode.AvalonEdit.Document;
|
||||||
using ICSharpCode.AvalonEdit.Highlighting;
|
|
||||||
using ICSharpCode.AvalonEdit.Rendering;
|
using ICSharpCode.AvalonEdit.Rendering;
|
||||||
using ICSharpCode.AvalonEdit.Search;
|
using ICSharpCode.AvalonEdit.Search;
|
||||||
using QuickLook.Common.Helpers;
|
using QuickLook.Common.Helpers;
|
||||||
using QuickLook.Common.Plugin;
|
using QuickLook.Common.Plugin;
|
||||||
|
using QuickLook.Plugin.TextViewer.Detectors;
|
||||||
|
using QuickLook.Plugin.TextViewer.Themes;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Threading;
|
using System.Windows.Threading;
|
||||||
using UtfUnknown;
|
|
||||||
|
|
||||||
namespace QuickLook.Plugin.TextViewer;
|
namespace QuickLook.Plugin.TextViewer;
|
||||||
|
|
||||||
public class TextViewerPanel : TextEditor, IDisposable
|
public partial class TextViewerPanel : TextEditor, IDisposable
|
||||||
{
|
{
|
||||||
private readonly ContextObject _context;
|
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private HighlightingManager highlightingManager = HighlightingManager.Instance;
|
|
||||||
|
|
||||||
public TextViewerPanel(string path, ContextObject context)
|
public TextViewerPanel()
|
||||||
{
|
{
|
||||||
_context = context;
|
|
||||||
|
|
||||||
FontSize = 14;
|
FontSize = 14;
|
||||||
ShowLineNumbers = true;
|
ShowLineNumbers = true;
|
||||||
WordWrap = true;
|
WordWrap = true;
|
||||||
@@ -79,14 +73,6 @@ public class TextViewerPanel : TextEditor, IDisposable
|
|||||||
TextArea.TextView.ElementGenerators.Add(new TruncateLongLines());
|
TextArea.TextView.ElementGenerators.Add(new TruncateLongLines());
|
||||||
|
|
||||||
SearchPanel.Install(this);
|
SearchPanel.Install(this);
|
||||||
|
|
||||||
LoadFileAsync(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HighlightingManager HighlightingManager
|
|
||||||
{
|
|
||||||
get => highlightingManager;
|
|
||||||
set => highlightingManager = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@@ -146,7 +132,7 @@ public class TextViewerPanel : TextEditor, IDisposable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LoadFileAsync(string path)
|
public void LoadFileAsync(string path, ContextObject context)
|
||||||
{
|
{
|
||||||
_ = Task.Run(() =>
|
_ = Task.Run(() =>
|
||||||
{
|
{
|
||||||
@@ -173,15 +159,14 @@ public class TextViewerPanel : TextEditor, IDisposable
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (fileTooLong)
|
if (fileTooLong)
|
||||||
_context.Title += " (0 ~ 5MB)";
|
context.Title += " (0 ~ 5MB)";
|
||||||
|
|
||||||
var bufferCopy = buffer.ToArray();
|
var bufferCopy = buffer.ToArray();
|
||||||
buffer.Dispose();
|
buffer.Dispose();
|
||||||
|
|
||||||
var result = CharsetDetector.DetectFromBytes(bufferCopy);
|
var encoding = EncodingDetector.DetectFromBytes(bufferCopy);
|
||||||
var encoding = result.DoubleDetectFromResult(bufferCopy); // Fix issues
|
var text = encoding.GetString(bufferCopy);
|
||||||
|
var doc = new TextDocument(text);
|
||||||
var doc = new TextDocument(encoding.GetString(bufferCopy));
|
|
||||||
doc.SetOwnerThread(Dispatcher.Thread);
|
doc.SetOwnerThread(Dispatcher.Thread);
|
||||||
|
|
||||||
if (_disposed)
|
if (_disposed)
|
||||||
@@ -189,68 +174,30 @@ public class TextViewerPanel : TextEditor, IDisposable
|
|||||||
|
|
||||||
Dispatcher.BeginInvoke(() =>
|
Dispatcher.BeginInvoke(() =>
|
||||||
{
|
{
|
||||||
|
var extension = Path.GetExtension(path);
|
||||||
|
var highlighting = HighlightingThemeManager.GetHighlightingByExtensionOrDetector(extension, text);
|
||||||
|
|
||||||
Encoding = encoding;
|
Encoding = encoding;
|
||||||
SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength
|
SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength
|
||||||
? null
|
? null
|
||||||
: HighlightingManager?.GetDefinitionByExtension(Path.GetExtension(path));
|
: highlighting.SyntaxHighlighting;
|
||||||
Document = doc;
|
Document = doc;
|
||||||
|
|
||||||
_context.IsBusy = false;
|
if (highlighting.IsDark)
|
||||||
|
{
|
||||||
|
Background = Brushes.Transparent;
|
||||||
|
SetResourceReference(ForegroundProperty, "WindowTextForeground");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// if os dark mode, but not AllowDarkTheme, make background light
|
||||||
|
Background = OSThemeHelper.AppsUseDarkTheme()
|
||||||
|
? new SolidColorBrush(Color.FromArgb(175, 255, 255, 255))
|
||||||
|
: Brushes.Transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.IsBusy = false;
|
||||||
}, DispatcherPriority.Render);
|
}, DispatcherPriority.Render);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file static class DetectionExtensions
|
|
||||||
{
|
|
||||||
public static Encoding DoubleDetectFromResult(this DetectionResult result, byte[] buffer)
|
|
||||||
{
|
|
||||||
// Determine the highest confidence encoding, or fallback to ANSI
|
|
||||||
var encoding = result.Detected?.Encoding ?? Encoding.Default;
|
|
||||||
|
|
||||||
// When mixing encodings, one of the encodings may gain higher confidence
|
|
||||||
// In this case, we should return to encodings UTF8 / UTF32 / ANSI
|
|
||||||
// https://github.com/QL-Win/QuickLook/issues/769
|
|
||||||
if (encoding != Encoding.UTF8 && encoding != Encoding.UTF32 && encoding != Encoding.Default)
|
|
||||||
{
|
|
||||||
if (result.Details.Any(detail => detail.Encoding == Encoding.UTF8))
|
|
||||||
{
|
|
||||||
encoding = Encoding.UTF8;
|
|
||||||
}
|
|
||||||
else if (result.Details.Any(detail => detail.Encoding == Encoding.UTF32))
|
|
||||||
{
|
|
||||||
encoding = Encoding.UTF32;
|
|
||||||
}
|
|
||||||
else if (result.Details.Any(detail => detail.Encoding == Encoding.Default))
|
|
||||||
{
|
|
||||||
encoding = Encoding.Default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the text is too short and lacks a BOM
|
|
||||||
// In this case, we should fallback to an encoding if it is not recognized as UTF8 / UTF32 / ANSI
|
|
||||||
// https://github.com/QL-Win/QuickLook/issues/471
|
|
||||||
// https://github.com/QL-Win/QuickLook/issues/600
|
|
||||||
// https://github.com/QL-Win/QuickLook/issues/954
|
|
||||||
if (buffer.Length <= 50)
|
|
||||||
{
|
|
||||||
if (encoding != Encoding.UTF8 && encoding != Encoding.UTF32 && encoding != Encoding.Default)
|
|
||||||
{
|
|
||||||
if (!Encoding.UTF8.GetString(buffer).Contains("\uFFFD"))
|
|
||||||
{
|
|
||||||
encoding = Encoding.UTF8;
|
|
||||||
}
|
|
||||||
else if (!Encoding.UTF32.GetString(buffer).Contains("\uFFFD"))
|
|
||||||
{
|
|
||||||
encoding = Encoding.UTF32;
|
|
||||||
}
|
|
||||||
else if (!Encoding.Default.GetString(buffer).Contains("\uFFFD"))
|
|
||||||
{
|
|
||||||
encoding = Encoding.Default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return encoding;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -0,0 +1,38 @@
|
|||||||
|
// Copyright © 2017-2025 QL-Win Contributors
|
||||||
|
//
|
||||||
|
// This file is part of QuickLook program.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using ICSharpCode.AvalonEdit.Highlighting;
|
||||||
|
|
||||||
|
namespace QuickLook.Plugin.TextViewer.Themes;
|
||||||
|
|
||||||
|
public sealed class HighlightingTheme
|
||||||
|
{
|
||||||
|
public static HighlightingTheme Default => new() // No case should be cached
|
||||||
|
{
|
||||||
|
HighlightingManager = HighlightingManager.Instance,
|
||||||
|
};
|
||||||
|
|
||||||
|
public string Theme { get; set; }
|
||||||
|
|
||||||
|
public bool IsDark => Theme == nameof(HighlightingThemeManager.Dark);
|
||||||
|
|
||||||
|
public HighlightingManager HighlightingManager { get; set; }
|
||||||
|
|
||||||
|
public IHighlightingDefinition SyntaxHighlighting { get; set; }
|
||||||
|
|
||||||
|
public string Extension { get; set; }
|
||||||
|
}
|
@@ -0,0 +1,200 @@
|
|||||||
|
// Copyright © 2017-2025 QL-Win Contributors
|
||||||
|
//
|
||||||
|
// This file is part of QuickLook program.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using ICSharpCode.AvalonEdit.Highlighting;
|
||||||
|
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
|
||||||
|
using QuickLook.Common.Helpers;
|
||||||
|
using QuickLook.Plugin.TextViewer.Detectors;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
|
namespace QuickLook.Plugin.TextViewer.Themes;
|
||||||
|
|
||||||
|
public class HighlightingThemeManager
|
||||||
|
{
|
||||||
|
public static HighlightingManager Light { get; internal set; }
|
||||||
|
|
||||||
|
public static HighlightingManager Dark { get; internal set; }
|
||||||
|
|
||||||
|
public static void Initialize()
|
||||||
|
{
|
||||||
|
InitHighlightingManager();
|
||||||
|
AddHighlightingManager(Light, nameof(Light));
|
||||||
|
AddHighlightingManager(Dark, nameof(Dark));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HighlightingTheme GetHighlightingByExtensionOrDetector(string extension, string text = null)
|
||||||
|
{
|
||||||
|
if (Light is null || Dark is null) return HighlightingTheme.Default;
|
||||||
|
|
||||||
|
var highlightingTheme = GetDefinitionByExtension(nameof(Dark), extension);
|
||||||
|
|
||||||
|
if (highlightingTheme == null)
|
||||||
|
{
|
||||||
|
highlightingTheme = GetDefinitionByExtension(nameof(Light), extension);
|
||||||
|
|
||||||
|
if (highlightingTheme == null)
|
||||||
|
{
|
||||||
|
var useFormatDetector = SettingHelper.Get("UseFormatDetector", true, "QuickLook.Plugin.TextViewer");
|
||||||
|
|
||||||
|
if (useFormatDetector && FormatDetector.Detect(text)?.Extension is string detectExtension)
|
||||||
|
{
|
||||||
|
highlightingTheme = GetDefinitionByExtension(nameof(Dark), detectExtension)
|
||||||
|
?? GetDefinitionByExtension(nameof(Light), detectExtension);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsupported highlighting
|
||||||
|
highlightingTheme ??= HighlightingTheme.Default;
|
||||||
|
|
||||||
|
var darkThemeAllowed = SettingHelper.Get("AllowDarkTheme", highlightingTheme.IsDark, "QuickLook.Plugin.TextViewer");
|
||||||
|
var isDark = darkThemeAllowed && OSThemeHelper.AppsUseDarkTheme();
|
||||||
|
|
||||||
|
// The current environment does not require dark mode so revert to light mode
|
||||||
|
if (!isDark && highlightingTheme.IsDark)
|
||||||
|
{
|
||||||
|
highlightingTheme.Theme = nameof(Light);
|
||||||
|
highlightingTheme.HighlightingManager = Light;
|
||||||
|
highlightingTheme.SyntaxHighlighting
|
||||||
|
// The extension that supports dark mode must support light mode also
|
||||||
|
= Light.GetDefinitionByExtension(highlightingTheme.Extension);
|
||||||
|
}
|
||||||
|
|
||||||
|
return highlightingTheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HighlightingTheme GetDefinitionByExtension(string theme, string extension)
|
||||||
|
{
|
||||||
|
var highlightingManager = theme == nameof(Dark) ? Dark : Light;
|
||||||
|
var def = highlightingManager.GetDefinitionByExtension(extension);
|
||||||
|
|
||||||
|
if (def != null)
|
||||||
|
{
|
||||||
|
return new HighlightingTheme()
|
||||||
|
{
|
||||||
|
Theme = theme,
|
||||||
|
HighlightingManager = highlightingManager,
|
||||||
|
SyntaxHighlighting = def,
|
||||||
|
Extension = extension,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void InitHighlightingManager()
|
||||||
|
{
|
||||||
|
Light = new HighlightingManager();
|
||||||
|
Dark = new HighlightingManager();
|
||||||
|
|
||||||
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||||
|
string[] resourceNames = assembly.GetManifestResourceNames();
|
||||||
|
|
||||||
|
foreach (var resourceName in resourceNames.Where(name => name.Contains(".Syntax.")))
|
||||||
|
{
|
||||||
|
using Stream s = assembly.GetManifestResourceStream(resourceName);
|
||||||
|
|
||||||
|
if (s == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Debug.WriteLine(resourceName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var hlm = resourceName.Contains(".Syntax.Dark.") ? Dark : Light;
|
||||||
|
var name = EmbeddedResource.GetFileNameWithoutExtension(resourceName);
|
||||||
|
using var reader = new XmlTextReader(s);
|
||||||
|
var xshd = HighlightingLoader.LoadXshd(reader);
|
||||||
|
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
|
||||||
|
if (xshd.Extensions.Count > 0)
|
||||||
|
hlm.RegisterHighlighting(name, [.. xshd.Extensions], highlightingDefinition);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ProcessHelper.WriteLog(e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddHighlightingManager(HighlightingManager hlm, string dirName)
|
||||||
|
{
|
||||||
|
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||||
|
if (string.IsNullOrEmpty(assemblyPath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var syntaxPath = Path.Combine(assemblyPath, "Syntax", dirName);
|
||||||
|
if (!Directory.Exists(syntaxPath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var file in Directory.EnumerateFiles(syntaxPath, "*.xshd").OrderBy(f => f))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Debug.WriteLine(file);
|
||||||
|
var ext = Path.GetFileNameWithoutExtension(file);
|
||||||
|
using Stream s = File.OpenRead(Path.GetFullPath(file));
|
||||||
|
using var reader = new XmlTextReader(s);
|
||||||
|
var xshd = HighlightingLoader.LoadXshd(reader);
|
||||||
|
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
|
||||||
|
if (xshd.Extensions.Count > 0)
|
||||||
|
hlm.RegisterHighlighting(ext, [.. xshd.Extensions], highlightingDefinition);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ProcessHelper.WriteLog(e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file static class EmbeddedResource
|
||||||
|
{
|
||||||
|
public static string GetFileNameWithoutExtension(string resourceName)
|
||||||
|
{
|
||||||
|
// Requires the embedded resource file name
|
||||||
|
// must have a file extension and have only one '.' character
|
||||||
|
int start = int.MinValue, end = int.MinValue;
|
||||||
|
|
||||||
|
for (int i = resourceName.Length - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (resourceName[i] == '.')
|
||||||
|
{
|
||||||
|
if (end == int.MinValue)
|
||||||
|
{
|
||||||
|
end = i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start == int.MinValue)
|
||||||
|
{
|
||||||
|
start = i + 1; // Exinclude '.' character
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((start != int.MinValue) && (end != int.MinValue))
|
||||||
|
{
|
||||||
|
return resourceName.Substring(start, end - start);
|
||||||
|
}
|
||||||
|
return resourceName;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user