mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-12 02:09:05 +00:00
Use format detector feature for TextViewer
This commit is contained in:
@@ -17,35 +17,29 @@
|
||||
|
||||
using ICSharpCode.AvalonEdit;
|
||||
using ICSharpCode.AvalonEdit.Document;
|
||||
using ICSharpCode.AvalonEdit.Highlighting;
|
||||
using ICSharpCode.AvalonEdit.Rendering;
|
||||
using ICSharpCode.AvalonEdit.Search;
|
||||
using QuickLook.Common.Helpers;
|
||||
using QuickLook.Common.Plugin;
|
||||
using QuickLook.Plugin.TextViewer.Detectors;
|
||||
using QuickLook.Plugin.TextViewer.Themes;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using UtfUnknown;
|
||||
|
||||
namespace QuickLook.Plugin.TextViewer;
|
||||
|
||||
public class TextViewerPanel : TextEditor, IDisposable
|
||||
public partial class TextViewerPanel : TextEditor, IDisposable
|
||||
{
|
||||
private readonly ContextObject _context;
|
||||
private bool _disposed;
|
||||
private HighlightingManager highlightingManager = HighlightingManager.Instance;
|
||||
|
||||
public TextViewerPanel(string path, ContextObject context)
|
||||
public TextViewerPanel()
|
||||
{
|
||||
_context = context;
|
||||
|
||||
FontSize = 14;
|
||||
ShowLineNumbers = true;
|
||||
WordWrap = true;
|
||||
@@ -79,14 +73,6 @@ public class TextViewerPanel : TextEditor, IDisposable
|
||||
TextArea.TextView.ElementGenerators.Add(new TruncateLongLines());
|
||||
|
||||
SearchPanel.Install(this);
|
||||
|
||||
LoadFileAsync(path);
|
||||
}
|
||||
|
||||
public HighlightingManager HighlightingManager
|
||||
{
|
||||
get => highlightingManager;
|
||||
set => highlightingManager = value;
|
||||
}
|
||||
|
||||
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(() =>
|
||||
{
|
||||
@@ -173,15 +159,14 @@ public class TextViewerPanel : TextEditor, IDisposable
|
||||
return;
|
||||
|
||||
if (fileTooLong)
|
||||
_context.Title += " (0 ~ 5MB)";
|
||||
context.Title += " (0 ~ 5MB)";
|
||||
|
||||
var bufferCopy = buffer.ToArray();
|
||||
buffer.Dispose();
|
||||
|
||||
var result = CharsetDetector.DetectFromBytes(bufferCopy);
|
||||
var encoding = result.DoubleDetectFromResult(bufferCopy); // Fix issues
|
||||
|
||||
var doc = new TextDocument(encoding.GetString(bufferCopy));
|
||||
var encoding = EncodingDetector.DetectFromBytes(bufferCopy);
|
||||
var text = encoding.GetString(bufferCopy);
|
||||
var doc = new TextDocument(text);
|
||||
doc.SetOwnerThread(Dispatcher.Thread);
|
||||
|
||||
if (_disposed)
|
||||
@@ -189,68 +174,30 @@ public class TextViewerPanel : TextEditor, IDisposable
|
||||
|
||||
Dispatcher.BeginInvoke(() =>
|
||||
{
|
||||
var extension = Path.GetExtension(path);
|
||||
var highlighting = HighlightingThemeManager.GetHighlightingByExtensionOrDetector(extension, text);
|
||||
|
||||
Encoding = encoding;
|
||||
SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength
|
||||
? null
|
||||
: HighlightingManager?.GetDefinitionByExtension(Path.GetExtension(path));
|
||||
: highlighting.SyntaxHighlighting;
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user