Class DOMAttributeFormatter

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

public class DOMAttributeFormatter extends Object
Formats DOM element attributes: spacing, indentation, quoting, and line splitting.

Attribute formatting has three main phases for each attribute:

  1. Left spacing / indentation — controls whitespace before the attribute name. Depends on the XMLFormattingOptions.SplitAttributes setting:
    • preserve — keeps existing layout; wraps to new line only when maxLineWidth is exceeded. Use case: <foo attr1="" attr2=""> stays on one line if it fits.
    • splitNewLine — each attribute on its own line (when >1 attribute). Use case: <foo attr1="" attr2=""> →
       <foo
           attr1=""
           attr2="">
       
    • alignWithFirstAttr — attributes aligned with the first attribute. Use case:
       <foo attr1=""
            attr2="">
       
  2. Delimiter formatting — removes extra spaces around =. Use case: <foo attr = "val"> → <foo attr="val">.
  3. Quote enforcement — replaces quotes with the preferred style. Use case (preferred = single): <a name="value"> → <a name='value'>.
Author:
Angelo ZERR
  • Constructor Details

    • DOMAttributeFormatter

      public DOMAttributeFormatter(XMLFormatterDocument formatterDocument)
      Creates a new attribute formatter.
      Parameters:
      formatterDocument - the parent formatter document.
  • Method Details

    • formatAttribute

      public void formatAttribute(DOMAttr attr, int prevOffset, boolean singleAttribute, boolean useSettings, boolean isFirstAttr, XMLFormattingConstraints parentConstraints, List<org.eclipse.lsp4j.TextEdit> edits)
      Formats a single attribute: left spacing, delimiter (=), value, and quote style.

      Use case (normalize spacing):

      <foo  attr1 = "val"  attr2="val"> → <foo attr1="val" attr2="val">

      Use case (splitNewLine with 2+ attributes):

       <foo attr1="val" attr2="val">
       →
       <foo
           attr1="val"
           attr2="val">
       
      Parameters:
      attr - the attribute to format.
      prevOffset - the end offset of the previous token (tag name or previous attribute) — used as left boundary for whitespace replacement.
      singleAttribute - true if this is the only attribute on the element (splitNewLine does not split single attributes).
      useSettings - true to apply split/preserve settings; false to only normalize spacing.
      isFirstAttr - true if this is the first attribute (alignWithFirstAttr skips the first).
      parentConstraints - the parent's formatting constraints (modified: availableLineWidth is updated).
      edits - the list of text edits to populate.