diff --git a/addons/tabswitcher/autotests/tabswitchertest.h b/addons/tabswitcher/autotests/tabswitchertest.h --- a/addons/tabswitcher/autotests/tabswitchertest.h +++ b/addons/tabswitcher/autotests/tabswitchertest.h @@ -32,5 +32,6 @@ private Q_SLOTS: void testLongestCommonPrefix(); + void testLongestCommonPrefix_data(); }; diff --git a/addons/tabswitcher/autotests/tabswitchertest.cpp b/addons/tabswitcher/autotests/tabswitchertest.cpp --- a/addons/tabswitcher/autotests/tabswitchertest.cpp +++ b/addons/tabswitcher/autotests/tabswitchertest.cpp @@ -35,30 +35,43 @@ void KateTabSwitcherTest::testLongestCommonPrefix() { - // standard case + QFETCH(std::vector, input_strings); + QFETCH(QString, expected); + + QCOMPARE(detail::longestCommonPrefix(input_strings), expected); +} + +void KateTabSwitcherTest::testLongestCommonPrefix_data() +{ + QTest::addColumn>("input_strings"); + QTest::addColumn("expected"); std::vector strs; + + strs.clear(); strs.push_back(QLatin1String("/home/user1/a")); strs.push_back(QLatin1String("/home/user1/bc")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("/home/user1/")); + QTest::newRow("standard case") << strs << QString(QLatin1String("/home/user1/")); - // empty string at the end of the list strs.clear(); strs.push_back(QLatin1String("/home/a")); strs.push_back(QLatin1String("/home/b")); strs.push_back(QLatin1String("")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("")); + QTest::newRow("empty string at the end of the list") << strs << QString(); - // empty string not only at the end of the list strs.clear(); strs.push_back(QLatin1String("")); strs.push_back(QLatin1String("/home/a")); strs.push_back(QLatin1String("/home/b")); strs.push_back(QLatin1String("")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("")); + QTest::newRow("empty string not only at the end of the list") << strs << QString(); - // a prefix with length 1 strs.clear(); strs.push_back(QLatin1String("/home/a")); strs.push_back(QLatin1String("/etc/b")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("/")); + QTest::newRow("a prefix with length 1") << strs << QString(QLatin1String("/")); + + strs.clear(); + strs.push_back(QLatin1String("a")); + strs.push_back(QLatin1String("a")); + QTest::newRow("two equal strings") << strs << QString(QLatin1String("a")); } diff --git a/addons/tabswitcher/tabswitcherfilesmodel.h b/addons/tabswitcher/tabswitcherfilesmodel.h --- a/addons/tabswitcher/tabswitcherfilesmodel.h +++ b/addons/tabswitcher/tabswitcherfilesmodel.h @@ -80,6 +80,6 @@ FilenameList data_; }; -QString longestCommonPrefix(std::vector& strs); +QString longestCommonPrefix(std::vector const & strs); } diff --git a/addons/tabswitcher/tabswitcherfilesmodel.cpp b/addons/tabswitcher/tabswitcherfilesmodel.cpp --- a/addons/tabswitcher/tabswitcherfilesmodel.cpp +++ b/addons/tabswitcher/tabswitcherfilesmodel.cpp @@ -30,7 +30,7 @@ * see also http://www.cplusplus.com/forum/beginner/83540/ * Note that if strs contains the empty string, the result will be "" */ - QString longestCommonPrefix(std::vector& strs) { + QString longestCommonPrefix(std::vector const & strs) { int n = INT_MAX; if (strs.size() <= 0) { return QString(); diff --git a/kate/katequickopen.cpp b/kate/katequickopen.cpp --- a/kate/katequickopen.cpp +++ b/kate/katequickopen.cpp @@ -163,6 +163,20 @@ */ const QList sortedViews(m_mainWindow->viewManager()->sortedViews()); + auto prettyDocumentUrl = [](const QUrl & url) { + if (!url.isEmpty() && url.isLocalFile()) { + return url.toLocalFile(); + } else { + return url.toString(); + } + }; + + auto addToAlreadySeen = [&alreadySeenFiles](KTextEditor::Document *doc) { + if (!doc->url().isEmpty() && doc->url().isLocalFile()) { + alreadySeenFiles.insert(doc->url().toLocalFile()); + } + }; + /** * now insert them in order */ @@ -187,15 +201,13 @@ font.setBold(true); itemName->setFont(font); - QStandardItem *itemUrl = new QStandardItem(doc->url().toString()); + QStandardItem *itemUrl = new QStandardItem(prettyDocumentUrl(doc->url())); itemUrl->setEditable(false); m_base_model->setItem(linecount, 0, itemName); m_base_model->setItem(linecount, 1, itemUrl); linecount++; - if (!doc->url().isEmpty() && doc->url().isLocalFile()) { - alreadySeenFiles.insert(doc->url().toLocalFile()); - } + addToAlreadySeen(doc); // select second document, that is the last used (beside the active one) if (linecount == 2) { @@ -215,7 +227,6 @@ continue; } - //QStandardItem *item=new QStandardItem(i18n("%1: %2",doc->documentName(),doc->url().toString())); QStandardItem *itemName = new QStandardItem(doc->documentName()); itemName->setData(qVariantFromValue(QPointer (doc)), DocumentRole); @@ -225,15 +236,13 @@ font.setBold(true); itemName->setFont(font); - QStandardItem *itemUrl = new QStandardItem(doc->url().toString()); + QStandardItem *itemUrl = new QStandardItem(prettyDocumentUrl(doc->url())); itemUrl->setEditable(false); m_base_model->setItem(linecount, 0, itemName); m_base_model->setItem(linecount, 1, itemUrl); linecount++; - if (!doc->url().isEmpty() && doc->url().isLocalFile()) { - alreadySeenFiles.insert(doc->url().toLocalFile()); - } + addToAlreadySeen(doc); } /**