1 //===--- Rename.h - Symbol-rename refactorings -------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_RENAME_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_RENAME_H
11 
12 #include "Protocol.h"
13 #include "SourceCode.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "llvm/Support/Error.h"
16 
17 namespace clang {
18 namespace clangd {
19 class ParsedAST;
20 class SymbolIndex;
21 
22 struct RenameOptions {
23   /// The maximum number of affected files (0 means no limit), only meaningful
24   /// when AllowCrossFile = true.
25   /// If the actual number exceeds the limit, rename is forbidden.
26   size_t LimitFiles = 50;
27   /// If true, format the rename edits, only meaningful in ClangdServer layer.
28   bool WantFormat = false;
29 };
30 
31 struct RenameInputs {
32   Position Pos; // the position triggering the rename
33   llvm::StringRef NewName;
34 
35   ParsedAST *
36   llvm::StringRef MainFilePath;
37 
38   // The filesystem to query when performing cross file renames.
39   // If this is set, Index must also be set, likewise if this is nullptr, Index
40   // must also be nullptr.
41   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr;
42 
43   const SymbolIndex *Index = nullptr;
44 
45   RenameOptions Opts = {};
46 };
47 
48 struct RenameResult {
49   // The range of the symbol that the user can attempt to rename.
50   Range Target;
51   // Rename occurrences for the current main file.
52   std::vector<Range> LocalChanges;
53   // Complete edits for the rename, including LocalChanges.
54   // If the full set of changes is unknown, this field is empty.
55   FileEdits GlobalChanges;
56 };
57 
58 /// Renames all occurrences of the symbol. The result edits are unformatted.
59 /// If AllowCrossFile is false, returns an error if rename a symbol that's used
60 /// in another file (per the index).
61 llvm::Expected<RenameResult> rename(const RenameInputs &RInputs);
62 
63 /// Generates rename edits that replaces all given occurrences with the
64 /// NewName.
65 /// Exposed for testing only.
66 /// REQUIRED: Occurrences is sorted and doesn't have duplicated ranges.
67 llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
68                                      llvm::StringRef InitialCode,
69                                      std::vector<Range> Occurrences,
70                                      llvm::StringRef NewName);
71 
72 /// Adjusts indexed occurrences to match the current state of the file.
73 ///
74 /// The Index is not always up to date. Blindly editing at the locations
75 /// reported by the index may mangle the code in such cases.
76 /// This function determines whether the indexed occurrences can be applied to
77 /// this file, and heuristically repairs the occurrences if necessary.
78 ///
79 /// The API assumes that Indexed contains only named occurrences (each
80 /// occurrence has the same length).
81 /// REQUIRED: Indexed is sorted.
82 llvm::Optional<std::vector<Range>>
83 adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier,
84                    std::vector<Range> Indexed, const LangOptions &LangOpts);
85 
86 /// Calculates the lexed occurrences that the given indexed occurrences map to.
87 /// Returns None if we don't find a mapping.
88 ///
89 /// Exposed for testing only.
90 ///
91 /// REQUIRED: Indexed and Lexed are sorted.
92 llvm::Optional<std::vector<Range>> getMappedRanges(ArrayRef<Range> Indexed,
93                                                    ArrayRef<Range> Lexed);
94 /// Evaluates how good the mapped result is. 0 indicates a perfect match.
95 ///
96 /// Exposed for testing only.
97 ///
98 /// REQUIRED: Indexed and Lexed are sorted, Indexed and MappedIndex have the
99 /// same size.
100 size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed, ArrayRef<Range> Lexed,
101                                  ArrayRef<size_t> MappedIndex);
102 
103 } // namespace clangd
104 } // namespace clang
105 
106 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_RENAME_H
107