Skip to content

Commit 414f0e5

Browse files
committed
fix(cmdlets): 🐛 update input handling and line splitting
* Refactored `_inputObjectBuffer` handling in `TextMateCmdletBase` to use `TextMateHelper.SplitToLines`. * Improved `NormalizeToLines` method to handle multiline strings more effectively. * Added a new test case to validate splitting of multiline pipeline items in `Format-CSharp`. * Updated demo image path in README for consistency.
1 parent b645774 commit 414f0e5

5 files changed

Lines changed: 31 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ What it does
99
- Provides discovery and testing helpers for installed grammars, extensions, or language IDs.
1010
- Does inline Sixel images in markdown
1111

12-
![Demo](/assets/demo.png)
12+
![Demo](./assets/demo.png)
1313

1414
## Cmdlets
1515

src/Cmdlets/TextMateCmdletBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected override void ProcessRecord() {
101101
EnsureSourceHints();
102102
}
103103

104-
_inputObjectBuffer.Add(inputString);
104+
_inputObjectBuffer.AddRange(TextMateHelper.SplitToLines(inputString));
105105
}
106106
}
107107
else if (InputObject is not null) {
@@ -142,7 +142,7 @@ protected override void EndProcessing() {
142142
}
143143

144144
private HighlightedText? ProcessStringInput() {
145-
string[] lines = TextMateHelper.NormalizeToLines(_inputObjectBuffer);
145+
string[] lines = [.. _inputObjectBuffer];
146146

147147
if (lines.AllIsNullOrEmpty()) {
148148
WriteVerbose("All input strings are null or empty");

src/Utilities/Helpers.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,28 @@ static TextMateHelper() {
3636
throw new TypeInitializationException(nameof(TextMateHelper), ex);
3737
}
3838
}
39-
internal static string[] NormalizeToLines(List<string> buffer) {
40-
if (buffer.Count == 0) {
41-
return [];
39+
internal static string[] SplitToLines(string input) {
40+
if (input.Length == 0) {
41+
return [string.Empty];
4242
}
4343

44-
// Multiple strings in buffer - treat each as a line
45-
if (buffer.Count > 1) {
46-
return [.. buffer];
44+
if (input.Contains('\n') || input.Contains('\r')) {
45+
return input.Split(["\r\n", "\n", "\r"], StringSplitOptions.None);
4746
}
4847

49-
// Single string - check if it contains newlines
50-
string? single = buffer[0];
51-
if (string.IsNullOrEmpty(single)) {
52-
return single is not null ? [single] : [];
48+
return [input];
49+
}
50+
51+
internal static string[] NormalizeToLines(List<string> buffer) {
52+
if (buffer.Count == 0) {
53+
return [];
5354
}
5455

55-
// Split on newlines if present
56-
if (single.Contains('\n') || single.Contains('\r')) {
57-
return single.Split(["\r\n", "\n", "\r"], StringSplitOptions.None);
56+
var lines = new List<string>(buffer.Count * 2);
57+
foreach (string item in buffer) {
58+
lines.AddRange(SplitToLines(item));
5859
}
5960

60-
// Single string with no newlines
61-
return [single];
61+
return [.. lines];
6262
}
6363
}

tests/Format-CSharp.tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ BeforeAll {
22
if (-Not (Get-Module 'TextMate')) {
33
Import-Module (Join-Path $PSScriptRoot '..' 'output' 'TextMate.psd1') -ErrorAction Stop
44
}
5+
6+
Import-Module (Join-Path $PSScriptRoot 'testhelper.psm1') -Force
57
}
68

79
Describe 'Format-CSharp' {
@@ -32,6 +34,16 @@ Describe 'Format-CSharp' {
3234
Remove-Item -Force -ErrorAction SilentlyContinue $temp
3335
}
3436
}
37+
38+
It 'Splits multiline pipeline items into individual lines' {
39+
$s1 = "// a`npublic class A { }"
40+
$s2 = "// b`npublic class B { }"
41+
42+
$out = @($s1, $s2) | Format-CSharp
43+
44+
$out.LineCount | Should -Be 4
45+
}
46+
3547
It 'Should have Help and examples' {
3648
$help = Get-Help Format-CSharp -Full
3749
$help.Synopsis | Should -Not -BeNullOrEmpty

tests/testhelper.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function _GetSpectreRenderable {
1616
$console = [AnsiConsole]::Create($settings)
1717
$console.Write($RenderableObject)
1818
if ($EscapeAnsi) {
19-
return $writer.ToString() | _EscapeAnsi
19+
return [Host.PSHostUserInterface]::GetOutputString($writer.ToString(),$false)
2020
}
2121
$writer.ToString()
2222
}

0 commit comments

Comments
 (0)