summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/SourceCode.h
diff options
context:
space:
mode:
authorKadir Cetinkaya <kadircet@google.com>2019-09-09 12:28:44 +0000
committerKadir Cetinkaya <kadircet@google.com>2019-09-09 12:28:44 +0000
commit5b270932cc6ee39d977d397bafc363e9c5df040f (patch)
tree5d55df28fdce1d6ad01e26152a66541c4f797316 /clang-tools-extra/clangd/SourceCode.h
parent7c5697c8b24c6a1288f9682ba627182bdfb914f7 (diff)
downloadbcm5719-llvm-5b270932cc6ee39d977d397bafc363e9c5df040f.tar.gz
bcm5719-llvm-5b270932cc6ee39d977d397bafc363e9c5df040f.zip
[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
Diffstat (limited to 'clang-tools-extra/clangd/SourceCode.h')
-rw-r--r--clang-tools-extra/clangd/SourceCode.h28
1 files changed, 27 insertions, 1 deletions
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 <string>
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<tooling::Replacements>
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<std::string> apply() const;
+
+ /// Represents Replacements as TextEdits that are available for use in LSP.
+ std::vector<TextEdit> 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<unsigned> collectIdentifiers(llvm::StringRef Content,
const format::FormatStyle &Style);
OpenPOWER on IntegriCloud