File indexing completed on 2024-05-05 04:40:53

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef PROJECT_ITEM_QUICKOPEN
0008 #define PROJECT_ITEM_QUICKOPEN
0009 
0010 #include "duchainitemquickopen.h"
0011 
0012 #include <serialization/indexedstring.h>
0013 #include <language/duchain/identifier.h>
0014 
0015 #include <functional>
0016 #include <type_traits>
0017 
0018 template <typename Type>
0019 class ResultCache
0020 {
0021 public:
0022     ResultCache(std::function<Type()> func)
0023         : m_func(func)
0024     {
0025     }
0026 
0027     /// Mark this cache dirty. A call to cachedResult() will need to refill the cache
0028     inline void markDirty() const
0029     {
0030         m_isDirty = true;
0031     }
0032 
0033     /**
0034      * If marked dirty, calls @p func and stores return value of @p func
0035      *
0036      * @return Cached result of @p func
0037      */
0038     inline Type cachedResult() const
0039     {
0040         if (m_isDirty) {
0041             m_result = m_func();
0042             m_isDirty = false;
0043         }
0044         return m_result;
0045     }
0046 private:
0047     std::function<Type()> m_func;
0048 
0049     mutable Type m_result;
0050     mutable bool m_isDirty = true;
0051 };
0052 
0053 struct CodeModelViewItem
0054 {
0055     CodeModelViewItem()
0056     {
0057     }
0058     CodeModelViewItem(const KDevelop::IndexedString& file, const KDevelop::QualifiedIdentifier& id)
0059         : m_file(file)
0060         , m_id(id)
0061     {
0062     }
0063     KDevelop::IndexedString m_file;
0064     KDevelop::QualifiedIdentifier m_id;
0065 };
0066 
0067 Q_DECLARE_TYPEINFO(CodeModelViewItem, Q_MOVABLE_TYPE);
0068 
0069 using AddedItems = QMap<uint, QList<KDevelop::QuickOpenDataPointer>>;
0070 
0071 class ProjectItemDataProvider
0072     : public KDevelop::QuickOpenDataProviderBase
0073 {
0074     Q_OBJECT
0075 public:
0076     enum ItemTypes {
0077         NoItems = 0,
0078         Classes = 1,
0079         Functions = 2,
0080         AllItemTypes = Classes + Functions
0081     };
0082 
0083     explicit ProjectItemDataProvider(KDevelop::IQuickOpen* quickopen);
0084 
0085     void enableData(const QStringList& items, const QStringList& scopes) override;
0086 
0087     void setFilterText(const QString& text) override;
0088 
0089     void reset() override;
0090 
0091     uint itemCount() const override;
0092     uint unfilteredItemCount() const override;
0093 
0094     static QStringList supportedItemTypes();
0095 private:
0096     KDevelop::QuickOpenDataPointer data(uint pos) const override;
0097 
0098     ItemTypes m_itemTypes;
0099     KDevelop::IQuickOpen* m_quickopen;
0100     QSet<KDevelop::IndexedString> m_files;
0101     QVector<CodeModelViewItem> m_currentItems;
0102     QString m_currentFilter;
0103     QVector<CodeModelViewItem> m_filteredItems;
0104 
0105     //Maps positions to the additional items behind those positions
0106     //Here additional inserted items are stored, that are not represented in m_filteredItems.
0107     //This is needed at least to also show overloaded function declarations
0108     mutable AddedItems m_addedItems;
0109     ResultCache<uint> m_addedItemsCountCache;
0110 };
0111 
0112 #endif