qml_formatter

Crates.ioqml_formatter
lib.rsqml_formatter
version0.2.0
sourcesrc
created_at2022-09-06 15:32:12.484839
updated_at2022-09-29 12:02:43.063729
descriptionQML formatter
homepagehttps://github.com/qarmin/qml_formatter
repositoryhttps://github.com/qarmin/qml_formatter
max_upload_size
id659583
size48,725
RafaƂ Mikrut (qarmin)

documentation

README

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()
Commit count: 39

cargo fmt