diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -367,7 +367,7 @@ auto End = positionToOffset(AST.Inputs.Contents, Sel.end); if (!End) return End.takeError(); - return Tweak::Selection(AST.AST, *Begin, *End); + return Tweak::Selection(AST.AST, *Begin, *End, AST.Inputs.Index); } void ClangdServer::enumerateTweaks(PathRef File, Range Sel, diff --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h --- a/clang-tools-extra/clangd/refactor/Tweak.h +++ b/clang-tools-extra/clangd/refactor/Tweak.h @@ -24,6 +24,7 @@ #include "Protocol.h" #include "Selection.h" #include "SourceCode.h" +#include "index/Index.h" #include "clang/Tooling/Core/Replacement.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringMap.h" @@ -46,7 +47,8 @@ public: /// Input to prepare and apply tweaks. struct Selection { - Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd); + Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd, + const SymbolIndex *Index); /// The text of the active document. llvm::StringRef Code; /// Parsed AST of the active file. @@ -60,6 +62,8 @@ unsigned SelectionEnd; /// The AST nodes that were selected. SelectionTree ASTSelection; + /// The Index being used by ClangdServer. + const SymbolIndex *Index = nullptr; // FIXME: provide a way to get sources and ASTs for other files. }; diff --git a/clang-tools-extra/clangd/refactor/Tweak.cpp b/clang-tools-extra/clangd/refactor/Tweak.cpp --- a/clang-tools-extra/clangd/refactor/Tweak.cpp +++ b/clang-tools-extra/clangd/refactor/Tweak.cpp @@ -9,6 +9,7 @@ #include "Logger.h" #include "Path.h" #include "SourceCode.h" +#include "index/Index.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" @@ -45,9 +46,10 @@ } // namespace Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin, - unsigned RangeEnd) + unsigned RangeEnd, const SymbolIndex *Index) : AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd), - ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) { + ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd), + Index(Index) { auto &SM = AST.getSourceManager(); Code = SM.getBufferData(SM.getMainFileID()); Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin); diff --git a/clang-tools-extra/clangd/unittests/TweakTesting.h b/clang-tools-extra/clangd/unittests/TweakTesting.h --- a/clang-tools-extra/clangd/unittests/TweakTesting.h +++ b/clang-tools-extra/clangd/unittests/TweakTesting.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TWEAKTESTING_H #include "TestTU.h" +#include "index/Index.h" #include "llvm/ADT/StringMap.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -66,6 +67,9 @@ // Context in which snippets of code should be placed to run tweaks. CodeContext Context = File; + // Index to be passed into Tweak::Selection. + const SymbolIndex *Index = nullptr; + // Apply the current tweak to the range (or point) in MarkedCode. // MarkedCode will be wrapped according to the Context. // - if the tweak produces edits, returns the edited code (without markings) diff --git a/clang-tools-extra/clangd/unittests/TweakTesting.cpp b/clang-tools-extra/clangd/unittests/TweakTesting.cpp --- a/clang-tools-extra/clangd/unittests/TweakTesting.cpp +++ b/clang-tools-extra/clangd/unittests/TweakTesting.cpp @@ -14,6 +14,7 @@ #include "refactor/Tweak.h" #include "clang/Tooling/Core/Replacement.h" #include "llvm/Support/Error.h" +#include "gmock/gmock.h" #include namespace clang { @@ -61,7 +62,7 @@ cantFail(positionToOffset(A.code(), SelectionRng.end))}; } -MATCHER_P4(TweakIsAvailable, TweakID, Ctx, Header, ExtraFiles, +MATCHER_P5(TweakIsAvailable, TweakID, Ctx, Header, ExtraFiles, Index, (TweakID + (negation ? " is unavailable" : " is available")).str()) { std::string WrappedCode = wrap(Ctx, arg); Annotations Input(WrappedCode); @@ -71,7 +72,7 @@ TU.Code = Input.code(); TU.AdditionalFiles = std::move(ExtraFiles); ParsedAST AST = TU.build(); - Tweak::Selection S(AST, Selection.first, Selection.second); + Tweak::Selection S(AST, Selection.first, Selection.second, Index); auto PrepareResult = prepareTweak(TweakID, S); if (PrepareResult) return true; @@ -93,7 +94,7 @@ TU.Code = Input.code(); TU.ExtraArgs = ExtraArgs; ParsedAST AST = TU.build(); - Tweak::Selection S(AST, Selection.first, Selection.second); + Tweak::Selection S(AST, Selection.first, Selection.second, Index); auto T = prepareTweak(TweakID, S); if (!T) { @@ -123,8 +124,8 @@ } ::testing::Matcher TweakTest::isAvailable() const { - return TweakIsAvailable(llvm::StringRef(TweakID), Context, Header, - ExtraFiles); + return TweakIsAvailable(llvm::StringRef(TweakID), Context, Header, ExtraFiles, + Index); } std::vector TweakTest::expandCases(llvm::StringRef MarkedCode) {