Skip to content

Model XML documentation comments as structured data #86

Description

@matt-edmondson

Problem

In the template model being ported in #84, documentation is a bag of raw strings:

public List<string> Comments { get; set; } = [];

Callers therefore hand-assemble XML doc comments line by line, including the /// prefix and the tag markup. ktsu.Semantics even keeps named constants for the delimiters to avoid typos:

internal const string SummaryOpen = "/// <summary>";
internal const string SummaryClose = "/// </summary>";

Three consequences:

  1. Nothing escapes the content. A description containing <, > or & — entirely plausible for a type described in metadata, e.g. "values in the range <0, 1>" — emits malformed XML and trips the compiler's doc-comment warnings.
  2. Nothing enforces structure. A <param> tag whose name does not match any parameter produces CS1572/CS1573 in the generated file, and there is no way for the model to catch it even though it knows the parameter list.
  3. Every generator re-derives the same layout — which tags in which order, how to wrap long lines, how to indent a multi-line <remarks>.

Proposal

Add a DocComment type carrying the standard tags, and render it from the template:

public sealed class DocComment
{
    public string? Summary { get; set; }
    public string? Remarks { get; set; }
    public string? Returns { get; set; }
    public string? Value { get; set; }
    public List<(string Name, string Text)> Params { get; }
    public List<(string Name, string Text)> TypeParams { get; }
    public List<(string Cref, string Text)> Exceptions { get; }
    public List<string> SeeAlso { get; }
    public string? InheritDoc { get; set; }   // renders /// <inheritdoc/>
}

with:

  • XML-escaping of all text content by default, and an explicit opt-out for callers who are deliberately embedding markup such as <c> or <see cref="..."/>;
  • canonical tag order (summary, typeparam, param, returns, value, exception, remarks, seealso);
  • correct /// prefixing and indentation for multi-line text, honouring the configured indent string;
  • optional validation against the owning template's parameter and type-parameter lists, surfaced as an exception or a diagnostic rather than as a compile error in the generated output.

Keep Comments as the escape hatch for non-doc comments and anything the model does not cover.

Acceptance criteria

  • DocComment renders each supported tag in canonical order with correct prefixing and indentation.
  • Text content is XML-escaped by default; the opt-out is tested.
  • <param> / <typeparam> names can be validated against a MethodTemplate / ConstructorTemplate parameter list.
  • Multi-line <remarks> and <summary> render each line with its own /// prefix.
  • README.md documents the type with an example.

Context

Part of ktsu-dev/Semantics#181. Depends on #84.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions