aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmljsutils.cpp
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2022-04-14 15:31:37 +0200
committerAndrei Golubev <andrei.golubev@qt.io>2022-04-25 11:44:26 +0200
commit07c4d31cfdcedf5bb9fb8c0044f09940cc22d483 (patch)
treece6616fe9d4bbf4e3497a398d0c1bfdc2610f75b /src/qmlcompiler/qqmljsutils.cpp
parentb4e7b2a9e4af6a03ed51fd1f9c32b47d9945b1b5 (diff)
Improve consistency of QQmlJSUtils::didYouMean()
We have to either sort the input candidate list or ensure that we always get same-ordered candidates at the call sites. The latter seems rather problematic since we usually use QHash::keys() as candidates so blarantly sort the candidates instead Complexity-wise, this adds an extra O(n * log(n)) procedure (where n is the size of the candidates list) which might or might not be amortized by the main logic (depends on average input/candidate string lengths) Change-Id: Ib528d453e2f4b2d6646f1451f5aefa559a0bdc10 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qmlcompiler/qqmljsutils.cpp')
-rw-r--r--src/qmlcompiler/qqmljsutils.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/qmlcompiler/qqmljsutils.cpp b/src/qmlcompiler/qqmljsutils.cpp
index 9feb102298..b593b02b9a 100644
--- a/src/qmlcompiler/qqmljsutils.cpp
+++ b/src/qmlcompiler/qqmljsutils.cpp
@@ -31,11 +31,22 @@
#include <algorithm>
std::optional<FixSuggestion> QQmlJSUtils::didYouMean(const QString &userInput,
- const QStringList &candidates,
+ QStringList candidates,
QQmlJS::SourceLocation location)
{
QString shortestDistanceWord;
int shortestDistance = userInput.length();
+
+ // Most of the time the candidates are keys() from QHash, which means that
+ // running this function in the seemingly same setup might yield different
+ // best cadidate (e.g. imagine a typo 'thing' with candidates 'thingA' vs
+ // 'thingB'). This is especially flaky in e.g. test environment where the
+ // results may differ (even when the global hash seed is fixed!) when
+ // running one test vs the whole test suite (recall platform-dependent
+ // QSKIPs). There could be user-visible side effects as well, so just sort
+ // the candidates to guarantee consistent results
+ std::sort(candidates.begin(), candidates.end());
+
for (const QString &candidate : candidates) {
/*
* Calculate the distance between the userInput and candidate using Damerau–Levenshtein