Class RNCFormatterDocument

java.lang.Object
org.eclipse.lemminx.extensions.relaxng.grammar.rnc.format.RNCFormatterDocument

public class RNCFormatterDocument extends Object
Formats an RNC (RELAX NG Compact Syntax) document by producing a list of TextEdits that transform whitespace (indentation, line breaks, spacing around operators) without altering semantic content.

Approach

Unlike XML formatting (which walks a DOM tree), RNC formatting is scanner-based because RNC has no DOM structure. The algorithm:

  1. Scan all tokens using RNCScanner and collect their types and offsets.
  2. Iterate over each token and decide what whitespace should appear before/after it based on the token type and current brace depth.
  3. Compare the expected whitespace with the actual whitespace in the document. If they differ, create a TextEdit to replace the actual whitespace with the expected one.

Formatting Rules

  • { (BraceOpen) — space before, content on new indented line (unless the block is empty { }).
  • } (BraceClose) — on its own line at the parent indent level.
  • =, |=, &= (Assign operators) — space before and space after.
  • | (Pipe), & (Ampersand) — space before and space after (choice/interleave operators).
  • , (Comma) — no space before, space after (group operator).
  • ?, *, + (Postfix operators) — no space before the operator.
  • Top-level definitions — blank line between consecutive definitions at brace depth 0.
  • Keywords followed by name (element, attribute, namespace, etc.) — space after the keyword.

Use case: the user requests formatting of an .rnc file via the LSP textDocument/formatting request.

Example — unformatted input:

 namespace a="http://example.com"
 start=element root{attribute id{text},name+}
 name=element name{text}
 

Formatted output:

 namespace a = "http://example.com"

 start = element root {
   attribute id { text }, name+
 }

 name = element name {
   text
 }
 
See Also:
  • Constructor Details

    • RNCFormatterDocument

      public RNCFormatterDocument(TextDocument textDocument, SharedSettings sharedSettings, RNCFormattingSettings rncFormattingSettings)
      Creates a new formatter for the given RNC document.
      Parameters:
      textDocument - the RNC document to format.
      sharedSettings - the user's formatting preferences.
      rncFormattingSettings - the RNC-specific formatting settings, or null to use defaults.
  • Method Details

    • format

      public List<? extends org.eclipse.lsp4j.TextEdit> format()
      Formats the document and returns a list of TextEdits.

      The method performs three passes:

      1. Token pass — scans all tokens and adjusts whitespace around operators, braces, keywords, and definitions.
      2. Trailing whitespace pass — removes trailing spaces on each line (if trimTrailingWhitespace is enabled).
      3. Final newline pass — ensures a final newline exists (if insertFinalNewline is enabled) and trims excess final newlines (if trimFinalNewlines is enabled).
      Returns:
      a list of text edits. Empty if the document is already properly formatted.