diff options
| -rw-r--r-- | src/querycompleter.cpp | 18 | ||||
| -rw-r--r-- | tests/test_querycompleter.cpp | 41 |
2 files changed, 55 insertions, 4 deletions
diff --git a/src/querycompleter.cpp b/src/querycompleter.cpp index 44ca010..1e86c1b 100644 --- a/src/querycompleter.cpp +++ b/src/querycompleter.cpp @@ -46,9 +46,19 @@ CompletionContext completionContext(const QString &text, int cursor) const int start = tokenStart(text, cursor); const QString token = text.mid(start, cursor - start); - ctx.kind = CompletionContext::Prefix; - ctx.stem = token; - ctx.replaceFrom = start; - ctx.replaceLength = token.size(); + const int colon = token.indexOf(QLatin1Char(':')); + if (colon < 0) { + ctx.kind = CompletionContext::Prefix; + ctx.stem = token; + ctx.replaceFrom = start; + ctx.replaceLength = token.size(); + return ctx; + } + + ctx.kind = CompletionContext::Value; + ctx.prefix = token.left(colon).toLower(); + ctx.stem = token.mid(colon + 1); + ctx.replaceFrom = start + colon + 1; + ctx.replaceLength = ctx.stem.size(); return ctx; } diff --git a/tests/test_querycompleter.cpp b/tests/test_querycompleter.cpp index 9278a1e..4621b19 100644 --- a/tests/test_querycompleter.cpp +++ b/tests/test_querycompleter.cpp @@ -29,6 +29,10 @@ private slots: void bareWordCompletesPrefix(); void wordAfterOperatorCompletesPrefix(); void prefixReplaceSpanCoversTheWord(); + void colonSwitchesToValue(); + void valueReplaceSpanExcludesThePrefix(); + void prefixIsLowercased(); + void emptyValueAfterColonStillCompletes(); }; void TestQueryCompleter::emptyTextCompletesPrefix() @@ -62,5 +66,42 @@ void TestQueryCompleter::prefixReplaceSpanCoversTheWord() QCOMPARE(ctx.replaceLength, 2); } +void TestQueryCompleter::colonSwitchesToValue() +{ + const QString text = QStringLiteral("tag:sho"); + const CompletionContext ctx = completionContext(text, text.size()); + QCOMPARE(ctx.kind, CompletionContext::Value); + QCOMPARE(ctx.prefix, QStringLiteral("tag")); + QCOMPARE(ctx.stem, QStringLiteral("sho")); +} + +void TestQueryCompleter::valueReplaceSpanExcludesThePrefix() +{ + // Accepting must overwrite "sho" only, never "tag:". + const QString text = QStringLiteral("tag:sho"); + const CompletionContext ctx = completionContext(text, text.size()); + QCOMPARE(ctx.replaceFrom, 4); + QCOMPARE(ctx.replaceLength, 3); +} + +void TestQueryCompleter::prefixIsLowercased() +{ + const QString text = QStringLiteral("TAG:sho"); + const CompletionContext ctx = completionContext(text, text.size()); + QCOMPARE(ctx.prefix, QStringLiteral("tag")); +} + +void TestQueryCompleter::emptyValueAfterColonStillCompletes() +{ + // "tag:" with the cursor at the end offers every tag. + const QString text = QStringLiteral("tag:"); + const CompletionContext ctx = completionContext(text, text.size()); + QCOMPARE(ctx.kind, CompletionContext::Value); + QCOMPARE(ctx.prefix, QStringLiteral("tag")); + QCOMPARE(ctx.stem, QString()); + QCOMPARE(ctx.replaceFrom, 4); + QCOMPARE(ctx.replaceLength, 0); +} + QTEST_MAIN(TestQueryCompleter) #include "test_querycompleter.moc" |
