forked from qt-creator/qt-creator
Clang: Add button to generate compile_commands.json
Change-Id: Iaabdcfc8d1b3463c3f6e5ce47536f9c52556eac0 Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
@@ -27,27 +27,33 @@
|
||||
|
||||
#include "clangconstants.h"
|
||||
#include "clangprojectsettingswidget.h"
|
||||
#include "clangutils.h"
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
# include "test/clangbatchfileprocessor.h"
|
||||
# include "test/clangcodecompletion_test.h"
|
||||
#endif
|
||||
|
||||
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||
#include <coreplugin/actionmanager/actionmanager.h>
|
||||
|
||||
#include <cpptools/cppmodelmanager.h>
|
||||
|
||||
#include <projectexplorer/buildconfiguration.h>
|
||||
#include <projectexplorer/projectpanelfactory.h>
|
||||
#include <projectexplorer/projectexplorer.h>
|
||||
#include <projectexplorer/session.h>
|
||||
#include <projectexplorer/target.h>
|
||||
#include <projectexplorer/taskhub.h>
|
||||
|
||||
#include <texteditor/textmark.h>
|
||||
|
||||
#include <QtConcurrent>
|
||||
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
namespace {
|
||||
|
||||
void addProjectPanelWidget()
|
||||
static void addProjectPanelWidget()
|
||||
{
|
||||
auto panelFactory = new ProjectExplorer::ProjectPanelFactory();
|
||||
panelFactory->setPriority(60);
|
||||
@@ -58,7 +64,32 @@ void addProjectPanelWidget()
|
||||
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
void ClangCodeModelPlugin::generateCompilationDB() {
|
||||
using namespace CppTools;
|
||||
|
||||
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
|
||||
if (!project)
|
||||
return;
|
||||
|
||||
m_generatorWatcher.setFuture(QtConcurrent::run(
|
||||
&Utils::generateCompilationDB,
|
||||
project->projectDirectory(),
|
||||
CppModelManager::instance()->projectInfo(project)));
|
||||
}
|
||||
|
||||
static bool isDBGenerationEnabled(ProjectExplorer::Project *project)
|
||||
{
|
||||
using namespace CppTools;
|
||||
if (!project)
|
||||
return false;
|
||||
ProjectInfo projectInfo = CppModelManager::instance()->projectInfo(project);
|
||||
return projectInfo.isValid() && !projectInfo.projectParts().isEmpty();
|
||||
}
|
||||
|
||||
ClangCodeModelPlugin::~ClangCodeModelPlugin()
|
||||
{
|
||||
m_generatorWatcher.waitForFinished();
|
||||
}
|
||||
|
||||
bool ClangCodeModelPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
||||
{
|
||||
@@ -77,9 +108,69 @@ bool ClangCodeModelPlugin::initialize(const QStringList &arguments, QString *err
|
||||
|
||||
addProjectPanelWidget();
|
||||
|
||||
createCompilationDBButton();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ClangCodeModelPlugin::createCompilationDBButton()
|
||||
{
|
||||
Core::ActionContainer *mbuild =
|
||||
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
|
||||
// generate compile_commands.json
|
||||
m_generateCompilationDBAction = new ::Utils::ParameterAction(
|
||||
tr("Generate compilation database"),
|
||||
tr("Generate compilation database for \"%1\""),
|
||||
::Utils::ParameterAction::AlwaysEnabled, this);
|
||||
|
||||
ProjectExplorer::Project *startupProject = ProjectExplorer::SessionManager::startupProject();
|
||||
m_generateCompilationDBAction->setEnabled(isDBGenerationEnabled(startupProject));
|
||||
if (startupProject)
|
||||
m_generateCompilationDBAction->setParameter(startupProject->displayName());
|
||||
|
||||
Core::Command *command = Core::ActionManager::registerAction(m_generateCompilationDBAction,
|
||||
Constants::GENERATE_COMPILATION_DB);
|
||||
command->setAttribute(Core::Command::CA_UpdateText);
|
||||
command->setDescription(m_generateCompilationDBAction->text());
|
||||
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
|
||||
|
||||
connect(&m_generatorWatcher, &QFutureWatcher<void>::finished, this, [this] () {
|
||||
m_generateCompilationDBAction->setEnabled(
|
||||
isDBGenerationEnabled(ProjectExplorer::SessionManager::startupProject()));
|
||||
});
|
||||
connect(m_generateCompilationDBAction, &QAction::triggered, this, [this] {
|
||||
if (!m_generateCompilationDBAction->isEnabled())
|
||||
return;
|
||||
|
||||
m_generateCompilationDBAction->setEnabled(false);
|
||||
generateCompilationDB();
|
||||
});
|
||||
connect(CppTools::CppModelManager::instance(), &CppTools::CppModelManager::projectPartsUpdated,
|
||||
this, [this](ProjectExplorer::Project *project) {
|
||||
if (project != ProjectExplorer::SessionManager::startupProject())
|
||||
return;
|
||||
m_generateCompilationDBAction->setParameter(project->displayName());
|
||||
if (!m_generatorWatcher.isRunning())
|
||||
m_generateCompilationDBAction->setEnabled(isDBGenerationEnabled(project));
|
||||
});
|
||||
connect(ProjectExplorer::SessionManager::instance(),
|
||||
&ProjectExplorer::SessionManager::startupProjectChanged,
|
||||
this,
|
||||
[this](ProjectExplorer::Project *project) {
|
||||
m_generateCompilationDBAction->setParameter(project->displayName());
|
||||
if (!m_generatorWatcher.isRunning())
|
||||
m_generateCompilationDBAction->setEnabled(isDBGenerationEnabled(project));
|
||||
});
|
||||
connect(ProjectExplorer::SessionManager::instance(),
|
||||
&ProjectExplorer::SessionManager::projectDisplayNameChanged,
|
||||
this,
|
||||
[this](ProjectExplorer::Project *project) {
|
||||
if (project != ProjectExplorer::SessionManager::startupProject())
|
||||
return;
|
||||
m_generateCompilationDBAction->setParameter(project->displayName());
|
||||
});
|
||||
}
|
||||
|
||||
void ClangCodeModelPlugin::extensionsInitialized()
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user