From 8608ab6e75f58fa27bc1c9953a9f79761e395742 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 17 Nov 2024 19:37:08 +0900 Subject: [PATCH] fix --- scanner/context.go | 5 ++++- scanner/scanner.go | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/scanner/context.go b/scanner/context.go index 908a384b..6898d082 100644 --- a/scanner/context.go +++ b/scanner/context.go @@ -151,6 +151,7 @@ func (c *Context) addDocumentIndent(column int) { } // Since addBuf ignore space character, add to the buffer directly. c.buf = append(c.buf, ' ') + c.notSpaceCharPos = len(c.buf) } } @@ -295,7 +296,9 @@ func (c *Context) bufferedSrc() []rune { } // If the text ends with a space character, remove all of them. - src = []rune(strings.TrimRight(string(src), " ")) + if c.hasTrimAllEndNewlineOpt() { + src = []rune(strings.TrimRight(string(src), " ")) + } if string(src) == "\n" { // If the content consists only of a newline, // it can be considered as the document ending without any specified value, diff --git a/scanner/scanner.go b/scanner/scanner.go index 5b149db7..a0e74bf9 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -639,13 +639,17 @@ func (s *Scanner) trimCommentFromDocumentOpt(text string, header rune) (string, func (s *Scanner) scanDocument(ctx *Context, c rune) error { ctx.addOriginBuf(c) if ctx.isEOS() { + if s.isFirstCharAtLine && c == ' ' { + ctx.addDocumentIndent(s.column) + } else { + ctx.addBuf(c) + } ctx.updateDocumentLineIndentColumn(s.column) if err := ctx.validateDocumentLineIndentColumn(); err != nil { invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos()) s.progressColumn(ctx, 1) return ErrInvalidToken(invalidTk) } - ctx.addBuf(c) value := ctx.bufferedSrc() ctx.addToken(token.String(string(value), string(ctx.obuf), s.pos())) ctx.clear()