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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #ifndef KDEVPLATFORM_STATICASSISTANT_H
0008 #define KDEVPLATFORM_STATICASSISTANT_H
0009 
0010 #include <language/languageexport.h>
0011 #include <serialization/indexedstring.h>
0012 #include <language/duchain/topducontext.h>
0013 
0014 #include <interfaces/iassistant.h>
0015 
0016 namespace KTextEditor {
0017 class Document;
0018 class View;
0019 class Range;
0020 }
0021 
0022 namespace KDevelop {
0023 class ILanguageSupport;
0024 class StaticAssistantPrivate;
0025 
0026 /**
0027  * @brief This class serves as a base for long-living assistants
0028  *
0029  * Normally, an assistant only lives for a short time, and is indirectly owned by a KDevelop::Problem.
0030  * Static assistants are owned by the language support plugins and exist as long as the language plugin is loaded.
0031  * They are not created by KDevelop::Problem instances, instead, they check for problematic code for themselves,
0032  * by tracking document changes via the textChanged() method.
0033  *
0034  * Note that static assistants are not bound to a single document/view.
0035  * Instead the current document/view we're looking at is passed via the textChanged() method.
0036  *
0037  * Register instances of this class view StaticAssistantsManager::registerAssistant
0038  *
0039  * @sa textChanged()
0040  * @sa StaticAssistantsManager::registerAssistant
0041  */
0042 class KDEVPLATFORMLANGUAGE_EXPORT StaticAssistant
0043     : public IAssistant
0044 {
0045     Q_OBJECT
0046 
0047 public:
0048     using Ptr = QExplicitlySharedDataPointer<StaticAssistant>;
0049 
0050     explicit StaticAssistant(ILanguageSupport* supportedLanguage);
0051     ~StaticAssistant() override;
0052 
0053     /**
0054      * Language this static assistant supports
0055      *
0056      * textChanged() will only be called for documents with language equal to this assistant's language
0057      * @sa textChanged(0)
0058      */
0059     ILanguageSupport* supportedLanguage() const;
0060 
0061     /**
0062      * Invoked whenever text inside a view was changed by the user
0063      *
0064      * Reimplement in subclass
0065      */
0066     virtual void textChanged(KTextEditor::Document* doc, const KTextEditor::Range& invocationRange,
0067                              const QString& removedText = QString()) = 0;
0068     /**
0069      * Whether it's worth showing this assistant to the user
0070      *
0071      * Reimplement in subclass
0072      */
0073     virtual bool isUseful() const = 0;
0074 
0075     /**
0076      * The range the assistant should be displayed in.
0077      */
0078     virtual KTextEditor::Range displayRange() const = 0;
0079 
0080     virtual void updateReady(const IndexedString&, const KDevelop::ReferencedTopDUContext&) { }
0081 
0082 private:
0083     const QScopedPointer<class StaticAssistantPrivate> d_ptr;
0084     Q_DECLARE_PRIVATE(StaticAssistant)
0085 };
0086 }
0087 
0088 #endif // KDEVPLATFORM_STATICASSISTANT_H