Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-120484

qmlls: formatting removes some comments

    XMLWordPrintable

Details

    • ff6d16d58 (dev), ada7ce820 (6.7), dfb65530f (dev), f6fc3cc66 (dev), 35bba9ab7 (dev), b0cc4d024 (dev), 9ee160941 (dev)

    Description

      It seems that comment formatting is broken in qmlls 6.7: comments either disappear after formatting or they end up at the wrong places.

      The reason for that is a bad cooperation between the new FileLocationRegions introduced by the autocompletion feature and the comment placement method AstComments::collectComment(). collectComments() will try to place comments next to the nearest FileLocationRegion, which now might be one that was introduced by the autocompletion feature.

       

      In qmlformat and tst_qmlformat, this is not a problem because the FileLocations were not modified in the "normal" Dom. In qmlls, on the other hand, we add extra FileLocations during the Dom construction and then the formatting gets impacted by that bug.

       

      The comments being at wrong places is explained by collectComment() attaching the comment to the wrong FileLocationRegion. Some comments, however, disappear in the formatted code: this is due to inconsistencies in the writeOut() methods, for example QmlObject::writeOut() will do:

       ow.writeRegion(LeftBraceRegion, u" {").newline();
      /* ... */
      ow.ensureNewline().write(u"}");}}
      

      where writeRegion() will add comments before/after the LeftBraceRegion but write() will ignore any comment attached to the RightBraceRegion filelocationregion, for example in:

      QtObject {
          // this comment is attached to the RightBraceRegion and will disappear when formatted in qmlls when using with https://codereview.qt-project.org/c/qt/qtdeclarative/+/526982/4, }
      

      Comments being moved or deleted during formatting is not too bad as the user can always revert the formatting via CTRL-Z. But it is still a weird for the user if the same code that would get formatted correctly via qmlformat does not work correctly in qmlls.

       

      Another reproducer that works on dev without https://codereview.qt-project.org/c/qt/qtdeclarative/+/526982/4 :

      import QtQml
      
      QtObject {
          // This to enum
          enum Foo {
              A = 3, // This is A
              B, // This is B
              C = 4, // This is C
              D  // This is D
          }
      }
      

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-120484
          # Subject Branch Project Status CR V

          Activity

            People

              semih.yavuz Semih Yavuz
              sami.shalayel Sami Shalayel
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes