Class DOMElementFormatter
java.lang.Object
org.eclipse.lemminx.services.format.DOMElementFormatter
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 Summary
ConstructorsConstructorDescriptionDOMElementFormatter(XMLFormatterDocument formatterDocument, DOMAttributeFormatter attributeFormatter) Creates a new element formatter. -
Method Summary
Modifier and TypeMethodDescriptionvoidformatElement(DOMElement element, XMLFormattingConstraints parentConstraints, int start, int end, List<org.eclipse.lsp4j.TextEdit> edits) Formats a DOM element: start tag, children, and end tag.
-
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:
- Start tag — indentation, attributes, and closing bracket
(
>or/>). UpdatesparentConstraints.availableLineWidth. - Children — formatted recursively with a copy of the parent constraints (indent level incremented by 1).
- End tag — indentation of
</tagName>. UpdatesparentConstraints.availableLineWidthfrom 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.
- Start tag — indentation, attributes, and closing bracket
(
-