From 55270d73c46e373c40336c743f229d0ba4892d95 Mon Sep 17 00:00:00 2001 From: Christoph Cullmann Date: Mon, 14 Sep 2020 23:10:04 +0200 Subject: [PATCH] replace removed that with next in lru list --- kate/katetabbar.cpp | 51 ++++++++++++++++++++++++++++++++++++++++----- kate/katetabbar.h | 2 +- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/kate/katetabbar.cpp b/kate/katetabbar.cpp index 1ec00225b..2e685d999 100644 --- a/kate/katetabbar.cpp +++ b/kate/katetabbar.cpp @@ -177,7 +177,8 @@ void KateTabBar::setTabDocument(int idx, KTextEditor::Document *doc) void KateTabBar::setCurrentDocument(KTextEditor::Document *doc) { // in any case: update lru counter for this document, might add new element to hash - m_docToLruCounter[doc] = ++m_lruCounter; + // we have a tab after this call, too! + m_docToLruCounterAndHasTab[doc] = std::make_pair(++m_lruCounter, true); // do we have a tab for this document? // if yes => just set as current one @@ -208,18 +209,23 @@ void KateTabBar::setCurrentDocument(KTextEditor::Document *doc) // search for the right tab quint64 minCounter = static_cast(-1); int indexToReplace = 0; + KTextEditor::Document *docToReplace = nullptr; for (int idx = 0; idx < count(); idx++) { QVariant data = tabData(idx); if (!data.isValid()) { continue; } - const quint64 currentCounter = m_docToLruCounter[data.value().doc]; + const quint64 currentCounter = m_docToLruCounterAndHasTab[data.value().doc].first; if (currentCounter <= minCounter) { minCounter = currentCounter; indexToReplace = idx; + docToReplace = data.value().doc; } } + // mark the replace doc as "has no tab" + m_docToLruCounterAndHasTab[docToReplace].second = false; + // replace it's data + set it as active setTabText(indexToReplace, doc->documentName()); setTabDocument(indexToReplace, doc); @@ -230,14 +236,49 @@ void KateTabBar::setCurrentDocument(KTextEditor::Document *doc) void KateTabBar::removeDocument(KTextEditor::Document *doc) { + // purge LRU storage, must work + Q_ASSERT(m_docToLruCounterAndHasTab.erase(doc) == 1); + // remove document if needed, we might have no tab for it, if tab count is limited! const int idx = documentIdx(doc); if (idx != -1) { + // purge the tab we have removeTab(idx); - } - // purge LRU storage, must work - Q_ASSERT(m_docToLruCounter.erase(doc) == 1); + // if we have some tab limit, replace the removed tab with the next best document that has none! + if (m_tabCountLimit > 0) { + quint64 maxCounter = 0; + KTextEditor::Document *docToReplace = nullptr; + for (const auto &lru : m_docToLruCounterAndHasTab) { + // ignore stuff with tabs + if (lru.second.second) { + continue; + } + + // search most recently used one + if (lru.second.first >= maxCounter) { + maxCounter = lru.second.first; + docToReplace = lru.first; + } + } + + // any document found? add tab for it + if (docToReplace) { + // get right icon to use + QIcon icon; + if (docToReplace->isModified()) { + icon = QIcon::fromTheme(QStringLiteral("document-save")); + } + + m_beingAdded = docToReplace; + int inserted = insertTab(idx, docToReplace->documentName()); + setTabIcon(inserted, icon); + + // mark the replace doc as "has a tab" + m_docToLruCounterAndHasTab[docToReplace].second = true; + } + } + } } int KateTabBar::documentIdx(KTextEditor::Document *doc) diff --git a/kate/katetabbar.h b/kate/katetabbar.h index 57de4a77f..87cce6112 100644 --- a/kate/katetabbar.h +++ b/kate/katetabbar.h @@ -159,7 +159,7 @@ private: * simple 64-bit counter, worst thing that can happen on 64-bit wraparound * is a bit strange tab replacement a few times */ - std::unordered_map m_docToLruCounter; + std::unordered_map> m_docToLruCounterAndHasTab; }; #endif // KATE_TAB_BAR_H -- GitLab