qmlls/qmllintsuggestions: Fix wrong fix suggestion offsets

When porting to code to use the logger data directly I failed to account
for the zero-indexed lines and characters used in the LSP.

Fixed and improved the test to ensure this regression doesn't happen
again.

Change-Id: I2a16896a49b38b9e5365d6f70aab3ec285d41d48
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@qt.io>
This commit is contained in:
Maximilian Goldstein 2022-04-22 17:19:56 +02:00
parent 756b54bd83
commit 19e74bd085
2 changed files with 31 additions and 13 deletions

View File

@ -191,9 +191,23 @@ void tst_Qmlls::didOpenTextDocument()
auto list = std::get<ListType>(response);
QList<QPair<QString, QString>> expectedData = {
{ QLatin1StringView("Did you mean \"width\"?"), QLatin1StringView("width") },
{ QLatin1StringView("Did you mean \"z\"?"), QLatin1StringView("z") }
struct ReplacementData
{
QString replacement;
Range range;
};
QHash<QString, ReplacementData> expectedData = {
{ QLatin1StringView("Did you mean \"width\"?"),
{ QLatin1StringView("width"),
Range { Position { 3, 4 }, Position { 3, 10 } } } },
{ QLatin1StringView("Did you mean \"z\"?"),
{ QLatin1StringView("z"),
Range { Position {
3,
12,
},
Position { 3, 15 } } } }
};
QCOMPARE(list.size(), expectedData.size());
@ -222,14 +236,18 @@ void tst_Qmlls::didOpenTextDocument()
QVERIFY(std::holds_alternative<TextEdit>(editVariant));
TextEdit textEdit = std::get<TextEdit>(editVariant);
QString newText = QString::fromUtf8(textEdit.newText);
QPair<QString, QString> data = { title, newText };
QString replacement = QString::fromUtf8(textEdit.newText);
const Range &range = textEdit.range;
qsizetype dataIndex = expectedData.indexOf(data);
QVERIFY2(dataIndex != -1,
qPrintable(QLatin1String("{\"%1\",\"%2\"}").arg(title, newText)));
QVERIFY2(expectedData.contains(title),
qPrintable(QLatin1String("Unexpected fix \"%1\"").arg(title)));
QCOMPARE(replacement, expectedData[title].replacement);
QCOMPARE(range.start.line, expectedData[title].range.start.line);
QCOMPARE(range.start.character, expectedData[title].range.start.character);
QCOMPARE(range.end.line, expectedData[title].range.end.line);
QCOMPARE(range.end.character, expectedData[title].range.end.character);
// Make sure every expected entry only occurs once
expectedData.remove(dataIndex);
expectedData.remove(title);
}
success = true;

View File

@ -221,8 +221,8 @@ void QmlLintSuggestions::diagnose(const QByteArray &uri)
for (const FixSuggestion::Fix &fix : suggestion->fixes) {
QQmlJS::SourceLocation cut = fix.cutLocation;
int line = cut.isValid() ? cut.startLine : 0;
int column = cut.isValid() ? cut.startColumn : 0;
int line = cut.isValid() ? cut.startLine - 1 : 0;
int column = cut.isValid() ? cut.startColumn - 1 : 0;
QJsonObject object;
object[u"lspBeginLine"] = line;
@ -230,8 +230,8 @@ void QmlLintSuggestions::diagnose(const QByteArray &uri)
Position end = { line, column };
addLength(end, srcLoc.isValid() ? srcLoc.offset : 0,
srcLoc.isValid() ? srcLoc.length : 0);
addLength(end, srcLoc.isValid() ? cut.offset : 0,
srcLoc.isValid() ? cut.length : 0);
object[u"lspEndLine"] = end.line;
object[u"lspEndCharacter"] = end.character;