Class DOMTextFormatter

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

public class DOMTextFormatter extends Object
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 trimTrailingWhitespace is enabled. Use case: <pre> keep spaces </pre> → unchanged.
  • NormalizeSpace — consecutive whitespace is collapsed to a single space. Line breaks are preserved (unless joinContentLines). When maxLineWidth is 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 reflow mode, text soft-wraps at word boundaries when maxLineWidth is exceeded. In expand mode, 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 Details

    • DOMTextFormatter

      public DOMTextFormatter(XMLFormatterDocument formatterDocument)
      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 at maxLineWidth, 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: availableLineWidth is 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.