Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Format XML documentation comments #974

Open
FelixZY opened this issue Oct 16, 2023 · 1 comment
Open

Feature request: Format XML documentation comments #974

FelixZY opened this issue Oct 16, 2023 · 1 comment
Labels
type:enhancement New feature or request
Milestone

Comments

@FelixZY
Copy link

FelixZY commented Oct 16, 2023

This is related to #667 but, I think, more narrow in scope.

We use xml documentation comments in code but have not been able to find a way to standardize formatting. Since we are already using csharpier to format our c# files, it would be really helpful if xml documentation comments were supported as well.

@FelixZY
Copy link
Author

FelixZY commented Oct 16, 2023

This is not a fix for the original problem but a manual workaround if you are simply looking to format the documentation you are currently writing.

Dirty workaround using prettier manually

1. Install prettier

$ npm install prettier

2. Copy documentation to a new .html file

Also remove leading ///

`MyClass.cs`
/// <summary>Some summary</summary>
/// <remarks>
///     <para>
///         This documentation is really long and requires that the formatter produces multiple lines when
///         formatted with
///         the <c>printWidth</c>
///         we are going with.
///     </para>
///     <code>
///         Console.WriteLine(
///             """
///             Hello World!
///             """
///         );
///     </code>
///     <para>
///         Another
///         badly <see cref="Formatting"/> formatted
///         para.
///     </para>
/// </remarks>
public class MyClass
{ }

documentation.html (input)

<summary>Some summary</summary>
<remarks>
    <para>
        This documentation is really long and requires that the formatter produces multiple lines when
        formatted with
        the <c>printWidth</c>
        we are going with.
    </para>
    <code>
        Console.WriteLine(
            """
            Hello World!
            """
        );
    </code>
    <para>
        Another
        badly <see cref="Formatting"/> formatted
        para.
    </para>
</remarks>

4. Add <!-- prettier-ignore --> before any <code> blocks etc. that should not be formatted

This will only affect the tag immediately following.

<summary>Some summary</summary>
<remarks>
    <para>
        This documentation is really long and requires that the formatter produces multiple lines when
        formatted with
        the <c>printWidth</c>
        we are going with.
    </para>
+   <!-- prettier-ignore -->
    <code>
        Console.WriteLine(
            """
            Hello World!
            """
        );
    </code>
    <para>
        Another
        badly <see cref="Formatting"/> formatted
        para.
    </para>
</remarks>

4. Run prettier

$ prettier --tab-width 4 --write documentation.html

documentation.html (output):

<summary>Some summary</summary>
<remarks>
    <para>
        This documentation is really long and requires that the formatter
        produces multiple lines when formatted with the <c>printWidth</c>
        we are going with.
    </para>
    <!-- prettier-ignore -->
    <code>
        Console.WriteLine(
            """
            Hello World!
            """
        );
    </code>
    <para> Another badly <see cref="Formatting" /> formatted para. </para>
</remarks>

5. Reinsert ///, remove <!-- prettier-ignore --> and copy back to your .cs file

You can use search and replace by regex ^ -> /// (note the trailing space in the replace expression!) to reinsert /// easily.

@belav belav added the type:enhancement New feature or request label Oct 20, 2023
@belav belav added this to the 0.27.0 milestone Oct 20, 2023
@belav belav modified the milestones: 0.27.0, Planned Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants