From 5b270932cc6ee39d977d397bafc363e9c5df040f Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Mon, 9 Sep 2019 12:28:44 +0000 Subject: [clangd] Support multifile edits as output of Tweaks Summary: First patch for propogating multifile changes from tweak outputs to LSP WorkspaceEdits. Uses SM to convert tooling::Replacements to TextEdits. Errors out if there are any inconsistencies between the draft version and the version generated the edits. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66637 llvm-svn: 371392 --- clang-tools-extra/clangd/SourceCode.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'clang-tools-extra/clangd/SourceCode.h') diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h index 81b4672b184..53a140fe4ab 100644 --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H + #include "Context.h" #include "Protocol.h" #include "clang/Basic/Diagnostic.h" @@ -22,7 +23,9 @@ #include "clang/Tooling/Core/Replacement.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" +#include "llvm/Support/Error.h" #include "llvm/Support/SHA1.h" +#include namespace clang { class SourceManager; @@ -197,11 +200,34 @@ format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, llvm::vfs::FileSystem *FS); -// Cleanup and format the given replacements. +/// Cleanup and format the given replacements. llvm::Expected cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, const format::FormatStyle &Style); +/// A set of edits generated for a single file. Can verify whether it is safe to +/// apply these edits to a code block. +struct Edit { + tooling::Replacements Replacements; + std::string InitialCode; + + Edit(llvm::StringRef Code, tooling::Replacements Reps) + : Replacements(std::move(Reps)), InitialCode(Code) {} + + /// Returns the file contents after changes are applied. + llvm::Expected apply() const; + + /// Represents Replacements as TextEdits that are available for use in LSP. + std::vector asTextEdits() const; + + /// Checks whether the Replacements are applicable to given Code. + bool canApplyTo(llvm::StringRef Code) const; +}; + +/// Formats the edits and code around it according to Style. Changes +/// Replacements to formatted ones if succeeds. +llvm::Error reformatEdit(Edit &E, const format::FormatStyle &Style); + /// Collects identifiers with counts in the source code. llvm::StringMap collectIdentifiers(llvm::StringRef Content, const format::FormatStyle &Style); -- cgit v1.2.3