Class RNCFormatterDocument
java.lang.Object
org.eclipse.lemminx.extensions.relaxng.grammar.rnc.format.RNCFormatterDocument
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:
- Scan all tokens using
RNCScannerand collect their types and offsets. - Iterate over each token and decide what whitespace should appear before/after it based on the token type and current brace depth.
- Compare the expected whitespace with the actual whitespace in the
document. If they differ, create a
TextEditto 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 Summary
ConstructorsConstructorDescriptionRNCFormatterDocument(TextDocument textDocument, SharedSettings sharedSettings, RNCFormattingSettings rncFormattingSettings) Creates a new formatter for the given RNC document. -
Method Summary
-
Constructor Details
-
Method Details
-
format
Formats the document and returns a list ofTextEdits.The method performs three passes:
- Token pass — scans all tokens and adjusts whitespace around operators, braces, keywords, and definitions.
- Trailing whitespace pass — removes trailing spaces on each
line (if
trimTrailingWhitespaceis enabled). - Final newline pass — ensures a final newline exists (if
insertFinalNewlineis enabled) and trims excess final newlines (iftrimFinalNewlinesis enabled).
- Returns:
- a list of text edits. Empty if the document is already properly formatted.
-