Skip to content

Commit 7e7630a

Browse files
author
exptprua@perstorp.com
committed
Refactor Markdown Rendering and Improve Performance
- Replaced legacy string-based Markdown rendering with a modern Markdig-based renderer in MarkdownRenderer.cs for improved performance and reduced VT escape sequence issues. - Removed unused legacy rendering code and streamlined the Render method. - Updated Rows class to use an empty array initializer. - Enhanced StandardRenderer to utilize StringBuilder pooling for better memory management. - Refactored TextMateProcessor to support cancellation tokens and progress reporting in batch processing methods. - Updated TokenProcessor to handle escaping of markup characters and improved debugging capabilities. - Added comprehensive unit tests for StandardRenderer, TextMateProcessor, and TokenProcessor to ensure functionality and performance. - Cleaned up CacheManager tests to verify caching behavior and grammar retrieval. - Updated project file to generate documentation.
1 parent 0cc914f commit 7e7630a

27 files changed

Lines changed: 767 additions & 286 deletions

Module/PSTextMate.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'PSTextMate.dll'
3-
ModuleVersion = '0.0.3'
3+
ModuleVersion = '0.1.0'
44
GUID = '5ba21f1d-5ca4-49df-9a07-a2ad379feb00'
55
Author = 'trackd'
66
CompanyName = 'trackd'

build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Get-ChildItem -Path (Join-Path -Path $outputfolder -ChildPath 'runtimes' | Join-
3636
Get-ChildItem -Path (Join-Path -Path $outputfolder -ChildPath 'runtimes' | Join-Path -ChildPath 'linux-x64' | Join-Path -ChildPath 'native') -Filter *.so | Copy-Item -Destination $moduleLibFolder -Force
3737
Move-Item (Join-Path -Path $outputfolder -ChildPath 'PSTextMate.dll') -Destination (Split-Path $moduleLibFolder) -Force
3838
Get-ChildItem -Path $outputfolder -File |
39-
Where-Object { -Not $_.Name.StartsWith('System.Text') -And $_.Extension -notin '.json','.pdb' } |
39+
Where-Object { -Not $_.Name.StartsWith('System.Text') -And $_.Extension -notin '.json','.pdb','.xml' } |
4040
Move-Item -Destination $moduleLibFolder -Force
4141

4242
Pop-Location

src/Cmdlets/DebugCmdlets.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ namespace PwshSpectreConsole.TextMate.Cmdlets;
1111
/// Provides detailed diagnostic information for troubleshooting rendering issues.
1212
/// </summary>
1313
[Cmdlet(VerbsDiagnostic.Debug, "TextMate", DefaultParameterSetName = "String")]
14+
[OutputType(typeof(Test.TextMateDebug))]
1415
public sealed class DebugTextMateCmdlet : PSCmdlet
1516
{
1617
private readonly List<string> _inputObjectBuffer = new();
1718

19+
/// <summary>
20+
/// String content to debug.
21+
/// </summary>
1822
[Parameter(
1923
Mandatory = true,
2024
ValueFromPipeline = true,
@@ -23,6 +27,9 @@ public sealed class DebugTextMateCmdlet : PSCmdlet
2327
[AllowEmptyString]
2428
public string InputObject { get; set; } = null!;
2529

30+
/// <summary>
31+
/// Path to file to debug.
32+
/// </summary>
2633
[Parameter(
2734
Mandatory = true,
2835
ValueFromPipelineByPropertyName = true,
@@ -33,15 +40,24 @@ public sealed class DebugTextMateCmdlet : PSCmdlet
3340
[Alias("FullName")]
3441
public string Path { get; set; } = null!;
3542

43+
/// <summary>
44+
/// TextMate language ID (default: 'powershell').
45+
/// </summary>
3646
[Parameter(
3747
ParameterSetName = "String"
3848
)]
3949
[ValidateSet(typeof(TextMateLanguages))]
4050
public string Language { get; set; } = "powershell";
4151

52+
/// <summary>
53+
/// Color theme for debug output (default: Dark).
54+
/// </summary>
4255
[Parameter()]
4356
public ThemeName Theme { get; set; } = ThemeName.Dark;
4457

58+
/// <summary>
59+
/// Override file extension for language detection.
60+
/// </summary>
4561
[Parameter(
4662
ParameterSetName = "Path"
4763
)]
@@ -50,6 +66,9 @@ public sealed class DebugTextMateCmdlet : PSCmdlet
5066
[Alias("As")]
5167
public string ExtensionOverride { get; set; } = null!;
5268

69+
/// <summary>
70+
/// Processes each input record from the pipeline.
71+
/// </summary>
5372
protected override void ProcessRecord()
5473
{
5574
if (ParameterSetName == "String" && InputObject is not null)
@@ -58,6 +77,9 @@ protected override void ProcessRecord()
5877
}
5978
}
6079

80+
/// <summary>
81+
/// Finalizes processing and outputs debug information.
82+
/// </summary>
6183
protected override void EndProcessing()
6284
{
6385
try
@@ -98,33 +120,52 @@ protected override void EndProcessing()
98120
/// Cmdlet for debugging individual TextMate tokens and their properties.
99121
/// Provides low-level token analysis for detailed syntax highlighting inspection.
100122
/// </summary>
123+
[OutputType(typeof(Core.TokenDebugInfo))]
101124
[Cmdlet(VerbsDiagnostic.Debug, "TextMateTokens", DefaultParameterSetName = "String")]
102125
public sealed class DebugTextMateTokensCmdlet : PSCmdlet
103126
{
104127
private readonly List<string> _inputObjectBuffer = new();
105128

129+
/// <summary>
130+
/// String content to analyze tokens from.
131+
/// </summary>
106132
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "String")]
107133
[AllowEmptyString]
108134
public string InputObject { get; set; } = null!;
109135

136+
/// <summary>
137+
/// Path to file to analyze tokens from.
138+
/// </summary>
110139
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Path", Position = 0)]
111140
[ValidateNotNullOrEmpty]
112141
[Alias("FullName")]
113142
public string Path { get; set; } = null!;
114143

144+
/// <summary>
145+
/// TextMate language ID (default: 'powershell').
146+
/// </summary>
115147
[Parameter(ParameterSetName = "String")]
116148
[ValidateSet(typeof(TextMateLanguages))]
117149
public string Language { get; set; } = "powershell";
118150

151+
/// <summary>
152+
/// Color theme for token analysis (default: DarkPlus).
153+
/// </summary>
119154
[Parameter()]
120155
public ThemeName Theme { get; set; } = ThemeName.DarkPlus;
121156

157+
/// <summary>
158+
/// Override file extension for language detection.
159+
/// </summary>
122160
[Parameter(ParameterSetName = "Path")]
123161
[TextMateExtensionTransform()]
124162
[ValidateSet(typeof(TextMateExtensions))]
125163
[Alias("As")]
126164
public string ExtensionOverride { get; set; } = null!;
127165

166+
/// <summary>
167+
/// Processes each input record from the pipeline.
168+
/// </summary>
128169
protected override void ProcessRecord()
129170
{
130171
if (ParameterSetName == "String" && InputObject is not null)
@@ -133,6 +174,9 @@ protected override void ProcessRecord()
133174
}
134175
}
135176

177+
/// <summary>
178+
/// Finalizes processing and outputs token debug information.
179+
/// </summary>
136180
protected override void EndProcessing()
137181
{
138182
try
@@ -176,6 +220,9 @@ protected override void EndProcessing()
176220
[Cmdlet(VerbsDiagnostic.Debug, "SixelSupport")]
177221
public sealed class DebugSixelSupportCmdlet : PSCmdlet
178222
{
223+
/// <summary>
224+
/// Processes the cmdlet and outputs Sixel support diagnostic information.
225+
/// </summary>
179226
protected override void ProcessRecord()
180227
{
181228
try
@@ -214,12 +261,21 @@ protected override void ProcessRecord()
214261
[Cmdlet(VerbsDiagnostic.Test, "ImageRendering")]
215262
public sealed class TestImageRenderingCmdlet : PSCmdlet
216263
{
264+
/// <summary>
265+
/// URL or path to image for rendering test.
266+
/// </summary>
217267
[Parameter(Mandatory = true, Position = 0)]
218268
public string ImageUrl { get; set; } = null!;
219269

220-
[Parameter()]
270+
/// <summary>
271+
/// Alternative text for the image.
272+
/// </summary>
273+
[Parameter(Position = 1)]
221274
public string AltText { get; set; } = "Test Image";
222275

276+
/// <summary>
277+
/// Processes the cmdlet and tests image rendering.
278+
/// </summary>
223279
protected override void ProcessRecord()
224280
{
225281
try

src/Cmdlets/ShowTextMateCmdlet.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,29 @@ namespace PwshSpectreConsole.TextMate.Cmdlets;
1313
/// </summary>
1414
[Cmdlet(VerbsCommon.Show, "TextMate", DefaultParameterSetName = "String")]
1515
[Alias("st","Show-Code")]
16-
[OutputType(typeof(RenderableBatch))]
16+
[OutputType(typeof(Spectre.Console.Rows), ParameterSetName = new[] { "String" })]
17+
[OutputType(typeof(Spectre.Console.Rows), ParameterSetName = new[] { "Path" })]
18+
[OutputType(typeof(RenderableBatch), ParameterSetName = new[] { "Path" })]
1719
public sealed class ShowTextMateCmdlet : PSCmdlet
1820
{
1921
private static readonly string[] NewLineSplit = ["\r\n", "\n", "\r"];
2022
private readonly List<string> _inputObjectBuffer = [];
2123
private string? _sourceExtensionHint;
2224

25+
/// <summary>
26+
/// String content to render with syntax highlighting.
27+
/// </summary>
2328
[Parameter(
2429
Mandatory = true,
2530
ValueFromPipeline = true,
2631
ParameterSetName = "String"
2732
)]
2833
[AllowEmptyString]
29-
[ValidateNotNull]
30-
public string? InputObject { get; set; }
34+
public string InputObject { get; set; } = string.Empty;
3135

36+
/// <summary>
37+
/// Path to file to render with syntax highlighting.
38+
/// </summary>
3239
[Parameter(
3340
Mandatory = true,
3441
ValueFromPipelineByPropertyName = true,
@@ -37,8 +44,12 @@ public sealed class ShowTextMateCmdlet : PSCmdlet
3744
)]
3845
[ValidateNotNullOrEmpty]
3946
[Alias("FullName")]
40-
public string? Path { get; set; }
47+
public string Path { get; set; } = string.Empty;
4148

49+
/// <summary>
50+
/// TextMate language ID for syntax highlighting (e.g., 'powershell', 'csharp', 'python').
51+
/// If not specified, detected from file extension or content.
52+
/// </summary>
4253
[Parameter(
4354
ParameterSetName = "String"
4455
)]
@@ -48,20 +59,35 @@ public sealed class ShowTextMateCmdlet : PSCmdlet
4859
[ArgumentCompleter(typeof(LanguageCompleter))]
4960
public string? Language { get; set; }
5061

62+
/// <summary>
63+
/// Color theme to use for syntax highlighting.
64+
/// </summary>
5165
[Parameter()]
5266
public ThemeName Theme { get; set; } = ThemeName.DarkPlus;
5367

68+
/// <summary>
69+
/// Returns the rendered output object instead of writing directly to host.
70+
/// </summary>
5471
[Parameter]
5572
public SwitchParameter PassThru { get; set; }
5673

74+
/// <summary>
75+
/// Enables streaming mode for large files, processing in batches.
76+
/// </summary>
5777
[Parameter(
5878
ParameterSetName = "Path"
5979
)]
6080
public SwitchParameter Stream { get; set; }
6181

82+
/// <summary>
83+
/// Number of lines to process per batch when streaming (default: 1000).
84+
/// </summary>
6285
[Parameter(ParameterSetName = "Path")]
6386
public int BatchSize { get; set; } = 1000;
6487

88+
/// <summary>
89+
/// Processes each input record from the pipeline.
90+
/// </summary>
6591
protected override void ProcessRecord()
6692
{
6793
if (ParameterSetName == "String" && InputObject is not null)
@@ -111,6 +137,9 @@ protected override void ProcessRecord()
111137
}
112138
}
113139

140+
/// <summary>
141+
/// Finalizes processing after all pipeline records have been processed.
142+
/// </summary>
114143
protected override void EndProcessing()
115144
{
116145
// For Path parameter set, each record is processed in ProcessRecord to support streaming multiple files.

src/Cmdlets/SupportCmdlets.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,27 @@ namespace PwshSpectreConsole.TextMate.Cmdlets;
1010
[Cmdlet(VerbsDiagnostic.Test, "SupportedTextMate")]
1111
public sealed class TestSupportedTextMateCmdlet : PSCmdlet
1212
{
13+
/// <summary>
14+
/// File extension to test for support (e.g., '.ps1').
15+
/// </summary>
1316
[Parameter()]
1417
public string? Extension { get; set; }
1518

19+
/// <summary>
20+
/// Language ID to test for support (e.g., 'powershell').
21+
/// </summary>
1622
[Parameter()]
1723
public string? Language { get; set; }
1824

25+
/// <summary>
26+
/// File path to test for support.
27+
/// </summary>
1928
[Parameter()]
2029
public string? File { get; set; }
2130

31+
/// <summary>
32+
/// Finalizes processing and outputs support check results.
33+
/// </summary>
2234
protected override void EndProcessing()
2335
{
2436
if (!string.IsNullOrEmpty(File))
@@ -44,6 +56,9 @@ protected override void EndProcessing()
4456
[Cmdlet(VerbsCommon.Get, "SupportedTextMate")]
4557
public sealed class GetSupportedTextMateCmdlet : PSCmdlet
4658
{
59+
/// <summary>
60+
/// Finalizes processing and outputs all supported languages.
61+
/// </summary>
4762
protected override void EndProcessing()
4863
{
4964
WriteObject(TextMateHelper.AvailableLanguages, enumerateCollection: true);

src/Core/MarkdigSpectreMarkdownRenderer.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Core/Markdown/InlineProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Text;
22
using Markdig.Syntax.Inlines;
3-
using PwshSpectreConsole.TextMate.Core.Helpers;
3+
using PwshSpectreConsole.TextMate.Helpers;
44
using PwshSpectreConsole.TextMate.Extensions;
55
using Spectre.Console;
66
using TextMateSharp.Themes;

src/Core/Markdown/Renderers/ImageRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Reflection;
2-
using PwshSpectreConsole.TextMate.Core.Helpers;
2+
using PwshSpectreConsole.TextMate.Helpers;
33
using Spectre.Console;
44
using Spectre.Console.Rendering;
55

src/Core/MarkdownLinkFormatter.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)