vknabel / vscode-swiftformat

SwiftFormat for VS Code
https://marketplace.visualstudio.com/items?itemName=vknabel.vscode-swiftformat
MIT License
39 stars 7 forks source link

End of file newline is removed #13

Closed fappelman closed 4 years ago

fappelman commented 4 years ago

I have a simple example file demo.swift:

import Foundation

struct Demo {
    init() {
        // Bla
    }
}

It is important to notice that the file does end with a newline:

$ od -c demo.swift
0000000    i   m   p   o   r   t       F   o   u   n   d   a   t   i   o
0000020    n  \n  \n   s   t   r   u   c   t       D   e   m   o       {
0000040   \n                   i   n   i   t   (   )       {  \n
0000060                            /   /       B   l   a  \n
0000100        }  \n   }  \n
0000105

When I run swiftformat using the command line:

$ swiftformat --verbose --config ~/.config/swiftformat/config demo.swift
Running SwiftFormat...
Formatting demo.swift
-- no changes (cached)

SwiftFormat completed in 0.01s.
0/1 files formatted.

As you can see swiftformat is fine with this demo struct.

The next step is then to run this within VSCode. I have configured the exact same config file and would expect the same output. It is not, it removes the last line.

When I save it I get the following content with od:

$ od -c demo.swift
0000000    i   m   p   o   r   t       F   o   u   n   d   a   t   i   o
0000020    n  \n  \n   s   t   r   u   c   t       D   e   m   o       {
0000040   \n                   i   n   i   t   (   )       {  \n
0000060                            /   /       B   l   a  \n
0000100        }  \n   }
0000104

That new line should not be removed.

fappelman commented 4 years ago

As a workaround I have enabled Insert Final Newline which restores the missing newline but that should not be the solution.

vknabel commented 4 years ago

@fappelman Good catch! Thanks for the great report. I will have a deeper look soon!

vknabel commented 4 years ago

Thanks again. Fix released as 1.3.4

Also I release a fix for vknabel/vscode-apple-swift-format@1.0.1, which had the same bug.

fappelman commented 4 years ago

I did notice that the other package had the same issue. Thanks for fixing.