kadircet created this revision. Herald added subscribers: usaxena95, arphaman, mgorny. kadircet requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang.
Plugins can be used to augment clangd's behaviours in various features. Initially it only supports generating extra code actions for diagnostics. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D96244 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/Plugin.cpp clang-tools-extra/clangd/Plugin.h Index: clang-tools-extra/clangd/Plugin.h =================================================================== --- /dev/null +++ clang-tools-extra/clangd/Plugin.h @@ -0,0 +1,38 @@ +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PLUGIN_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PLUGIN_H +#include "Diagnostics.h" +#include "support/Path.h" +#include "clang/Basic/Diagnostic.h" +#include "llvm/ADT/None.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Registry.h" +#include <vector> + +namespace clang { +namespace clangd { + +/// Plugins enable augmenting clangd's behaviour in various places. +class Plugin { +public: + virtual ~Plugin() = default; + + /// Identifier for the plugin, should be unique. + virtual llvm::StringLiteral id() = 0; + + /// Returns an action that can be invoked to fix the diagnostic. + virtual llvm::Optional<CodeAction> + actOnDiagnostic(DiagnosticsEngine::Level L, const clang::Diagnostic &Diag) { + return llvm::None; + } + + /// Convenience helper to collect fixes from all the plugins. + static std::vector<CodeAction> collectFixes(DiagnosticsEngine::Level L, + const clang::Diagnostic &Diag); +}; +using PluginRegistry = llvm::Registry<Plugin>; + +} // namespace clangd +} // namespace clang +#endif Index: clang-tools-extra/clangd/Plugin.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clangd/Plugin.cpp @@ -0,0 +1,24 @@ +#include "Plugin.h" +#include "Diagnostics.h" +#include "Protocol.h" +#include "clang/Basic/Diagnostic.h" +#include "llvm/Support/Registry.h" +#include <utility> +#include <vector> + +namespace clang { +namespace clangd { + +std::vector<CodeAction> Plugin::collectFixes(DiagnosticsEngine::Level L, + const clang::Diagnostic &Diag) { + std::vector<CodeAction> Actions; + for (const auto &Entry : PluginRegistry::entries()) { + if (auto Action = Entry.instantiate()->actOnDiagnostic(L, Diag)) + Actions.emplace_back(std::move(*Action)); + } + return Actions; +} +} // namespace clangd +} // namespace clang + +LLVM_INSTANTIATE_REGISTRY(clang::clangd::PluginRegistry); Index: clang-tools-extra/clangd/CMakeLists.txt =================================================================== --- clang-tools-extra/clangd/CMakeLists.txt +++ clang-tools-extra/clangd/CMakeLists.txt @@ -75,6 +75,7 @@ IncludeFixer.cpp JSONTransport.cpp PathMapping.cpp + Plugin.cpp Protocol.cpp Quality.cpp ParsedAST.cpp
Index: clang-tools-extra/clangd/Plugin.h =================================================================== --- /dev/null +++ clang-tools-extra/clangd/Plugin.h @@ -0,0 +1,38 @@ +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PLUGIN_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PLUGIN_H +#include "Diagnostics.h" +#include "support/Path.h" +#include "clang/Basic/Diagnostic.h" +#include "llvm/ADT/None.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Registry.h" +#include <vector> + +namespace clang { +namespace clangd { + +/// Plugins enable augmenting clangd's behaviour in various places. +class Plugin { +public: + virtual ~Plugin() = default; + + /// Identifier for the plugin, should be unique. + virtual llvm::StringLiteral id() = 0; + + /// Returns an action that can be invoked to fix the diagnostic. + virtual llvm::Optional<CodeAction> + actOnDiagnostic(DiagnosticsEngine::Level L, const clang::Diagnostic &Diag) { + return llvm::None; + } + + /// Convenience helper to collect fixes from all the plugins. + static std::vector<CodeAction> collectFixes(DiagnosticsEngine::Level L, + const clang::Diagnostic &Diag); +}; +using PluginRegistry = llvm::Registry<Plugin>; + +} // namespace clangd +} // namespace clang +#endif Index: clang-tools-extra/clangd/Plugin.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clangd/Plugin.cpp @@ -0,0 +1,24 @@ +#include "Plugin.h" +#include "Diagnostics.h" +#include "Protocol.h" +#include "clang/Basic/Diagnostic.h" +#include "llvm/Support/Registry.h" +#include <utility> +#include <vector> + +namespace clang { +namespace clangd { + +std::vector<CodeAction> Plugin::collectFixes(DiagnosticsEngine::Level L, + const clang::Diagnostic &Diag) { + std::vector<CodeAction> Actions; + for (const auto &Entry : PluginRegistry::entries()) { + if (auto Action = Entry.instantiate()->actOnDiagnostic(L, Diag)) + Actions.emplace_back(std::move(*Action)); + } + return Actions; +} +} // namespace clangd +} // namespace clang + +LLVM_INSTANTIATE_REGISTRY(clang::clangd::PluginRegistry); Index: clang-tools-extra/clangd/CMakeLists.txt =================================================================== --- clang-tools-extra/clangd/CMakeLists.txt +++ clang-tools-extra/clangd/CMakeLists.txt @@ -75,6 +75,7 @@ IncludeFixer.cpp JSONTransport.cpp PathMapping.cpp + Plugin.cpp Protocol.cpp Quality.cpp ParsedAST.cpp
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits