[clangd] Move helpers that convert Replacements to TextEdits to SourceCode.h

Reviewers: ilya-biryukov

Reviewed By: ilya-biryukov

Subscribers: klimek, MaskRay, jkorous, cfe-commits

Differential Revision: https://reviews.llvm.org/D46670

llvm-svn: 332089
This commit is contained in:
Eric Liu 2018-05-11 12:12:08 +00:00
parent e7306b105e
commit 9133ecdc8e
3 changed files with 26 additions and 27 deletions

View File

@ -60,32 +60,6 @@ public:
static URISchemeRegistry::Add<TestScheme>
X("test", "Test scheme for clangd lit tests.");
TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R) {
Range ReplacementRange = {
offsetToPosition(Code, R.getOffset()),
offsetToPosition(Code, R.getOffset() + R.getLength())};
return {ReplacementRange, R.getReplacementText()};
}
std::vector<TextEdit>
replacementsToEdits(StringRef Code,
const std::vector<tooling::Replacement> &Replacements) {
// Turn the replacements into the format specified by the Language Server
// Protocol. Fuse them into one big JSON array.
std::vector<TextEdit> Edits;
for (const auto &R : Replacements)
Edits.push_back(replacementToEdit(Code, R));
return Edits;
}
std::vector<TextEdit> replacementsToEdits(StringRef Code,
const tooling::Replacements &Repls) {
std::vector<TextEdit> Edits;
for (const auto &R : Repls)
Edits.push_back(replacementToEdit(Code, R));
return Edits;
}
SymbolKindBitset defaultSymbolKinds() {
SymbolKindBitset Defaults;
for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array);
@ -291,7 +265,11 @@ void ClangdLSPServer::onRename(RenameParams &Params) {
return replyError(ErrorCode::InternalError,
llvm::toString(Replacements.takeError()));
std::vector<TextEdit> Edits = replacementsToEdits(*Code, *Replacements);
// Turn the replacements into the format specified by the Language
// Server Protocol. Fuse them into one big JSON array.
std::vector<TextEdit> Edits;
for (const auto &R : *Replacements)
Edits.push_back(replacementToEdit(*Code, R));
WorkspaceEdit WE;
WE.changes = {{Params.textDocument.uri.uri(), Edits}};
reply(WE);

View File

@ -166,5 +166,20 @@ splitQualifiedName(llvm::StringRef QName) {
return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
}
TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R) {
Range ReplacementRange = {
offsetToPosition(Code, R.getOffset()),
offsetToPosition(Code, R.getOffset() + R.getLength())};
return {ReplacementRange, R.getReplacementText()};
}
std::vector<TextEdit> replacementsToEdits(StringRef Code,
const tooling::Replacements &Repls) {
std::vector<TextEdit> Edits;
for (const auto &R : Repls)
Edits.push_back(replacementToEdit(Code, R));
return Edits;
}
} // namespace clangd
} // namespace clang

View File

@ -15,6 +15,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
#include "Protocol.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Tooling/Core/Replacement.h"
namespace clang {
class SourceManager;
@ -55,6 +56,11 @@ std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
std::pair<llvm::StringRef, llvm::StringRef>
splitQualifiedName(llvm::StringRef QName);
TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R);
std::vector<TextEdit> replacementsToEdits(StringRef Code,
const tooling::Replacements &Repls);
} // namespace clangd
} // namespace clang
#endif