[clang-tools-extra] 17747d2 - [clangd] Remove Tweak::Intent, use CodeAction kind directly. NFC
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 2 02:14:46 PDT 2020
Author: Sam McCall
Date: 2020-10-02T11:14:23+02:00
New Revision: 17747d2ec8ec4471748197db54c8703f0c07c91c
URL: https://github.com/llvm/llvm-project/commit/17747d2ec8ec4471748197db54c8703f0c07c91c
DIFF: https://github.com/llvm/llvm-project/commit/17747d2ec8ec4471748197db54c8703f0c07c91c.diff
LOG: [clangd] Remove Tweak::Intent, use CodeAction kind directly. NFC
Intent was a nice idea but it ends up being a bit awkward/heavyweight
without adding much.
In particular, it makes it hard to implement `CodeActionParams.only` properly
(there's an inheritance hierarchy for kinds).
Differential Revision: https://reviews.llvm.org/D88427
Added:
Modified:
clang-tools-extra/clangd/ClangdLSPServer.cpp
clang-tools-extra/clangd/ClangdServer.cpp
clang-tools-extra/clangd/ClangdServer.h
clang-tools-extra/clangd/refactor/Tweak.h
clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index dfd26ad40b89..c2915aeada4f 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -68,14 +68,7 @@ CodeAction toCodeAction(const ClangdServer::TweakRef &T, const URIForFile &File,
Range Selection) {
CodeAction CA;
CA.title = T.Title;
- switch (T.Intent) {
- case Tweak::Refactor:
- CA.kind = std::string(CodeAction::REFACTOR_KIND);
- break;
- case Tweak::Info:
- CA.kind = std::string(CodeAction::INFO_KIND);
- break;
- }
+ CA.kind = T.Kind.str();
// This tweak may have an expensive second stage, we only run it if the user
// actually chooses it in the UI. We reply with a command that would run the
// corresponding tweak.
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index 27d1a2dc7cdc..8c73b6a7d063 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -521,7 +521,7 @@ void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
};
for (const auto &Sel : *Selections) {
for (auto &T : prepareTweaks(*Sel, Filter)) {
- Res.push_back({T->id(), T->title(), T->intent()});
+ Res.push_back({T->id(), T->title(), T->kind()});
PreparedTweaks.insert(T->id());
TweakAvailable.record(1, T->id());
}
diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h
index ae10dba32b58..d801d3cd4353 100644
--- a/clang-tools-extra/clangd/ClangdServer.h
+++ b/clang-tools-extra/clangd/ClangdServer.h
@@ -288,7 +288,7 @@ class ClangdServer {
struct TweakRef {
std::string ID; /// ID to pass for applyTweak.
std::string Title; /// A single-line message to show in the UI.
- Tweak::Intent Intent;
+ llvm::StringLiteral Kind;
};
/// Enumerate the code tweaks available to the user at a specified point.
void enumerateTweaks(PathRef File, Range Sel,
diff --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h
index 10e3e8d3e565..f991b78d8960 100644
--- a/clang-tools-extra/clangd/refactor/Tweak.h
+++ b/clang-tools-extra/clangd/refactor/Tweak.h
@@ -67,13 +67,6 @@ class Tweak {
// FIXME: provide a way to get sources and ASTs for other files.
};
- /// Output of a tweak.
- enum Intent {
- /// Apply changes that preserve the behavior of the code.
- Refactor,
- /// Provide information to the user.
- Info,
- };
struct Effect {
/// A message to be displayed to the user.
llvm::Optional<std::string> ShowMessage;
@@ -120,7 +113,7 @@ class Tweak {
virtual std::string title() const = 0;
/// Describes what kind of action this is.
/// EXPECTS: prepare() was called and returned true.
- virtual Intent intent() const = 0;
+ virtual llvm::StringLiteral kind() const = 0;
/// Is this a 'hidden' tweak, which are off by default.
virtual bool hidden() const { return false; }
};
diff --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index 8c69b64c5aff..fe01894b6386 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -38,7 +38,9 @@ class AddUsing : public Tweak {
bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override;
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
private:
// The qualifier to remove. Set by prepare().
diff --git a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
index 8e3eba35b004..b243f24eb369 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
@@ -28,7 +28,9 @@ class AnnotateHighlightings : public Tweak {
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override { return "Annotate highlighting tokens"; }
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
bool hidden() const override { return true; }
};
REGISTER_TWEAK(AnnotateHighlightings)
diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
index cdd5f9c6595b..02be220e0b6c 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
@@ -394,7 +394,9 @@ class DefineInline : public Tweak {
public:
const char *id() const override final;
- Intent intent() const override { return Intent::Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
std::string title() const override {
return "Move function body to declaration";
}
diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
index ed4d0cc46269..0462090ee25a 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
@@ -356,7 +356,9 @@ class DefineOutline : public Tweak {
const char *id() const override;
bool hidden() const override { return false; }
- Intent intent() const override { return Intent::Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
std::string title() const override {
return "Move function body to out-of-line.";
}
diff --git a/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp b/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
index b2b883d64567..72e7cd5a2527 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
@@ -45,7 +45,7 @@ class DumpAST : public Tweak {
return std::string(
llvm::formatv("Dump {0} AST", Node->getNodeKind().asStringRef()));
}
- Intent intent() const override { return Info; }
+ llvm::StringLiteral kind() const override { return CodeAction::INFO_KIND; }
bool hidden() const override { return true; }
private:
@@ -91,7 +91,7 @@ class ShowSelectionTree : public Tweak {
return Effect::showMessage(llvm::to_string(Inputs.ASTSelection));
}
std::string title() const override { return "Show selection tree"; }
- Intent intent() const override { return Info; }
+ llvm::StringLiteral kind() const override { return CodeAction::INFO_KIND; }
bool hidden() const override { return true; }
};
REGISTER_TWEAK(ShowSelectionTree)
@@ -117,7 +117,7 @@ class DumpSymbol : public Tweak {
return Effect::showMessage(Out.str());
}
std::string title() const override { return "Dump symbol under the cursor"; }
- Intent intent() const override { return Info; }
+ llvm::StringLiteral kind() const override { return CodeAction::INFO_KIND; }
bool hidden() const override { return true; }
};
REGISTER_TWEAK(DumpSymbol)
@@ -153,7 +153,7 @@ class DumpRecordLayout : public Tweak {
"Show {0} layout",
TypeWithKeyword::getTagTypeKindName(Record->getTagKind())));
}
- Intent intent() const override { return Info; }
+ llvm::StringLiteral kind() const override { return CodeAction::INFO_KIND; }
// FIXME: this is interesting to most users. However:
// - triggering is too broad (e.g. triggers on comments within a class)
// - showMessage has inconsistent UX (e.g. newlines are stripped in VSCode)
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index f9db50d934b0..4dfaf729c892 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -37,7 +37,9 @@ namespace {
class ExpandAutoType : public Tweak {
public:
const char *id() const final;
- Intent intent() const override { return Intent::Refactor;}
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
index 59a53f97c49c..a7e2dddf4cba 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
@@ -31,7 +31,9 @@ namespace {
class ExpandMacro : public Tweak {
public:
const char *id() const override final;
- Intent intent() const override { return Intent::Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
bool prepare(const Selection &Inputs) override;
Expected<Tweak::Effect> apply(const Selection &Inputs) override;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
index 6ee5aee37f51..1ba8c3c1d9ff 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
@@ -646,7 +646,9 @@ class ExtractFunction : public Tweak {
bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override { return "Extract to function"; }
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
private:
ExtractionZone ExtZone;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
index 104f8ba63dd0..8b668be5f2f9 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
@@ -438,7 +438,9 @@ class ExtractVariable : public Tweak {
std::string title() const override {
return "Extract subexpression to variable";
}
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
private:
// the expression to extract
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp b/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
index 894f018aa796..0c50db79d367 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
@@ -35,7 +35,9 @@ namespace {
class ObjCLocalizeStringLiteral : public Tweak {
public:
const char *id() const override final;
- Intent intent() const override { return Intent::Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
bool prepare(const Selection &Inputs) override;
Expected<Tweak::Effect> apply(const Selection &Inputs) override;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp b/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
index 753e8b4df826..12a6e49a1684 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
@@ -52,7 +52,9 @@ class PopulateSwitch : public Tweak {
bool prepare(const Selection &Sel) override;
Expected<Effect> apply(const Selection &Sel) override;
std::string title() const override { return "Populate switch"; }
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
private:
const DeclContext *DeclCtx = nullptr;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
index bec45be6c325..b0ab3067449b 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
@@ -41,7 +41,9 @@ class RawStringLiteral : public Tweak {
bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override { return "Convert to raw string"; }
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
private:
const clang::StringLiteral *Str = nullptr;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
index 9d1a9f12567c..8bd9703397b6 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
@@ -39,7 +39,9 @@ class RemoveUsingNamespace : public Tweak {
bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override;
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
private:
const UsingDirectiveDecl *TargetDirective = nullptr;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp b/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
index d5299f014cc7..976f68215581 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
@@ -39,7 +39,9 @@ class SwapIfBranches : public Tweak {
bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override { return "Swap if branches"; }
- Intent intent() const override { return Refactor; }
+ llvm::StringLiteral kind() const override {
+ return CodeAction::REFACTOR_KIND;
+ }
bool hidden() const override { return true; }
private:
More information about the cfe-commits
mailing list