File indexing completed on 2024-05-12 04:37:45

0001 /*
0002     SPDX-FileCopyrightText: 2006-2008 Hamish Rodda <rodda@kde.org>
0003     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDEVPLATFORM_CODECOMPLETIONITEMGROUPER_H
0009 #define KDEVPLATFORM_CODECOMPLETIONITEMGROUPER_H
0010 
0011 #include "codecompletionmodel.h"
0012 #include "codecompletionitem.h"
0013 #include <language/duchain/duchain.h>
0014 #include <language/duchain/duchainlock.h>
0015 
0016 namespace KDevelop {
0017 ///Always the last item of a grouping chain: Only inserts the items
0018 struct CodeCompletionItemLastGrouper
0019 {
0020     CodeCompletionItemLastGrouper(QList<QExplicitlySharedDataPointer<CompletionTreeElement>>& tree,
0021                                   CompletionTreeNode* parent, const QList<CompletionTreeItemPointer>& items)
0022     {
0023         tree.reserve(tree.size() + items.size());
0024         for (auto& item : items) {
0025             item->setParent(parent);
0026             tree << QExplicitlySharedDataPointer<CompletionTreeElement>(item.data());
0027         }
0028     }
0029 };
0030 
0031 ///Helper class that helps us grouping the completion-list. A chain of groupers can be built, by using NextGrouper.
0032 template <class KeyExtractor, class NextGrouper = CodeCompletionItemLastGrouper>
0033 struct CodeCompletionItemGrouper
0034 {
0035     using KeyType = typename KeyExtractor::KeyType;
0036 
0037     CodeCompletionItemGrouper(QList<QExplicitlySharedDataPointer<CompletionTreeElement>>& tree,
0038                               CompletionTreeNode* parent, const QList<CompletionTreeItemPointer>& items)
0039     {
0040         using GroupMap = QMap<KeyType, QList<CompletionTreeItemPointer>>;
0041         GroupMap groups;
0042 
0043         for (auto& item : items) {
0044             KeyType key = KeyExtractor::extract(item);
0045             typename GroupMap::iterator it = groups.find(key);
0046             if (it == groups.end())
0047                 it = groups.insert(key, QList<CompletionTreeItemPointer>());
0048 
0049             (*it).append(item);
0050         }
0051 
0052         tree.reserve(tree.size() + groups.size());
0053         for (typename GroupMap::const_iterator it = groups.constBegin(); it != groups.constEnd(); ++it) {
0054             QExplicitlySharedDataPointer<CompletionTreeNode> node(new CompletionTreeNode());
0055             node->setParent(parent);
0056             node->role = ( KTextEditor::CodeCompletionModel::ExtraItemDataRoles )KeyExtractor::Role;
0057             node->roleValue = QVariant(it.key());
0058 
0059             tree << QExplicitlySharedDataPointer<CompletionTreeElement>(node.data());
0060 
0061             NextGrouper nextGrouper(node->children, node.data(), *it);
0062         }
0063     }
0064 };
0065 
0066 ///Extracts the argument-hint depth from completion-items, to be used in ItemGrouper for grouping by argument-hint depth.
0067 struct ArgumentHintDepthExtractor
0068 {
0069     using KeyType = int;
0070     enum { Role = KTextEditor::CodeCompletionModel::ArgumentHintDepth };
0071 
0072     static KeyType extract(const CompletionTreeItemPointer& item)
0073     {
0074         return item->argumentHintDepth();
0075     }
0076 };
0077 
0078 struct InheritanceDepthExtractor
0079 {
0080     using KeyType = int;
0081 
0082     enum { Role = KTextEditor::CodeCompletionModel::InheritanceDepth };
0083 
0084     static KeyType extract(const CompletionTreeItemPointer& item)
0085     {
0086         return item->inheritanceDepth();
0087     }
0088 };
0089 
0090 struct SimplifiedAttributesExtractor
0091 {
0092     using KeyType = int;
0093 
0094     enum { Role = KTextEditor::CodeCompletionModel::CompletionRole };
0095 
0096     static const int groupingProperties;
0097 
0098     static KeyType extract(const CompletionTreeItemPointer& item)
0099     {
0100         DUChainReadLocker lock(DUChain::lock());
0101         return item->completionProperties() & groupingProperties;
0102     }
0103 };
0104 }
0105 
0106 #endif // KDEVPLATFORM_CODECOMPLETIONITEMGROUPER_H