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

public class RNCBuilder extends Object
Builder for generating formatted RNC (RELAX NG Compact Syntax) content. Analogous to XMLBuilder for XML content generation.

Wraps a StringBuilder and provides fluent methods for appending RNC constructs (namespaces, defines, elements, attributes, operators, keywords, comments) with correct formatting. All methods return this to enable method chaining.

Use case: used by code actions and completions to generate properly formatted RNC fragments that respect the user's indentation preferences (tab vs spaces, line delimiter). Also supports LSP snippet placeholders ($1, ${2:default}) for interactive completions.

Example — generating a complete RNC schema:

 RNCBuilder builder = new RNCBuilder("  ", "\n", false);
 builder.addDefaultNamespace("http://example.com")
        .blankLine()
        .addStart("root")
        .blankLine()
        .startDefine("root")
        .startElement("root", 0)
        .linefeed().indent(1)
        .appendText()
        .linefeed()
        .closeBrace();
 // Produces:
 // default namespace = "http://example.com"
 //
 // start = root
 //
 // root = element root {
 //   text
 // }
 

Example — generating a snippet with placeholders for a completion item:

 RNCBuilder builder = new RNCBuilder("  ", "\n", true);
 builder.append("element ")
        .addSnippetPlaceholder("name")  // ${1:name}
        .append(" {")
        .linefeed().indent(1)
        .addFinalTabstop()              // $0
        .linefeed()
        .closeBrace();
 // Produces: element ${1:name} {\n  $0\n}
 
See Also:
  • invalid reference
    org.eclipse.lemminx.dom.builder.XMLBuilder
  • Constructor Details

    • RNCBuilder

      public RNCBuilder(String whitespacesIndent, String lineDelimiter, boolean canSupportSnippets)
      Creates a new RNC builder.
      Parameters:
      whitespacesIndent - the string for one indent level (e.g. " " for 2-space indent, "\t" for tab indent).
      lineDelimiter - the line delimiter to use (e.g. "\n", "\r\n").
      canSupportSnippets - true if the client supports LSP snippet syntax in completions and code actions.
  • Method Details

    • addNamespace

      public RNCBuilder addNamespace(String prefix, String uri)
      Appends a namespace declaration: namespace prefix = "uri".

      Use case: generating a namespace declaration in a "New RNC" file template or when a code action needs to add a namespace.

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

      Parameters:
      prefix - the namespace prefix (e.g. "a", "html").
      uri - the namespace URI.
      Returns:
      this builder for chaining.
    • addDefaultNamespace

      public RNCBuilder addDefaultNamespace(String uri)
      Appends a default namespace declaration: default namespace = "uri".

      Example output: default namespace = "http://example.com"

      Parameters:
      uri - the default namespace URI.
      Returns:
      this builder for chaining.
    • addDatatypes

      public RNCBuilder addDatatypes(String prefix, String uri)
      Appends a datatypes declaration: datatypes prefix = "uri".

      Use case: adding an XSD datatypes declaration: datatypes xsd = "http://www.w3.org/2001/XMLSchema-datatypes"

      Parameters:
      prefix - the datatypes prefix (e.g. "xsd").
      uri - the datatypes library URI.
      Returns:
      this builder for chaining.
    • addStart

      public RNCBuilder addStart(String pattern)
      Appends a start pattern: start = pattern.

      Example output: start = root

      Parameters:
      pattern - the start pattern name.
      Returns:
      this builder for chaining.
    • startDefine

      public RNCBuilder startDefine(String name)
      Begins a define statement: name = .

      Use case: start a define, then append the pattern body. Example: builder.startDefine("root").startElement("root", 0)... produces root = element root {...}.

      Parameters:
      name - the define name.
      Returns:
      this builder for chaining.
    • startDefineChoice

      public RNCBuilder startDefineChoice(String name)
      Begins a choice-combine define: name |= .

      Use case: extending a pattern with additional choices: content |= newElement.

      Parameters:
      name - the define name.
      Returns:
      this builder for chaining.
    • startDefineInterleave

      public RNCBuilder startDefineInterleave(String name)
      Begins an interleave-combine define: name &= .

      Use case: extending a pattern with interleaved content: content &= optionalElement.

      Parameters:
      name - the define name.
      Returns:
      this builder for chaining.
    • startElement

      public RNCBuilder startElement(String name, int indentLevel)
      Begins an element pattern: element name {.

      Use case: generating element patterns in completions or code actions. The caller is responsible for adding content and closing with closeBrace().

      Example: builder.startElement("root", 0) produces element root {.

      Parameters:
      name - the element name.
      indentLevel - the current indent level (reserved for future use).
      Returns:
      this builder for chaining.
    • startAttribute

      public RNCBuilder startAttribute(String name)
      Begins an attribute pattern: attribute name { .

      Attributes use inline formatting with spaces inside braces: attribute id { text }.

      Parameters:
      name - the attribute name.
      Returns:
      this builder for chaining.
    • endAttribute

      public RNCBuilder endAttribute()
      Closes an attribute pattern: } (with leading space).
      Returns:
      this builder for chaining.
    • openBrace

      public RNCBuilder openBrace()
      Appends an opening brace: {.
      Returns:
      this builder for chaining.
    • closeBrace

      public RNCBuilder closeBrace()
      Appends a closing brace: }.
      Returns:
      this builder for chaining.
    • appendChoice

      public RNCBuilder appendChoice()
      Appends the choice operator with surrounding spaces: | .

      Use case: combining patterns with choice: a | b | c.

      Returns:
      this builder for chaining.
    • appendInterleave

      public RNCBuilder appendInterleave()
      Appends the interleave operator with surrounding spaces: & .

      Use case: combining patterns with interleave: a & b.

      Returns:
      this builder for chaining.
    • appendGroup

      public RNCBuilder appendGroup()
      Appends the group (sequence) operator: , (comma + space).

      Use case: sequencing patterns: a, b, c.

      Returns:
      this builder for chaining.
    • appendOptional

      public RNCBuilder appendOptional()
      Appends the optional postfix operator: ?.

      Use case: marking a pattern as optional: name?.

      Returns:
      this builder for chaining.
    • appendZeroOrMore

      public RNCBuilder appendZeroOrMore()
      Appends the zero-or-more postfix operator: *.

      Use case: marking a pattern as repeatable: item*.

      Returns:
      this builder for chaining.
    • appendOneOrMore

      public RNCBuilder appendOneOrMore()
      Appends the one-or-more postfix operator: +.

      Use case: marking a pattern as requiring at least one: name+.

      Returns:
      this builder for chaining.
    • appendText

      public RNCBuilder appendText()
      Appends the text keyword (matches any text content).

      Example: inside an element — element name { text }.

      Returns:
      this builder for chaining.
    • appendEmpty

      public RNCBuilder appendEmpty()
      Appends the empty keyword (matches empty content).

      Example: element br { empty }.

      Returns:
      this builder for chaining.
    • appendInclude

      public RNCBuilder appendInclude(String file)
      Appends an include declaration: include "file".

      Use case: including another RNC grammar: include "common.rnc".

      Parameters:
      file - the file path to include.
      Returns:
      this builder for chaining.
    • addSnippetTabstop

      public RNCBuilder addSnippetTabstop()
      Appends a snippet tabstop ($N) at the current position, auto-incrementing the index. If snippets are not supported, appends nothing.

      Use case: placing cursor positions in a completion snippet: element $1 { $2 }.

      Returns:
      this builder for chaining.
    • addSnippetPlaceholder

      public RNCBuilder addSnippetPlaceholder(String defaultValue)
      Appends a snippet placeholder (${N:defaultValue}) at the current position. If snippets are not supported, appends the default value as plain text.

      Use case: a completion for element with a placeholder for the name: element ${1:name} { $0 }.

      Parameters:
      defaultValue - the default text shown in the placeholder.
      Returns:
      this builder for chaining.
    • addFinalTabstop

      public RNCBuilder addFinalTabstop()
      Appends the final tabstop ($0), which marks where the cursor rests after all other tabstops are visited. If snippets are not supported, appends nothing.
      Returns:
      this builder for chaining.
    • linefeed

      public RNCBuilder linefeed()
      Appends a line feed (line delimiter).
      Returns:
      this builder for chaining.
    • indent

      public RNCBuilder indent(int level)
      Appends indentation for the given nesting level.

      Use case: indenting content inside a block: builder.linefeed().indent(1).appendText() produces \n text (with 2-space indent).

      Parameters:
      level - the number of indent levels to append.
      Returns:
      this builder for chaining.
    • space

      public RNCBuilder space()
      Appends a single space character.
      Returns:
      this builder for chaining.
    • blankLine

      public RNCBuilder blankLine()
      Appends a blank line (two consecutive line delimiters).

      Use case: separating top-level definitions with a visual gap.

      Returns:
      this builder for chaining.
    • append

      public RNCBuilder append(String text)
      Appends arbitrary text to the buffer.
      Parameters:
      text - the text to append.
      Returns:
      this builder for chaining.
    • append

      public RNCBuilder append(char c)
      Appends a single character to the buffer.
      Parameters:
      c - the character to append.
      Returns:
      this builder for chaining.
    • addComment

      public RNCBuilder addComment(String text)
      Appends a single-line comment: # text.

      Example output: # This is a comment

      Parameters:
      text - the comment text (without the # prefix).
      Returns:
      this builder for chaining.
    • addDocComment

      public RNCBuilder addDocComment(String text)
      Appends a documentation comment: ## text.

      Documentation comments in RNC are attached to the following definition and can be used to generate documentation.

      Example output: ## The root element of the document

      Parameters:
      text - the documentation comment text (without the ## prefix).
      Returns:
      this builder for chaining.
    • toString

      public String toString()
      Returns the accumulated RNC content as a string.
      Overrides:
      toString in class Object
      Returns:
      the built RNC content.
    • length

      public int length()
      Returns the current length of the accumulated content.
      Returns:
      the number of characters in the buffer.