vysker / vscode-php-formatter

Visual Studio Code extension. A wrapper for the Sensiolabs PHP CS Fixer. Analyzes some PHP source code and tries to fix coding standards issues (PSR-1 and PSR-2 compatible).
MIT License
93 stars 11 forks source link

Key Binding erases file contents #3

Closed gilbertoalbino closed 8 years ago

gilbertoalbino commented 8 years ago

I have installed the extension and it happens that when I use the KeyBinding, it erases all the file contents.

vysker commented 8 years ago

Well that's not supposed to happen.

Could you provide any details? Such as the OS you're using, the keybinding, your settings, the context of the problem. Also, if you could turn on the phpformatter.logging, try formatting again, and then posting the console log, we might be able to shed a little light on the problem at hand.

If you could post the results here, then we might be able to figure this out together.

Thanks for bringing this to my attention.

gilbertoalbino commented 8 years ago

I running Visual Code 1.4 on Windows 10 64, with these user settings:

    "phpformatter.composer": true,
    "phpformatter.onSave": true,
    "phpformatter.level": "psr2",
    "phpformatter.logging": true

And this keybinding:

{
        "key": "ctrl+shift+l",
        "command": "phpformatter.fix",
        "when": "editorFocus"
    }

No output to console, by the way!

Seidel-Michael commented 8 years ago

Exact same Problem here. In Addition when I activate the onSave Feature nothing happens.

vysker commented 8 years ago

This issue gave me huge headache, but I finally managed to fix it.

First, I tried checking whether this had anything to do with reading the file using NodeJS's fs library. According to the documentation, fs.readFileSync reads the entire file.

So, then I immediately assumed this had something to do with character encoding. After having spent several hours on that, I came to the conclusion that it could not be character encoding. I had completely isolated the issue from that.

So then I started fiddling around with reading files. Because I could positively confirm that the contents of the *.tmp files were properly written, the issue had to be with reading them. That's when I tried reading the file without using the file descriptor. Surprisingly, the entire file was read this time. Obviously, the file descriptor stored the wrong cursor position, which was used by fs.readFileSync.

The solution was to drop the fs.readFileSync method and use a read stream instead. This accepts an option to set the cursor position manually.

And that was that! Sorry for the long wait.

Also, this writing and reading from files currently assumes UTF-8. So that is not very reliable. Unfortunately, there is no way to check the character encoding of a file in Visual Studio Code programmatically, yet. I added a feature request to the Visual Studio Code GitHub just for that.

In the near future, I will have to implement a workaround for character encoding detection using a third-party package. For now, it will all be UTF-8.