Class DOMCommentFormatter

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

public class DOMCommentFormatter extends Object
Formats XML comment nodes (<!-- ... -->).

Comment formatting handles two main concerns:

  • Indentation — aligns the comment with its surrounding elements when it appears on its own line. Use case:
     <root>
              <!-- misindented -->
     </root>
     →
     <root>
       <!-- misindented -->
     </root>
     
  • Line wrapping — when maxLineWidth is enabled, comment content that exceeds the limit wraps at word boundaries. Use case (maxLineWidth=40):
     <!-- This is a very long comment that exceeds the max line width -->
     →
     <!-- This is a very long comment
     that exceeds the max line width -->
     

The joinCommentLines setting controls whether existing line breaks inside comments are collapsed. When true, multi-line comments are joined into a single line (then re-wrapped by maxLineWidth if needed). When false, existing line breaks are preserved.

  • Constructor Details

    • DOMCommentFormatter

      public DOMCommentFormatter(XMLFormatterDocument formatterDocument)
      Creates a new comment formatter.
      Parameters:
      formatterDocument - the parent formatter document (provides settings and edit helpers).
  • Method Details

    • formatComment

      public void formatComment(DOMComment commentNode, XMLFormattingConstraints parentConstraints, int startRange, int endRange, List<org.eclipse.lsp4j.TextEdit> edits)
      Formats a comment node: adjusts indentation and wraps content at maxLineWidth word boundaries.

      Use case (indentation):

       <root>
                    <!-- comment -->
       </root>
       →
       <root>
         <!-- comment -->
       </root>
       

      Use case (line wrapping with maxLineWidth):

       <!-- word1 word2 word3 word4 word5 -->
       →
       <!-- word1 word2
       word3 word4 word5 -->
       
      Parameters:
      commentNode - the comment node to format.
      parentConstraints - the parent's formatting constraints (availableLineWidth is updated).
      startRange - the start offset of the formatting range.
      endRange - the end offset of the formatting range.
      edits - the list of text edits to populate.