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

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_KDEV_CODECOMPLETIONITEM_H
0008 #define KDEVPLATFORM_KDEV_CODECOMPLETIONITEM_H
0009 
0010 #include "../duchain/duchainpointer.h"
0011 
0012 #include <KTextEditor/CodeCompletionModel>
0013 
0014 namespace KTextEditor {
0015 class CodeCompletionModel;
0016 class Range;
0017 class Cursor;
0018 }
0019 
0020 class QModelIndex;
0021 
0022 namespace KDevelop {
0023 class CodeCompletionModel;
0024 
0025 struct CompletionTreeNode;
0026 class CompletionTreeItem;
0027 class IndexedType;
0028 
0029 class KDEVPLATFORMLANGUAGE_EXPORT CompletionTreeElement
0030     : public QSharedData
0031 {
0032 public:
0033     CompletionTreeElement();
0034 
0035     virtual ~CompletionTreeElement();
0036 
0037     CompletionTreeElement* parent() const;
0038 
0039     /// Reparenting is not supported. This is only allowed if parent() is still zero.
0040     void setParent(CompletionTreeElement*);
0041 
0042     int rowInParent() const;
0043 
0044     int columnInParent() const;
0045 
0046     /// Each element is either a node, or an item.
0047 
0048     CompletionTreeNode* asNode();
0049 
0050     CompletionTreeItem* asItem();
0051 
0052     template <class T>
0053     T* asItem()
0054     {
0055         return dynamic_cast<T*>(this);
0056     }
0057 
0058     template <class T>
0059     const T* asItem() const
0060     {
0061         return dynamic_cast<const T*>(this);
0062     }
0063 
0064     const CompletionTreeNode* asNode() const;
0065 
0066     const CompletionTreeItem* asItem() const;
0067 
0068 private:
0069     CompletionTreeElement* m_parent;
0070     int m_rowInParent;
0071 };
0072 
0073 struct KDEVPLATFORMLANGUAGE_EXPORT CompletionTreeNode
0074     : public CompletionTreeElement
0075 {
0076     CompletionTreeNode();
0077     ~CompletionTreeNode() override;
0078 
0079     KTextEditor::CodeCompletionModel::ExtraItemDataRoles role;
0080     QVariant roleValue;
0081 
0082     /// Will append the child, and initialize it correctly to create a working tree-structure
0083     void appendChild(QExplicitlySharedDataPointer<CompletionTreeElement> );
0084     void appendChildren(const QList<QExplicitlySharedDataPointer<CompletionTreeElement>>& children);
0085     void appendChildren(const QList<QExplicitlySharedDataPointer<CompletionTreeItem>>& children);
0086 
0087     /// @warning Do not manipulate this directly, that's bad for consistency. Use appendChild instead.
0088     QList<QExplicitlySharedDataPointer<CompletionTreeElement>> children;
0089 };
0090 
0091 class KDEVPLATFORMLANGUAGE_EXPORT CompletionTreeItem
0092     : public CompletionTreeElement
0093 {
0094 public:
0095 
0096     /// Execute the completion item. The default implementation does nothing.
0097     virtual void execute(KTextEditor::View* view, const KTextEditor::Range& word);
0098 
0099     /// Should return normal completion data, @see KTextEditor::CodeCompletionModel
0100     /// The default implementation returns "unimplemented", so re-implement it!
0101     /// The duchain is not locked when this is called
0102     virtual QVariant data(const QModelIndex& index, int role, const CodeCompletionModel* model) const;
0103 
0104     /// Should return the inheritance-depth. The completion-items don't need to return it through the data() function.
0105     virtual int inheritanceDepth() const;
0106     /// Should return the argument-hint depth. The completion-items don't need to return it through the data() function.
0107     virtual int argumentHintDepth() const;
0108 
0109     /// The default-implementation calls DUChainUtils::completionProperties
0110     virtual KTextEditor::CodeCompletionModel::CompletionProperties completionProperties() const;
0111 
0112     /// If this item represents a Declaration, this should return the declaration.
0113     /// The default-implementation returns zero.
0114     virtual DeclarationPointer declaration() const;
0115 
0116     /// Should return the types should be used for matching items against this one when it's an argument hint.
0117     /// The matching against all types should be done, and the best one will be used as final match result.
0118     virtual QList<IndexedType> typeForArgumentMatching() const;
0119 
0120     /// Should return whether this completion-items data changes with input done by the user during code-completion.
0121     /// Returning true is very expensive.
0122     virtual bool dataChangedWithInput() const;
0123 };
0124 
0125 /// A custom-group node, that can be used as-is. Just create it, and call appendChild to add group items.
0126 /// The items in the group will be shown in the completion-list with a group-header that contains the given name
0127 struct KDEVPLATFORMLANGUAGE_EXPORT CompletionCustomGroupNode
0128     : public CompletionTreeNode
0129 {
0130     /// @param inheritanceDepth See KTextEditor::CodeCompletionModel::GroupRole
0131     explicit CompletionCustomGroupNode(const QString& groupName, int inheritanceDepth = 700);
0132 
0133     int inheritanceDepth;
0134 };
0135 
0136 using CompletionTreeItemPointer = QExplicitlySharedDataPointer<CompletionTreeItem>;
0137 using CompletionTreeElementPointer = QExplicitlySharedDataPointer<CompletionTreeElement>;
0138 }
0139 
0140 Q_DECLARE_METATYPE(KDevelop::CompletionTreeElementPointer)
0141 
0142 #endif