Class DOMAttributeFormatter
java.lang.Object
org.eclipse.lemminx.services.format.DOMAttributeFormatter
Formats DOM element attributes: spacing, indentation, quoting, and line
splitting.
Attribute formatting has three main phases for each attribute:
- Left spacing / indentation — controls whitespace before the
attribute name. Depends on the
XMLFormattingOptions.SplitAttributessetting:preserve— keeps existing layout; wraps to new line only whenmaxLineWidthis 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="">
- Delimiter formatting — removes extra spaces around
=. Use case:<foo attr = "val">→<foo attr="val">. - Quote enforcement — replaces quotes with the preferred style.
Use case (preferred = single):
<a name="value">→<a name='value'>.
- Author:
- Angelo ZERR
-
Constructor Summary
ConstructorsConstructorDescriptionDOMAttributeFormatter(XMLFormatterDocument formatterDocument) Creates a new attribute formatter. -
Method Summary
Modifier and TypeMethodDescriptionvoidformatAttribute(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.
-
Constructor Details
-
DOMAttributeFormatter
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">→<fooattr1="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-trueif this is the only attribute on the element (splitNewLine does not split single attributes).useSettings-trueto apply split/preserve settings;falseto only normalize spacing.isFirstAttr-trueif 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.
-