Clangd: add quick fix factory

Change-Id: Idea71364b0e3f528e9dd366d46a2797f5056dfed
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2021-06-22 14:40:19 +02:00
parent 1d39465a1b
commit 813c4b807a
7 changed files with 123 additions and 11 deletions

View File

@@ -6,8 +6,8 @@ endif()
add_qtc_plugin(ClangCodeModel add_qtc_plugin(ClangCodeModel
CONDITION TARGET libclang CONDITION TARGET libclang
DEPENDS ClangSupport CPlusPlus DEPENDS ClangSupport CPlusPlus
PLUGIN_DEPENDS Core CppTools LanguageClient ${TEST_LINK_DEPENDS} TextEditor PLUGIN_DEPENDS Core CppEditor CppTools LanguageClient ${TEST_LINK_DEPENDS} TextEditor
PLUGIN_TEST_DEPENDS CppEditor QmakeProjectManager PLUGIN_TEST_DEPENDS QmakeProjectManager
SOURCES SOURCES
clangactivationsequencecontextprocessor.cpp clangactivationsequencecontextprocessor.h clangactivationsequencecontextprocessor.cpp clangactivationsequencecontextprocessor.h
clangactivationsequenceprocessor.cpp clangactivationsequenceprocessor.h clangactivationsequenceprocessor.cpp clangactivationsequenceprocessor.h
@@ -24,11 +24,12 @@ add_qtc_plugin(ClangCodeModel
clangcompletionchunkstotextconverter.cpp clangcompletionchunkstotextconverter.h clangcompletionchunkstotextconverter.cpp clangcompletionchunkstotextconverter.h
clangcompletioncontextanalyzer.cpp clangcompletioncontextanalyzer.h clangcompletioncontextanalyzer.cpp clangcompletioncontextanalyzer.h
clangconstants.h clangconstants.h
clangdclient.cpp clangdclient.h
clangcurrentdocumentfilter.cpp clangcurrentdocumentfilter.h clangcurrentdocumentfilter.cpp clangcurrentdocumentfilter.h
clangdclient.cpp clangdclient.h
clangdiagnosticfilter.cpp clangdiagnosticfilter.h clangdiagnosticfilter.cpp clangdiagnosticfilter.h
clangdiagnosticmanager.cpp clangdiagnosticmanager.h clangdiagnosticmanager.cpp clangdiagnosticmanager.h
clangdiagnostictooltipwidget.cpp clangdiagnostictooltipwidget.h clangdiagnostictooltipwidget.cpp clangdiagnostictooltipwidget.h
clangdquickfixfactory.cpp clangdquickfixfactory.h
clangeditordocumentparser.cpp clangeditordocumentparser.h clangeditordocumentparser.cpp clangeditordocumentparser.h
clangeditordocumentprocessor.cpp clangeditordocumentprocessor.h clangeditordocumentprocessor.cpp clangeditordocumentprocessor.h
clangfixitoperation.cpp clangfixitoperation.h clangfixitoperation.cpp clangfixitoperation.h

View File

@@ -42,7 +42,8 @@ SOURCES += \
clangutils.cpp \ clangutils.cpp \
clangoverviewmodel.cpp \ clangoverviewmodel.cpp \
clangdclient.cpp \ clangdclient.cpp \
clanggloballocatorfilters.cpp clangdquickfixfactory.cpp \
clanggloballocatorfilters.cpp \
HEADERS += \ HEADERS += \
clangactivationsequencecontextprocessor.h \ clangactivationsequencecontextprocessor.h \
@@ -83,7 +84,8 @@ HEADERS += \
clangutils.h \ clangutils.h \
clangoverviewmodel.h \ clangoverviewmodel.h \
clangdclient.h \ clangdclient.h \
clanggloballocatorfilters.h clangdquickfixfactory.h \
clanggloballocatorfilters.h \
FORMS += clangprojectsettingswidget.ui FORMS += clangprojectsettingswidget.ui

View File

@@ -13,12 +13,12 @@ QtcPlugin {
Depends { name: "Utils" } Depends { name: "Utils" }
Depends { name: "ClangSupport" } Depends { name: "ClangSupport" }
Depends { name: "LanguageClient" } Depends { name: "LanguageClient" }
Depends { name: "CppEditor" }
Depends { name: "libclang"; required: false } Depends { name: "libclang"; required: false }
Depends { name: "clang_defines" } Depends { name: "clang_defines" }
pluginTestDepends: [ pluginTestDepends: [
"CppEditor",
"QmakeProjectManager", "QmakeProjectManager",
] ]
@@ -64,6 +64,8 @@ QtcPlugin {
"clangdiagnosticmanager.h", "clangdiagnosticmanager.h",
"clangdiagnostictooltipwidget.cpp", "clangdiagnostictooltipwidget.cpp",
"clangdiagnostictooltipwidget.h", "clangdiagnostictooltipwidget.h",
"clangdquickfixfactory.cpp",
"clangdquickfixfactory.h",
"clangeditordocumentparser.cpp", "clangeditordocumentparser.cpp",
"clangeditordocumentparser.h", "clangeditordocumentparser.h",
"clangeditordocumentprocessor.cpp", "clangeditordocumentprocessor.cpp",

View File

@@ -4,11 +4,11 @@ QTC_LIB_DEPENDS += \
clangsupport clangsupport
QTC_PLUGIN_DEPENDS += \ QTC_PLUGIN_DEPENDS += \
coreplugin \ coreplugin \
cppeditor \
cpptools \ cpptools \
languageclient \ languageclient \
texteditor texteditor
QTC_TEST_DEPENDS += \ QTC_TEST_DEPENDS += \
cppeditor \
qmakeprojectmanager qmakeprojectmanager
equals(TEST, 1): QTC_PLUGIN_DEPENDS += qtsupport equals(TEST, 1): QTC_PLUGIN_DEPENDS += qtsupport

View File

@@ -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 <languageclient/languageclientquickfix.h>
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<Diagnostic> &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

View File

@@ -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 <cppeditor/cppquickfix.h>
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

View File

@@ -26,16 +26,17 @@
#include "clangmodelmanagersupport.h" #include "clangmodelmanagersupport.h"
#include "clangconstants.h" #include "clangconstants.h"
#include "clangcurrentdocumentfilter.h"
#include "clangdclient.h" #include "clangdclient.h"
#include "clangdquickfixfactory.h"
#include "clangeditordocumentprocessor.h" #include "clangeditordocumentprocessor.h"
#include "clangutils.h"
#include "clangfollowsymbol.h" #include "clangfollowsymbol.h"
#include "clanggloballocatorfilters.h"
#include "clanghoverhandler.h" #include "clanghoverhandler.h"
#include "clangoverviewmodel.h"
#include "clangprojectsettings.h" #include "clangprojectsettings.h"
#include "clangrefactoringengine.h" #include "clangrefactoringengine.h"
#include "clangcurrentdocumentfilter.h" #include "clangutils.h"
#include "clanggloballocatorfilters.h"
#include "clangoverviewmodel.h"
#include <coreplugin/editormanager/documentmodel.h> #include <coreplugin/editormanager/documentmodel.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
@@ -128,6 +129,7 @@ ClangModelManagerSupport::ClangModelManagerSupport()
// TODO: Enable this once we do document-level stuff with clangd (highlighting etc) // TODO: Enable this once we do document-level stuff with clangd (highlighting etc)
// createClient(nullptr, {}); // createClient(nullptr, {});
m_generatorSynchronizer.setCancelOnWait(true); m_generatorSynchronizer.setCancelOnWait(true);
new ClangdQuickFixFactory(); // memory managed by CppEditor::g_cppQuickFixFactories
} }
ClangModelManagerSupport::~ClangModelManagerSupport() ClangModelManagerSupport::~ClangModelManagerSupport()