Improve startup speed #1521
Some checks are pending
MSBuild / build (push) Waiting to run
MSBuild / publish (push) Blocked by required conditions

This commit is contained in:
ema
2025-05-25 03:45:17 +08:00
parent eed0c53e9c
commit b81b2f9ef8
2 changed files with 53 additions and 28 deletions

View File

@@ -47,8 +47,9 @@ public class Plugin : IViewer
// pre-load
var _ = new TextEditor();
_hlmLight = GetHighlightingManager(Themes.Light, "Light");
_hlmDark = GetHighlightingManager(Themes.Dark, "Dark");
InitHighlightingManager();
AddHighlightingManager(_hlmLight, "Light");
AddHighlightingManager(_hlmDark, "Dark");
// Implementation of the Search Panel Styled with Fluent Theme
{
@@ -78,15 +79,13 @@ public class Plugin : IViewer
// return true;
// otherwise, read the first 16KB, check if we can get something.
using (var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
const int bufferLength = 16 * 1024;
var buffer = new byte[bufferLength];
var size = s.Read(buffer, 0, bufferLength);
return IsText(buffer, size);
}
}
public void Prepare(string path, ContextObject context)
{
@@ -132,17 +131,15 @@ public class Plugin : IViewer
return true;
}
private HighlightingManager GetHighlightingManager(Themes theme, string dirName)
private void AddHighlightingManager(HighlightingManager hlm, string dirName)
{
var hlm = new HighlightingManager();
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (string.IsNullOrEmpty(assemblyPath))
return hlm;
return;
var syntaxPath = Path.Combine(assemblyPath, "Syntax", dirName);
if (!Directory.Exists(syntaxPath))
return hlm;
return;
foreach (var file in Directory.EnumerateFiles(syntaxPath, "*.xshd").OrderBy(f => f))
{
@@ -150,22 +147,52 @@ public class Plugin : IViewer
{
Debug.WriteLine(file);
var ext = Path.GetFileNameWithoutExtension(file);
using (Stream s = File.OpenRead(Path.GetFullPath(file)))
using (var reader = new XmlTextReader(s))
{
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.ToArray(), highlightingDefinition);
}
hlm.RegisterHighlighting(ext, [.. xshd.Extensions], highlightingDefinition);
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
}
}
}
return hlm;
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;
try
{
Debug.WriteLine(resourceName);
var hlm = resourceName.Contains(".Syntax.Dark.") ? _hlmDark : _hlmLight;
var ext = Path.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(ext, [.. xshd.Extensions], highlightingDefinition);
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
}
}
}
private void AssignHighlightingManager(string path, TextViewerPanel tvp, ContextObject context)

View File

@@ -73,9 +73,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Syntax\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Syntax\**" />
<None Include="Translations.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>