qmljsreformatter: support nullish coalescing

qmljsscanner did interpret ?? and ?. as a single ? and thus as the
start of a ternary operator, breaking reformatting.

Fixes: QTCREATORBUG-27344
Change-Id: I0429d20aed0196743f1c20e6ceac11351d5a7a3b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Fawzi Mohamed
2022-05-05 16:36:12 +02:00
parent 8bb63ebc9a
commit 556ec7c20e
3 changed files with 23 additions and 2 deletions

View File

@@ -957,10 +957,10 @@ protected:
accept(ast->left); accept(ast->left);
// in general, avoid splitting at the operator // in general, avoid splitting at the operator
// but && and || are ok // but && || and ?? are ok
qreal splitBadness = 30; qreal splitBadness = 30;
if (ast->op == QSOperator::And if (ast->op == QSOperator::And
|| ast->op == QSOperator::Or) || ast->op == QSOperator::Or || ast->op == QSOperator::Coalesce)
splitBadness = 0; splitBadness = 0;
addPossibleSplit(splitBadness); addPossibleSplit(splitBadness);

View File

@@ -498,6 +498,18 @@ QList<Token> Scanner::operator()(const QString &text, int startState)
setRegexpMayFollow(&_state, true); setRegexpMayFollow(&_state, true);
break; break;
case '?':
switch (la.unicode()) {
case '?':
case '.':
tokens.append(Token(index, 2, Token::Delimiter));
index += 2;
default:
tokens.append(Token(index++, 1, Token::Delimiter));
}
setRegexpMayFollow(&_state, true);
break;
default: default:
if (ch.isSpace()) { if (ch.isSpace()) {
do { do {

View File

@@ -0,0 +1,9 @@
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
property string otherString: ""
property string aProp: otherString ?? "N/A"
property string bProp: otherString?.trim()
property int unrelatedProp: 10
}