Class DOMElementFormatter

java.lang.Object
org.eclipse.lemminx.services.format.DOMElementFormatter

public class DOMElementFormatter extends Object
Formats DOM elements according to their FormatElementCategory.

Each element is classified into one of four categories that determine how its start tag, children, and end tag are formatted:

  • IgnoreSpace — element contains only other elements (no text). Children are indented on new lines. Use case: <root><child/></root> → each child on its own line.
  • NormalizeSpace — element contains only text (no child elements). Whitespace within text is normalized. Use case: <p> hello world </p> → <p>hello world</p>.
  • MixedContent — element contains both text and child elements. Inline flow is preserved; elements only wrap to new lines when maxLineWidth is exceeded or indentation needs normalizing. Use case: <p>text <b>bold</b> more</p>.
  • PreserveSpace — element has xml:space="preserve". All whitespace is kept as-is. Use case: <pre> keep spaces </pre>.

The formatter tracks availableLineWidth to enforce maxLineWidth. Each formatted token decrements the remaining width; when it goes negative, subsequent elements are moved to new lines.

Author:
Angelo ZERR
  • Constructor Details

    • DOMElementFormatter

      public DOMElementFormatter(XMLFormatterDocument formatterDocument, DOMAttributeFormatter attributeFormatter)
      Creates a new element formatter.
      Parameters:
      formatterDocument - the parent formatter document (provides settings and edit helpers).
      attributeFormatter - the formatter for element attributes.
  • Method Details

    • formatElement

      public void formatElement(DOMElement element, XMLFormattingConstraints parentConstraints, int start, int end, List<org.eclipse.lsp4j.TextEdit> edits)
      Formats a DOM element: start tag, children, and end tag.

      Formatting proceeds in three phases:

      1. Start tag — indentation, attributes, and closing bracket (> or />). Updates parentConstraints.availableLineWidth.
      2. Children — formatted recursively with a copy of the parent constraints (indent level incremented by 1).
      3. End tag — indentation of </tagName>. Updates parentConstraints.availableLineWidth from the child constraints.

      Use case (IgnoreSpace):

       <root><child/></root>
       →
       <root>
         <child />
       </root>
       
      Parameters:
      element - the DOM element to format.
      parentConstraints - the formatting constraints from the parent element. Modified in place: availableLineWidth is updated.
      start - the start offset of the formatting range (-1 for no limit).
      end - the end offset of the formatting range (-1 for no limit).
      edits - the list of text edits to populate.