2 unstable releases

0.2.0 Sep 29, 2022
0.1.0 Sep 6, 2022

#158 in Value formatting

MIT license

44KB
1K SLoC

Rust 1K SLoC // 0.0% comments QML 197 SLoC // 0.1% comments Python 5 SLoC

QML Formatter

This repository is simple project which implements basic qml formatter, which is used internally inside my projects.

It is alternative for default QML formatter which sometimes broke ours code, so we want to create own app to format files only with needed rules.

I don't expect to add any options to configure it in runtime.

For now formatter can:

  • remove empty lines from start and end of file
  • connect multiple empty lines into one
  • add at the end empty line
  • add spaces before and after ? :
  • format if/else/for oneliners
  • handle strings inside ', " and `
  • format classes inside other classes

Usage

Formatter checks for qml files recursively inside provided folders:

qml_formatter folder_with_files_to_check folder_with_files_to_check2 -efolder_with_excluded_files -efolder_with_excluded_files2

e.g.

qml_formatter /home/a /home/b -e/home/a/not_to_check -e/home/a/completelly

will list all files inside folder /home/a and /home/b that are not inside /home/a/not_to_check and /home/a/completelly.

By default, app runs in interactive mode which require to confirm formatting, but there is additional argument NO_QUESTION which format files without questions e.g.

qml_formatter /home/a NO_QUESTION

Conversion example

Before:



import QtQuick
import "../../commons/elements"
import "../preparationScreen" as ExamCommons


Text {
    id     : root
    property var able: is_able? no_able: very_able
    signal pressed()
    image       :       "qrc://image.svg"
    layer.effect: ElevationEffect 
    
    {
        elevation: elevation
    }

}


After

import QtQuick
import "../../commons/elements"
import "../preparationScreen" as ExamCommons

Text {
    id: root
    property var able: is_able ? no_able : very_able
    signal pressed()
    image: "qrc://image.svg"
    layer.effect: ElevationEffect {
        elevation: elevation
    }
}

TODO

  • support for Javascript/HTML/C++ comments
  • switch/case
  • better multi-line support

Limitations

Multiline for/if/else without {}

for(abcd)
    if(abcd)
        DBCC()

will be formatted into

for(abcd)
    if(abcd)
    DBCC()

as workaround use always brackets, which are fully supported

for(abcd) {
    if(abcd) {
        DBCC()
    }
}

it works fine with simple oneliners

for(ABCD)
BCDF()

will become

for(ABCD)
    BCDF()

Dependencies

~0.1–6.5MB
~38K SLoC