From 44bb74f8fabf281ca7a3ffcd92625e2969ccec8a Mon Sep 17 00:00:00 2001 From: Junya Akahira Date: Thu, 8 Jun 2023 16:32:34 +0900 Subject: [PATCH] Fix unexpected format when files do not end with newline --- editor/filter_vertical_formatter.go | 3 +++ editor/filter_vertical_formatter_test.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/editor/filter_vertical_formatter.go b/editor/filter_vertical_formatter.go index 7864d2d..2745072 100644 --- a/editor/filter_vertical_formatter.go +++ b/editor/filter_vertical_formatter.go @@ -62,6 +62,9 @@ func trimTrailingDuplicatedNewLine(tokens hclwrite.Tokens) hclwrite.Tokens { if tokens[end-1].Type == hclsyntax.TokenEOF { // skip EOF eof = tokens[end-1] + if tokens[end-2].Type != hclsyntax.TokenNewline { + break + } continue } if tokens[end-1].Type == hclsyntax.TokenNewline && diff --git a/editor/filter_vertical_formatter_test.go b/editor/filter_vertical_formatter_test.go index 7e69198..e5d7651 100644 --- a/editor/filter_vertical_formatter_test.go +++ b/editor/filter_vertical_formatter_test.go @@ -44,6 +44,12 @@ b1 { b2 l1 {} `, }, + { + name: "non-POSIX style end of file", + src: `a0 = v0`, + ok: true, + want: `a0 = v0`, + }, } for _, tc := range cases {