[utilities/kate] addons/project: Fix select file when path gets flattened

Waqar Ahmed <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 7d5a7c964b130ca1e3eab717dc33a0c4d7ccd9e1 by Waqar Ahmed.
Committed on 28/07/2026 at 08:21.
Pushed by waqar into branch 'master'.

Fix select file when path gets flattened

M  +20   -20   addons/project/kateprojectviewtree.cpp

https://invent.kde.org/utilities/kate/-/commit/7d5a7c964b130ca1e3eab717dc33a0c4d7ccd9e1

diff --git a/addons/project/kateprojectviewtree.cpp b/addons/project/kateprojectviewtree.cpp
index 0d2e6618a0..04ab49fa2e 100644
--- a/addons/project/kateprojectviewtree.cpp
+++ b/addons/project/kateprojectviewtree.cpp
@@ -238,28 +238,28 @@ KateProjectViewTree::~KateProjectViewTree() = default;
 
 void KateProjectViewTree::selectFile(const QString &file)
 {
-    /**
-     * get item if any
-     */
-    QStandardItem *item = m_project->itemForFile(file);
-    if (!item) {
-        return;
-    }
-
-    /**
-     * select it
-     */
-    const auto index = static_cast<QSortFilterProxyModel *>(model())->mapFromSource(m_project->model()->indexFromItem(item));
-    if (!index.isValid()) {
-        return;
-    }
+    // We do 2 iterations
+    // first iteration for normal case
+    // second one is for the case where directories are flattened into
+    // a single node
+    for (int i = 0; i < 2; i++) {
+        const QStandardItem *item = m_project->itemForFile(file);
+        const auto proxyModel = static_cast<QSortFilterProxyModel *>(model());
+        const auto index = item ? proxyModel->mapFromSource(m_project->model()->indexFromItem(item)) : QModelIndex();
+        if (!index.isValid()) {
+            return;
+        }
 
-    // scrollTo may lead to path flattening (see flattenPath) which might invalidate this index
-    const QPersistentModelIndex persistentIndex = index;
+        // scrollTo may lead to path flattening (see flattenPath) which might invalidate this index
+        const QPersistentModelIndex persistentIndex = index;
 
-    scrollTo(index, QAbstractItemView::EnsureVisible);
-    if (persistentIndex.isValid()) {
-        selectionModel()->setCurrentIndex(persistentIndex, QItemSelectionModel::Clear | QItemSelectionModel::Select);
+        scrollTo(index, QAbstractItemView::EnsureVisible);
+        if (persistentIndex.isValid()) {
+            selectionModel()->setCurrentIndex(persistentIndex, QItemSelectionModel::Clear | QItemSelectionModel::Select);
+            return;
+        } else {
+            // index was invalidated due to flattening, try to select file again
+        }
     }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.