From f98b59a5f023e626766dc0736f206637066699bc Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 10 Apr 2023 02:18:02 +0200 Subject: [PATCH] fix: print a newline after a doc comment, these are trimmed when parsing and cause the output to look bad --- pkg/visitor/printer/printer.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/visitor/printer/printer.go b/pkg/visitor/printer/printer.go index 31f3999..7a04bf8 100644 --- a/pkg/visitor/printer/printer.go +++ b/pkg/visitor/printer/printer.go @@ -91,6 +91,21 @@ func (p *printer) printToken(t *token.Token, def []byte) { for _, ff := range t.FreeFloating { p.write(ff.Value) + + // I believe doc comments have their newlines trimmed during parsing, + // Causing the output to look weird (like: "*/function" for example). + // Lets add a newline for now, as most doc comments are followed by a + // newline anyways. + if ff.ID == token.T_DOC_COMMENT { + p.write([]byte("\n")) + + // Add indentation based on the indentation of the comment. + if len(ff.Value) > 4 { + for i := len(ff.Value)-4; ff.Value[i] == ' '; i-- { + p.write([]byte(" ")) + } + } + } } p.write(t.Value) }