Class DOMTextFormatter
java.lang.Object
org.eclipse.lemminx.services.format.DOMTextFormatter
Formats text nodes within XML elements according to their parent's
FormatElementCategory.
Text formatting behavior varies by category:
- PreserveSpace — text is kept as-is; only trailing whitespace
on empty lines is trimmed when
trimTrailingWhitespaceis enabled. Use case:<pre> keep spaces </pre>→ unchanged. - NormalizeSpace — consecutive whitespace is collapsed to a
single space. Line breaks are preserved (unless
joinContentLines). WhenmaxLineWidthis exceeded, text wraps to a new line. Use case:<p> hello world </p>→<p>hello world</p>. - MixedContent — inline whitespace is collapsed to one space,
existing newlines are preserved. In
reflowmode, text soft-wraps at word boundaries whenmaxLineWidthis exceeded. Inexpandmode, each text node goes on its own indented line. Use case:<p>text <b>bold</b> more</p>→<p>text <b>bold</b> more</p>. - IgnoreSpace — text nodes (typically whitespace-only between elements) are handled by element indentation, not here.
The formatter tracks availableLineWidth through each text token.
When the remaining width goes negative, the next whitespace gap is
replaced with a newline + indentation (text wrapping).
- Author:
- Angelo ZERR
-
Constructor Summary
ConstructorsConstructorDescriptionDOMTextFormatter(XMLFormatterDocument formatterDocument) Creates a new text formatter. -
Method Summary
Modifier and TypeMethodDescriptionvoidformatText(DOMText textNode, XMLFormattingConstraints parentConstraints, int start, int end, List<org.eclipse.lsp4j.TextEdit> edits) Formats a text node: normalizes whitespace, wraps atmaxLineWidth, and trims trailing spaces.
-
Constructor Details
-
DOMTextFormatter
Creates a new text formatter.- Parameters:
formatterDocument- the parent formatter document (provides settings and edit helpers).
-
-
Method Details
-
formatText
public void formatText(DOMText textNode, XMLFormattingConstraints parentConstraints, int start, int end, List<org.eclipse.lsp4j.TextEdit> edits) Formats a text node: normalizes whitespace, wraps atmaxLineWidth, and trims trailing spaces.Use case (NormalizeSpace):
<p> hello world </p> → <p>hello world</p>
Use case (maxLineWidth wrapping):
<p>aaa bbb ccc ddd</p> (maxLineWidth=10) → <p>aaa bbb ccc ddd</p>
- Parameters:
textNode- the text node to format.parentConstraints- the parent's formatting constraints (modified:availableLineWidthis updated).start- start offset of the formatting range.end- end offset of the formatting range (-1 for no limit).edits- the list of text edits to populate.
-