reformatter: fix formatting of js directives

Fix the miscalculation of the start / end of the js directives lines.
Also small optimizations on usage of increment operators. Add a unit
test.

Fixes: QTCREATORBUG-29001
Change-Id: I38923f137dca5c0b89d474cd747a64ec11e62fd9
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Semih Yavuz
2023-04-11 11:11:08 +02:00
parent 8175d5abda
commit 78cf563142
2 changed files with 23 additions and 7 deletions

View File

@@ -109,14 +109,15 @@ public:
}
const QList<SourceLocation> &directives = _doc->jsDirectives();
for (const auto &d: directives) {
quint32 line = 1;
int i = 0;
while (line++ < d.startLine && i++ >= 0)
i = _doc->source().indexOf(QChar('\n'), i);
quint32 line = 0;
const QString &source = _doc->source();
int i = -1;
while (++line < d.startLine)
i = source.indexOf(QChar('\n'), i + 1);
quint32 offset = static_cast<quint32>(i) + d.startColumn;
int endline = _doc->source().indexOf('\n', static_cast<int>(offset) + 1);
int end = endline == -1 ? _doc->source().length() : endline;
quint32 length = static_cast<quint32>(end) - offset;
int endline = source.indexOf('\n', static_cast<int>(offset) + 1);
int end = endline == -1 ? source.length() : endline;
quint32 length = static_cast<quint32>(end) - offset + 1;
out(SourceLocation(offset, length, d.startLine, d.startColumn));
}
if (!directives.isEmpty())

View File

@@ -0,0 +1,15 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
.pragma library
.import QtQml as QTQML
.import QtQuick as QTQUICK
function test() {}