diff options
| author | Ilya Biryukov <ibiryukov@google.com> | 2019-01-29 14:17:36 +0000 |
|---|---|---|
| committer | Ilya Biryukov <ibiryukov@google.com> | 2019-01-29 14:17:36 +0000 |
| commit | cce67a32cf822bf2a3520e256d92f4f0ba0dc6fe (patch) | |
| tree | b36de7806421ed73ba3dc821aca5f50aa19d539b /clang-tools-extra/clangd/ClangdServer.cpp | |
| parent | 81675c8f3bcfe7ceef7ebaa7e9933bf6a095db4c (diff) | |
| download | bcm5719-llvm-cce67a32cf822bf2a3520e256d92f4f0ba0dc6fe.tar.gz bcm5719-llvm-cce67a32cf822bf2a3520e256d92f4f0ba0dc6fe.zip | |
[clangd] Interfaces for writing code tweaks
Summary:
The code tweaks are an implementation of mini-refactorings exposed
via the LSP code actions. They run in two stages:
- Stage 1. Decides whether the action is available to the user and
collects all the information required to finish the action.
Should be cheap, since this will run over all the actions known to
clangd on each textDocument/codeAction request from the client.
- Stage 2. Uses information from stage 1 to produce the actual edits
that the code action should perform. This stage can be expensive and
will only run if the user chooses to perform the specified action in
the UI.
One unfortunate consequence of this change is increased latency of
processing the textDocument/codeAction requests, which now wait for an
AST. However, we cannot avoid this with what we have available in the LSP
today.
Reviewers: kadircet, ioeric, hokein, sammccall
Reviewed By: sammccall
Subscribers: mgrang, mgorny, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D56267
llvm-svn: 352494
Diffstat (limited to 'clang-tools-extra/clangd/ClangdServer.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/ClangdServer.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index 9b9eb10db0d..7221b382944 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -16,11 +16,13 @@ #include "XRefs.h" #include "index/FileIndex.h" #include "index/Merge.h" +#include "refactor/Tweak.h" #include "clang/Format/Format.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/CompilationDatabase.h" +#include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Refactoring/RefactoringResultConsumer.h" #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" #include "llvm/ADT/ArrayRef.h" @@ -28,10 +30,12 @@ #include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include <future> +#include <memory> #include <mutex> namespace clang { @@ -325,6 +329,56 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName, "Rename", File, Bind(Action, File.str(), NewName.str(), std::move(CB))); } +void ClangdServer::enumerateTweaks(PathRef File, Range Sel, + Callback<std::vector<TweakRef>> CB) { + auto Action = [Sel](decltype(CB) CB, std::string File, + Expected<InputsAndAST> InpAST) { + if (!InpAST) + return CB(InpAST.takeError()); + + auto &AST = InpAST->AST; + auto CursorLoc = sourceLocationInMainFile( + AST.getASTContext().getSourceManager(), Sel.start); + if (!CursorLoc) + return CB(CursorLoc.takeError()); + Tweak::Selection Inputs = {InpAST->Inputs.Contents, InpAST->AST, + *CursorLoc}; + + std::vector<TweakRef> Res; + for (auto &T : prepareTweaks(Inputs)) + Res.push_back({T->id(), T->title()}); + CB(std::move(Res)); + }; + + WorkScheduler.runWithAST("EnumerateTweaks", File, + Bind(Action, std::move(CB), File.str())); +} + +void ClangdServer::applyTweak(PathRef File, Range Sel, TweakID ID, + Callback<tooling::Replacements> CB) { + auto Action = [ID, Sel](decltype(CB) CB, std::string File, + Expected<InputsAndAST> InpAST) { + if (!InpAST) + return CB(InpAST.takeError()); + + auto &AST = InpAST->AST; + auto CursorLoc = sourceLocationInMainFile( + AST.getASTContext().getSourceManager(), Sel.start); + if (!CursorLoc) + return CB(CursorLoc.takeError()); + Tweak::Selection Inputs = {InpAST->Inputs.Contents, InpAST->AST, + *CursorLoc}; + + auto A = prepareTweak(ID, Inputs); + if (!A) + return CB(A.takeError()); + // FIXME: run formatter on top of resulting replacements. + return CB((*A)->apply(Inputs)); + }; + WorkScheduler.runWithAST("ApplyTweak", File, + Bind(Action, std::move(CB), File.str())); +} + void ClangdServer::dumpAST(PathRef File, llvm::unique_function<void(std::string)> Callback) { auto Action = [](decltype(Callback) Callback, |

