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

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 KDEVPLATFORM_CODECOMPLETIONCONTEXT_H
0008 #define KDEVPLATFORM_CODECOMPLETIONCONTEXT_H
0009 
0010 #include "../duchain/duchainpointer.h"
0011 #include <language/languageexport.h>
0012 #include "../editor/cursorinrevision.h"
0013 #include "codecompletionitem.h"
0014 
0015 namespace KTextEditor {
0016 class View;
0017 class Cursor;
0018 }
0019 
0020 namespace KDevelop {
0021 class CursorInRevision;
0022 
0023 class CompletionTreeItem;
0024 class CompletionTreeElement;
0025 using CompletionTreeItemPointer = QExplicitlySharedDataPointer<CompletionTreeItem>;
0026 using CompletionTreeElementPointer = QExplicitlySharedDataPointer<CompletionTreeElement>;
0027 
0028 /**
0029  * This class is responsible for finding out what kind of completion is needed, what expression should be evaluated for the container-class of the completion, what conversion will be applied to the result of the completion, etc.
0030  * */
0031 class KDEVPLATFORMLANGUAGE_EXPORT CodeCompletionContext
0032     : public QSharedData
0033 {
0034 public:
0035     using Ptr = QExplicitlySharedDataPointer<CodeCompletionContext>;
0036 
0037     /**
0038      * @param text the text to analyze. It usually is the text in the range starting at the beginning of the context,
0039      *    and ending at the position where completion should start
0040      *
0041      * @warning The du-chain must be unlocked when this is called
0042      * */
0043     CodeCompletionContext(const KDevelop::DUContextPointer& context, const QString& text,
0044                           const KDevelop::CursorInRevision& position, int depth = 0);
0045     virtual ~CodeCompletionContext();
0046 
0047     /**
0048      * @return Whether this context is valid for code-completion
0049      */
0050     bool isValid() const;
0051 
0052     /**
0053      * @return Depth of the context. The basic completion-context has depth 0, its parent 1, and so on..
0054      */
0055     int depth() const;
0056 
0057     /**
0058      * Computes the full set of completion items, using the information retrieved earlier.
0059      * Should only be called on the first context, parent contexts are included in the computations.
0060      *
0061      * @param abort Checked regularly, and if false, the computation is aborted.
0062      *
0063      * @warning Please check @p abort and @p isValid when reimplementing this method
0064      */
0065     virtual QList<KDevelop::CompletionTreeItemPointer> completionItems(bool& abort, bool fullCompletion = true) = 0;
0066 
0067     /**
0068      * After completionItems(..) has been called, this may return completion-elements that are already grouped,
0069      * for example using custom grouping(@see CompletionCustomGroupNode
0070      */
0071     virtual QList<KDevelop::CompletionTreeElementPointer> ungroupedElements();
0072 
0073     /**
0074      * In the case of recursive argument-hints, there may be a chain of parent-contexts, each for the higher argument-matching
0075      * The parentContext() should always have the access-operation FunctionCallAccess.
0076      * When a completion-list is computed, the members of the list can be highlighted that match
0077      * the corresponding parentContext()->functions() function-argument, or parentContext()->additionalMatchTypes()
0078      */
0079     CodeCompletionContext* parentContext();
0080 
0081     ///Sets the new parent context, and also updates the depth
0082     void setParentContext(QExplicitlySharedDataPointer<CodeCompletionContext> newParent);
0083 
0084     DUContext* duContext() const;
0085 
0086 protected:
0087     static QString extractLastLine(const QString& str);
0088 
0089     QString m_text;
0090     int m_depth;
0091     bool m_valid;
0092     KDevelop::CursorInRevision m_position;
0093 
0094     KDevelop::DUContextPointer m_duContext;
0095 
0096     QExplicitlySharedDataPointer<CodeCompletionContext> m_parentContext;
0097 };
0098 }
0099 
0100 Q_DECLARE_METATYPE(KDevelop::CodeCompletionContext::Ptr)
0101 
0102 #endif