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

[REG 5.15 -> 6.0] Syntax Highlighter includes '\n' chars in the length

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P1: Critical
    • Resolution: Duplicate
    • Affects Version/s: 6.0.0
    • Fix Version/s: None
    • Component/s: GUI: Text handling
    • Labels:
      None
    • Environment:
      Ubuntu 20.04
    • Platform/s:
      Linux/X11

      Description

      In Qt 6, sometimes carriage return characters are included in the length of QTextLayout formats.

      With this code, we get different results in Qt 5.15 and Qt 6.0:

      #include <QApplication>
      #include <QtWidgets>
      #include <QDebug>
      
      class Highlighter : public QSyntaxHighlighter
      {
      public:
          Highlighter(QTextDocument *parent = nullptr)
              : QSyntaxHighlighter(parent)
          {
              multiLineCommentFormat.setForeground(Qt::red);
              commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
              commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
          }
      
      protected:
          void highlightBlock(const QString &text) override
          {
              setCurrentBlockState(0);
      
              int startIndex = 0;
              if (previousBlockState() != 1)
                  startIndex = text.indexOf(commentStartExpression);
      
              while (startIndex >= 0) {
                  const QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
                  const auto endIndex = match.capturedStart();
                  int commentLength = 0;
                  if (endIndex == -1) {
                      setCurrentBlockState(1);
                      commentLength = text.length() - startIndex;
                  } else {
                      commentLength = endIndex - startIndex + match.capturedLength();
                  }
                  setFormat(startIndex, commentLength, multiLineCommentFormat);
                  startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
              }
          }
      
      private:
          QRegularExpression commentStartExpression;
          QRegularExpression commentEndExpression;
          QTextCharFormat multiLineCommentFormat;
      };
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          QTextEdit editor;
          auto highlighter = new Highlighter(editor.document());
          Q_UNUSED(highlighter);
          editor.setPlainText("Here is /* a\n"
                              "multi-lines\n"
                              "comment in C++ */ !");
      
          const auto firstLine = editor.textCursor().block();
          const auto format1 = firstLine.layout()->formats().first();
      
          const auto secondLine = editor.textCursor().block().next();
          const auto format2 = secondLine.layout()->formats().first();
      
          qDebug() << "First line: From" << format1.start << "/" << format1.length << "chars";
          qDebug() << "Second line: From" << format2.start << "/" << format2.length << "chars";
      
          editor.show();
          return app.exec();
      }
      

      Qt 5.15:

      First line: From 8 / 4 chars
      Second line: From 0 / 11 chars
      

      Qt 6.0:

      First line: From 8 / 4 chars
      Second line: From 0 / 12 chars
      

       

        Attachments

          Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              Assignee:
              esabraha Eskil Abrahamsen Blomfeldt
              Reporter:
              maluna34 Manuel Chataigner
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes