handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

[ISSUE-1168] Read whitespace in tokenstream and reconstruct source content

Open sshailabh opened this issue 4 months ago • 0 comments

  • Read whitespace to a separate channle in tokenstream.
  • Added Template.getSourceText() for exact source reconstruction with whitespace preservation. Enables formatters, linters, and IDE tooling.

Implementation:

  • Whitespace tokens sent to channel 1 (not skipped) in HbsLexer.g4
  • BaseTemplate stores tokenStream + token span (start/end indices)
  • TemplateBuilder.visitTemplate() and visitBody() set token spans during parsing
  • getSourceText() uses TokenStream.getText(Interval) for exact reconstruction
  • ForwardingTemplate delegates getSourceText() to wrapped template

Example:

handlebars.compileInline("{{ foo }}").text();          // "{{foo}}"   (normalized, Existing)
handlebars.compileInline("{{ foo }}").getSourceText(); // "{{ foo }}" (exact, New)

sshailabh avatar Oct 04 '25 03:10 sshailabh