PowerShell/src/Microsoft.PowerShell.MarkdownRender/ListItemBlockRenderer.cs

81 lines
2.7 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
2018-07-10 17:58:19 +02:00
using System.Threading;
2018-07-11 21:52:22 +02:00
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
/// <summary>
/// Renderer for adding VT100 escape sequences for items in a list block.
/// </summary>
internal class ListItemBlockRenderer : VT100ObjectRenderer<ListItemBlock>
{
protected override void Write(VT100Renderer renderer, ListItemBlock obj)
{
2018-07-12 18:00:52 +02:00
if (obj.Parent is ListBlock parent)
{
if (!parent.IsOrdered)
{
foreach (var line in obj)
{
RenderWithIndent(renderer, line, parent.BulletType, 0);
}
}
}
}
private void RenderWithIndent(VT100Renderer renderer, MarkdownObject block, char listBullet, int indentLevel)
{
// Indent left by 2 for each level on list.
2018-07-10 17:58:19 +02:00
string indent = Padding(indentLevel * 2);
2018-07-12 18:00:52 +02:00
if (block is ParagraphBlock paragraphBlock)
{
renderer.Write(indent).Write(listBullet).Write(" ").Write(paragraphBlock.Inline);
}
2018-07-11 21:52:22 +02:00
else
{
2018-07-11 21:52:22 +02:00
// If there is a sublist, the block is a ListBlock instead of ParagraphBlock.
2018-07-12 18:00:52 +02:00
if (block is ListBlock subList)
{
2018-07-11 21:52:22 +02:00
foreach (var subListItem in subList)
{
if (subListItem is ListItemBlock subListItemBlock)
{
foreach (var line in subListItemBlock)
{
// Increment indent level for sub list.
RenderWithIndent(renderer, line, listBullet, indentLevel + 1);
}
}
}
}
}
}
2018-07-10 17:58:19 +02:00
// Typical padding is at most a screen's width, any more than that and we won't bother caching.
private const int IndentCacheMax = 120;
Formatting: Add empty line between declarations (#12824) # PR Summary Automated fixes: * RCS0013: Add empty line between single-line declarations of different kind * RCS010: Add empty line between declarations ## PR Context ## PR Checklist - [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - Use the present tense and imperative mood when describing your changes - [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress). - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready. - **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)** - [x] None - **OR** - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md) - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here --> - **User-facing changes** - [x] Not Applicable - **OR** - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [ ] Issue filed: <!-- Number/link of that issue here --> - **Testing - New and feature** - [x] N/A or can only be tested interactively - **OR** - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting) - **Tooling** - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted. - **OR** - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include: - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host). - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features. - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions). - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
2020-05-29 06:56:33 +02:00
2018-07-10 17:58:19 +02:00
private static readonly string[] IndentCache = new string[IndentCacheMax];
2018-07-11 21:52:22 +02:00
2018-07-10 17:58:19 +02:00
internal static string Padding(int countOfSpaces)
{
if (countOfSpaces >= IndentCacheMax)
2018-07-11 21:52:22 +02:00
{
2018-07-10 17:58:19 +02:00
return new string(' ', countOfSpaces);
2018-07-11 21:52:22 +02:00
}
2018-07-10 17:58:19 +02:00
var result = IndentCache[countOfSpaces];
if (result == null)
2018-07-10 17:58:19 +02:00
{
Interlocked.CompareExchange(ref IndentCache[countOfSpaces], new string(' ', countOfSpaces), comparand: null);
2018-07-10 17:58:19 +02:00
result = IndentCache[countOfSpaces];
}
return result;
}
}
}