diff --git a/src/plugins/clangcodemodel/CMakeLists.txt b/src/plugins/clangcodemodel/CMakeLists.txt index 4176bfd1e9f..51c89160480 100644 --- a/src/plugins/clangcodemodel/CMakeLists.txt +++ b/src/plugins/clangcodemodel/CMakeLists.txt @@ -6,8 +6,8 @@ endif() add_qtc_plugin(ClangCodeModel CONDITION TARGET libclang DEPENDS ClangSupport CPlusPlus - PLUGIN_DEPENDS Core CppTools LanguageClient ${TEST_LINK_DEPENDS} TextEditor - PLUGIN_TEST_DEPENDS CppEditor QmakeProjectManager + PLUGIN_DEPENDS Core CppEditor CppTools LanguageClient ${TEST_LINK_DEPENDS} TextEditor + PLUGIN_TEST_DEPENDS QmakeProjectManager SOURCES clangactivationsequencecontextprocessor.cpp clangactivationsequencecontextprocessor.h clangactivationsequenceprocessor.cpp clangactivationsequenceprocessor.h @@ -24,11 +24,12 @@ add_qtc_plugin(ClangCodeModel clangcompletionchunkstotextconverter.cpp clangcompletionchunkstotextconverter.h clangcompletioncontextanalyzer.cpp clangcompletioncontextanalyzer.h clangconstants.h - clangdclient.cpp clangdclient.h clangcurrentdocumentfilter.cpp clangcurrentdocumentfilter.h + clangdclient.cpp clangdclient.h clangdiagnosticfilter.cpp clangdiagnosticfilter.h clangdiagnosticmanager.cpp clangdiagnosticmanager.h clangdiagnostictooltipwidget.cpp clangdiagnostictooltipwidget.h + clangdquickfixfactory.cpp clangdquickfixfactory.h clangeditordocumentparser.cpp clangeditordocumentparser.h clangeditordocumentprocessor.cpp clangeditordocumentprocessor.h clangfixitoperation.cpp clangfixitoperation.h diff --git a/src/plugins/clangcodemodel/clangcodemodel.pro b/src/plugins/clangcodemodel/clangcodemodel.pro index f6d7b79071d..57eeb22d870 100644 --- a/src/plugins/clangcodemodel/clangcodemodel.pro +++ b/src/plugins/clangcodemodel/clangcodemodel.pro @@ -42,7 +42,8 @@ SOURCES += \ clangutils.cpp \ clangoverviewmodel.cpp \ clangdclient.cpp \ - clanggloballocatorfilters.cpp + clangdquickfixfactory.cpp \ + clanggloballocatorfilters.cpp \ HEADERS += \ clangactivationsequencecontextprocessor.h \ @@ -83,7 +84,8 @@ HEADERS += \ clangutils.h \ clangoverviewmodel.h \ clangdclient.h \ - clanggloballocatorfilters.h + clangdquickfixfactory.h \ + clanggloballocatorfilters.h \ FORMS += clangprojectsettingswidget.ui diff --git a/src/plugins/clangcodemodel/clangcodemodel.qbs b/src/plugins/clangcodemodel/clangcodemodel.qbs index d16c11f7326..18e42a0f04e 100644 --- a/src/plugins/clangcodemodel/clangcodemodel.qbs +++ b/src/plugins/clangcodemodel/clangcodemodel.qbs @@ -13,12 +13,12 @@ QtcPlugin { Depends { name: "Utils" } Depends { name: "ClangSupport" } Depends { name: "LanguageClient" } + Depends { name: "CppEditor" } Depends { name: "libclang"; required: false } Depends { name: "clang_defines" } pluginTestDepends: [ - "CppEditor", "QmakeProjectManager", ] @@ -64,6 +64,8 @@ QtcPlugin { "clangdiagnosticmanager.h", "clangdiagnostictooltipwidget.cpp", "clangdiagnostictooltipwidget.h", + "clangdquickfixfactory.cpp", + "clangdquickfixfactory.h", "clangeditordocumentparser.cpp", "clangeditordocumentparser.h", "clangeditordocumentprocessor.cpp", diff --git a/src/plugins/clangcodemodel/clangcodemodel_dependencies.pri b/src/plugins/clangcodemodel/clangcodemodel_dependencies.pri index f32eaa4c4f4..f333b4dda14 100644 --- a/src/plugins/clangcodemodel/clangcodemodel_dependencies.pri +++ b/src/plugins/clangcodemodel/clangcodemodel_dependencies.pri @@ -4,11 +4,11 @@ QTC_LIB_DEPENDS += \ clangsupport QTC_PLUGIN_DEPENDS += \ coreplugin \ + cppeditor \ cpptools \ languageclient \ texteditor QTC_TEST_DEPENDS += \ - cppeditor \ qmakeprojectmanager equals(TEST, 1): QTC_PLUGIN_DEPENDS += qtsupport diff --git a/src/plugins/clangcodemodel/clangdquickfixfactory.cpp b/src/plugins/clangcodemodel/clangdquickfixfactory.cpp new file mode 100644 index 00000000000..5146df8bc5b --- /dev/null +++ b/src/plugins/clangcodemodel/clangdquickfixfactory.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "clangdquickfixfactory.h" + +#include "clangdclient.h" +#include "clangmodelmanagersupport.h" + +#include + +using namespace LanguageServerProtocol; + +namespace ClangCodeModel { +namespace Internal { + +ClangdQuickFixFactory::ClangdQuickFixFactory() = default; + +void ClangdQuickFixFactory::match(const CppEditor::Internal::CppQuickFixInterface &interface, + QuickFixOperations &result) +{ + const auto client = ClangModelManagerSupport::instance()->clientForFile(interface.filePath()); + if (!client) + return; + + const auto uri = DocumentUri::fromFilePath(interface.filePath()); + QTextCursor cursor(interface.textDocument()); + cursor.setPosition(interface.position()); + cursor.select(QTextCursor::LineUnderCursor); + const QList &diagnostics = client->diagnosticsAt(uri, cursor); + for (const Diagnostic &diagnostic : diagnostics) { + ClangdDiagnostic clangdDiagnostic(diagnostic); + if (const auto actions = clangdDiagnostic.codeActions()) { + for (const CodeAction &action : *actions) + result << new LanguageClient::CodeActionQuickFixOperation(action, client); + } + } +} + +} // namespace Internal +} // namespace ClangCodeModel diff --git a/src/plugins/clangcodemodel/clangdquickfixfactory.h b/src/plugins/clangcodemodel/clangdquickfixfactory.h new file mode 100644 index 00000000000..022b6033407 --- /dev/null +++ b/src/plugins/clangcodemodel/clangdquickfixfactory.h @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include + +namespace ClangCodeModel { +namespace Internal { + +class ClangdQuickFixFactory : public CppEditor::CppQuickFixFactory +{ +public: + ClangdQuickFixFactory(); + + void match(const CppEditor::Internal::CppQuickFixInterface &interface, + QuickFixOperations &result) override; +}; + +} // namespace Internal +} // namespace ClangCodeModel diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index 06746bbb840..569cf792c88 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -26,16 +26,17 @@ #include "clangmodelmanagersupport.h" #include "clangconstants.h" +#include "clangcurrentdocumentfilter.h" #include "clangdclient.h" +#include "clangdquickfixfactory.h" #include "clangeditordocumentprocessor.h" -#include "clangutils.h" #include "clangfollowsymbol.h" +#include "clanggloballocatorfilters.h" #include "clanghoverhandler.h" +#include "clangoverviewmodel.h" #include "clangprojectsettings.h" #include "clangrefactoringengine.h" -#include "clangcurrentdocumentfilter.h" -#include "clanggloballocatorfilters.h" -#include "clangoverviewmodel.h" +#include "clangutils.h" #include #include @@ -128,6 +129,7 @@ ClangModelManagerSupport::ClangModelManagerSupport() // TODO: Enable this once we do document-level stuff with clangd (highlighting etc) // createClient(nullptr, {}); m_generatorSynchronizer.setCancelOnWait(true); + new ClangdQuickFixFactory(); // memory managed by CppEditor::g_cppQuickFixFactories } ClangModelManagerSupport::~ClangModelManagerSupport()