File indexing completed on 2024-05-12 04:39:13

0001 /*
0002     SPDX-FileCopyrightText: 2014 Sergey Kalinichev <kalinichev.so.0@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "missingincludepathproblem.h"
0008 
0009 #include <interfaces/icore.h>
0010 #include <interfaces/iproject.h>
0011 #include <interfaces/iprojectcontroller.h>
0012 #include <custom-definesandincludes/idefinesandincludesmanager.h>
0013 
0014 #include <KLocalizedString>
0015 
0016 namespace
0017 {
0018 void openConfigurationPage(const QString& path)
0019 {
0020     KDevelop::IDefinesAndIncludesManager::manager()->openConfigurationDialog(path);
0021 }
0022 }
0023 
0024 class AddCustomIncludePathAction : public KDevelop::IAssistantAction
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     explicit AddCustomIncludePathAction(const KDevelop::IndexedString& path)
0030         : m_path(path)
0031     {}
0032 
0033     QString description() const override
0034     {
0035         return i18n("Add Custom Include Path");
0036     }
0037 
0038     void execute() override
0039     {
0040         openConfigurationPage(m_path.str());
0041         emit executed(this);
0042     }
0043 
0044 private:
0045     KDevelop::IndexedString m_path;
0046 };
0047 
0048 class OpenProjectForFileAssistant : public KDevelop::IAssistantAction
0049 {
0050     Q_OBJECT
0051 public:
0052     explicit OpenProjectForFileAssistant(const KDevelop::IndexedString& path)
0053         : m_path(path)
0054     {}
0055 
0056     QString description() const override
0057     {
0058         return i18n("Open Project");
0059     };
0060 
0061     void execute() override
0062     {
0063         KDevelop::ICore::self()->projectController()->openProjectForUrl(m_path.toUrl());
0064         emit executed(this);
0065     }
0066 
0067 private:
0068     KDevelop::IndexedString m_path;
0069 };
0070 
0071 class MissingIncludePathAssistant : public ClangFixitAssistant
0072 {
0073     Q_OBJECT
0074 public:
0075     MissingIncludePathAssistant(const QString& title, const KDevelop::IndexedString& path)
0076         : ClangFixitAssistant(title, {})
0077         , m_path(path)
0078     {}
0079 
0080     void createActions() override
0081     {
0082         auto project = KDevelop::ICore::self()->projectController()->findProjectForUrl(m_path.toUrl());
0083 
0084         if (!project) {
0085             addAction(KDevelop::IAssistantAction::Ptr(new OpenProjectForFileAssistant(m_path)));
0086         }
0087         addAction(KDevelop::IAssistantAction::Ptr(new AddCustomIncludePathAction(m_path)));
0088     }
0089 
0090 private:
0091     KDevelop::IndexedString m_path;
0092 };
0093 
0094 MissingIncludePathProblem::MissingIncludePathProblem(CXDiagnostic diagnostic, CXTranslationUnit unit)
0095     : ClangProblem(diagnostic, unit)
0096 {}
0097 
0098 KDevelop::IAssistant::Ptr MissingIncludePathProblem::solutionAssistant() const
0099 {
0100     return KDevelop::IAssistant::Ptr(new MissingIncludePathAssistant(description(), finalLocation().document));
0101 }
0102 
0103 #include "missingincludepathproblem.moc"