Delgan / qml-format

A VS Code extension to beautify QML files
https://marketplace.visualstudio.com/items?itemName=delgan.qml-format
MIT License
9 stars 0 forks source link

Generates double newlines when using NewlineType=windows #2

Closed SnowMB closed 2 years ago

SnowMB commented 2 years ago

I try to use this extension on Windows with qmlformat.exe from qt 6.3.1.

When I manually call the formatter on the command line i get the expected result:

C:\Qt\6.3.1\msvc2019_64\bin\qmlformat.exe main.qml

.qmlformat.ini

[General]
IndentWidth=4
NormalizeOrder=true
UseTabs=
NewlineType=windows
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    color: "#303030"
    height: 480
    title: qsTr("Window")
    visible: true
    width: 640

    Rectangle {
        id: content
        anchors.full: parent
        color: "blue"
    }
}

However when using the extension in vscode I get a different result:

import QtQuick 2.15

import QtQuick.Window 2.15

import QtQuick.Controls 2.15

Window {

    color: "#303030"

    height: 480

    title: qsTr("Window")

    visible: true

    width: 640

    Rectangle {

        id: content

        anchors.full: parent

        color: "blue"

    }

}

settings.json:

    "qmlFormat.command": "C:\\Qt\\6.3.1\\msvc2019_64\\bin\\qmlformat.exe",
Delgan commented 2 years ago

Thanks for the bug report. I will try to get my hands on a Windows machine and will keep you posted.

SnowMB commented 2 years ago

I just tested with 6.4.0 and same results.

Delgan commented 2 years ago

Hi @SnowMB.

I identified the problem. However, during my tests, a noticed another issue.

When running C:\Qt\6.4.0\msvc2019_64\bin\qmlformat.exe --inplace main.qml (outside of VS Code, note the --inplace argument) it also created a file named main.qml~ which wasn't automatically deleted.

Did you notice something similar with 6.4.0 or is it a problem on my side only? Asking to know if I should implement a workaround inside the qml-format extension or not.

SnowMB commented 2 years ago

Hey @Delgan,

thank you for your time in investigating this issue. 👍

I also get the .qml~ files when running with the --inplace argument. If I understood correctly your extension does not rely on using --inplace so this would not be something you need to fix here?

Personally I just ignore them with git so not a problem for me either.

Delgan commented 2 years ago

Thanks for the test, @SnowMB. :)

Regarding the .qml~ files created, I actually opened a ticket on Qt bug tracker: QTBUG-107685. It should be fixed in one of their upcoming releases.

You're right that this extension originally did not use --inplace. However, I needed to modify the implementation to fix the double new lines bug! Actually, I learned that "\n" is automatically converted in "\r\n" on Windows while writing to a non-binary stream. So, when qmlformat was outputing "\r\n" due to NewlineType=windows, the output I received from the execute process contained "\r\r\n". To solve the problem, the stdout would have to be opened in binary mode, but this is not necessarily very simple or portable. Therefore, I decided to use --inplace internally so that qmlformat can format the file without going through stdout.

I just release v1.0.4, it should fix your issue. :+1:

SnowMB commented 2 years ago

@Delgan thank you so much!