Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions src/System.Management.Automation/help/HelpCommentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,47 +495,25 @@ private void BuildSyntaxForParameterSet(XmlElement command, XmlElement syntax, M

private static void GetExampleSections(string content, out string prompt_str, out string code_str, out string remarks_str)
{
prompt_str = code_str = string.Empty;
StringBuilder builder = new StringBuilder();
string default_prompt_str = "PS > ";

int collectingPart = 1;
foreach (char c in content)
var promptMatch = Regex.Match(content, "^.*?>");
prompt_str = promptMatch.Success ? promptMatch.Value : default_prompt_str;
if (promptMatch.Success)
{
if (c == '>' && collectingPart == 1)
{
builder.Append(c);
prompt_str = builder.ToString().Trim();
builder = new StringBuilder();
++collectingPart;
continue;
}

if (c == '\n' && collectingPart < 3)
{
if (collectingPart == 1)
{
prompt_str = default_prompt_str;
}

code_str = builder.ToString().Trim();
builder = new StringBuilder();
collectingPart = 3;
continue;
}

builder.Append(c);
content = content.Substring(prompt_str.Length);
}

if (collectingPart == 1)
var codeAndRemarksMatch = Regex.Match(content, "^(?<code>.*?)\r?\n\r?\n(?<remarks>.*)$", RegexOptions.Singleline);
if (codeAndRemarksMatch.Success)
{
prompt_str = default_prompt_str;
code_str = builder.ToString().Trim();
remarks_str = string.Empty;
code_str = codeAndRemarksMatch.Groups["code"].Value.Trim();
remarks_str = codeAndRemarksMatch.Groups["remarks"].Value;
}
else
{
remarks_str = builder.ToString();
code_str = content.Trim();
remarks_str = string.Empty;
}
}

Expand Down
59 changes: 59 additions & 0 deletions test/powershell/Language/Scripting/ScriptHelp.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,63 @@ Describe 'get-help other tests' -Tags "CI" {
(Get-Help foo -Examples).examples.example.introduction.Text | Should -BeExactly "PS > "
}
}

Context 'get-help -Examples multi-line code block should be handled' {
function foo {
<#
.EXAMPLE
$a = Get-Service
$a | group Status

.EXAMPLE
PS> $a = Get-Service
PS> $a | group Status

Explanation.

.EXAMPLE

PS> Get-Service
Output

.EXAMPLE
PS> Get-Service
Output

Explanation.
Second line.

.EXAMPLE
PS> Get-Service
Output

Explanation.

Next section.
#>
param()
}

$x = get-help foo
It '$x.examples.example[0].introduction[0].text' { $x.examples.example[0].introduction[0].text | Should -BeExactly "PS > " }
It '$x.examples.example[0].code' { $x.examples.example[0].code | Should -BeExactly "`$a = Get-Service`n`$a | group Status" }
It '$x.examples.example[0].remarks[0].text' { $x.examples.example[0].remarks[0].text | Should -BeNullOrEmpty }
It '$x.examples.example[0].remarks.length' { $x.examples.example[0].remarks.length | Should -Be 5 }
It '$x.examples.example[1].introduction[0].text' { $x.examples.example[1].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[1].code' { $x.examples.example[1].code | Should -BeExactly "`$a = Get-Service`nPS> `$a | group Status" }
It '$x.examples.example[1].remarks[0].text' { $x.examples.example[1].remarks[0].text | Should -BeExactly "Explanation." }
It '$x.examples.example[1].remarks.length' { $x.examples.example[1].remarks.length | Should -Be 5 }
It '$x.examples.example[2].introduction[0].text' { $x.examples.example[2].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[2].code' { $x.examples.example[2].code | Should -BeExactly "Get-Service`nOutput" }
It '$x.examples.example[2].remarks[0].text' { $x.examples.example[2].remarks[0].text | Should -BeNullOrEmpty }
It '$x.examples.example[2].remarks.length' { $x.examples.example[2].remarks.length | Should -Be 5 }
It '$x.examples.example[3].introduction[0].text' { $x.examples.example[3].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[3].code' { $x.examples.example[3].code | Should -BeExactly "Get-Service`nOutput" }
It '$x.examples.example[3].remarks[0].text' { $x.examples.example[3].remarks[0].text | Should -BeExactly "Explanation.`nSecond line." }
It '$x.examples.example[3].remarks.length' { $x.examples.example[3].remarks.length | Should -Be 5 }
It '$x.examples.example[4].introduction[0].text' { $x.examples.example[4].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[4].code' { $x.examples.example[4].code | Should -BeExactly "Get-Service`nOutput" }
It '$x.examples.example[4].remarks[0].text' { $x.examples.example[4].remarks[0].text | Should -BeExactly "Explanation.`n`nNext section." }
It '$x.examples.example[4].remarks.length' { $x.examples.example[4].remarks.length | Should -Be 5 }
}
}