clang-tools  16.0.0git
Rename.h
Go to the documentation of this file.
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  /// Allow rename of virtual method hierarchies.
30  /// Disable to support broken index implementations with missing relations.
31  /// FIXME: fix those implementations and remove this option.
32  bool RenameVirtual = true;
33 };
34 
35 struct RenameInputs {
36  Position Pos; // the position triggering the rename
37  llvm::StringRef NewName;
38 
40  llvm::StringRef MainFilePath;
41 
42  // The filesystem to query when performing cross file renames.
43  // If this is set, Index must also be set, likewise if this is nullptr, Index
44  // must also be nullptr.
45  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr;
46 
47  const SymbolIndex *Index = nullptr;
48 
50 };
51 
52 struct RenameResult {
53  // The range of the symbol that the user can attempt to rename.
55  // Rename occurrences for the current main file.
56  std::vector<Range> LocalChanges;
57  // Complete edits for the rename, including LocalChanges.
58  // If the full set of changes is unknown, this field is empty.
60 };
61 
62 /// Renames all occurrences of the symbol. The result edits are unformatted.
63 /// If AllowCrossFile is false, returns an error if rename a symbol that's used
64 /// in another file (per the index).
65 llvm::Expected<RenameResult> rename(const RenameInputs &RInputs);
66 
67 /// Generates rename edits that replaces all given occurrences with the
68 /// NewName.
69 /// Exposed for testing only.
70 /// REQUIRED: Occurrences is sorted and doesn't have duplicated ranges.
71 llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
72  llvm::StringRef InitialCode,
73  std::vector<Range> Occurrences,
74  llvm::StringRef NewName);
75 
76 /// Adjusts indexed occurrences to match the current state of the file.
77 ///
78 /// The Index is not always up to date. Blindly editing at the locations
79 /// reported by the index may mangle the code in such cases.
80 /// This function determines whether the indexed occurrences can be applied to
81 /// this file, and heuristically repairs the occurrences if necessary.
82 ///
83 /// The API assumes that Indexed contains only named occurrences (each
84 /// occurrence has the same length).
85 /// REQUIRED: Indexed is sorted.
86 llvm::Optional<std::vector<Range>>
87 adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier,
88  std::vector<Range> Indexed, const LangOptions &LangOpts);
89 
90 /// Calculates the lexed occurrences that the given indexed occurrences map to.
91 /// Returns std::nullopt if we don't find a mapping.
92 ///
93 /// Exposed for testing only.
94 ///
95 /// REQUIRED: Indexed and Lexed are sorted.
96 llvm::Optional<std::vector<Range>> getMappedRanges(ArrayRef<Range> Indexed,
97  ArrayRef<Range> Lexed);
98 /// Evaluates how good the mapped result is. 0 indicates a perfect match.
99 ///
100 /// Exposed for testing only.
101 ///
102 /// REQUIRED: Indexed and Lexed are sorted, Indexed and MappedIndex have the
103 /// same size.
104 size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed, ArrayRef<Range> Lexed,
105  ArrayRef<size_t> MappedIndex);
106 
107 } // namespace clangd
108 } // namespace clang
109 
110 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_RENAME_H
clang::clangd::rename
llvm::Expected< RenameResult > rename(const RenameInputs &RInputs)
Renames all occurrences of the symbol.
Definition: Rename.cpp:733
clang::clangd::RenameResult::Target
Range Target
Definition: Rename.h:54
clang::clangd::RenameInputs
Definition: Rename.h:35
clang::clangd::RenameInputs::MainFilePath
llvm::StringRef MainFilePath
Definition: Rename.h:40
clang::clangd::RenameInputs::AST
ParsedAST & AST
Definition: Rename.h:39
clang::clangd::RenameResult
Definition: Rename.h:52
clang::clangd::RenameOptions::RenameVirtual
bool RenameVirtual
Allow rename of virtual method hierarchies.
Definition: Rename.h:32
clang::clangd::RenameResult::GlobalChanges
FileEdits GlobalChanges
Definition: Rename.h:59
clang::clangd::RenameInputs::Opts
RenameOptions Opts
Definition: Rename.h:49
Protocol.h
clang::clangd::Position
Definition: Protocol.h:156
clang::clangd::RenameInputs::FS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: Rename.h:45
clang::clangd::RenameInputs::Index
const SymbolIndex * Index
Definition: Rename.h:47
clang::clangd::buildRenameEdit
llvm::Expected< Edit > buildRenameEdit(llvm::StringRef AbsFilePath, llvm::StringRef InitialCode, std::vector< Range > Occurrences, llvm::StringRef NewName)
Generates rename edits that replaces all given occurrences with the NewName.
Definition: Rename.cpp:847
clang::clangd::RenameOptions
Definition: Rename.h:22
clang::clangd::RenameResult::LocalChanges
std::vector< Range > LocalChanges
Definition: Rename.h:56
clang::clangd::getMappedRanges
llvm::Optional< std::vector< Range > > getMappedRanges(ArrayRef< Range > Indexed, ArrayRef< Range > Lexed)
Calculates the lexed occurrences that the given indexed occurrences map to.
Definition: Rename.cpp:926
SourceCode.h
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
clang::clangd::RenameOptions::LimitFiles
size_t LimitFiles
The maximum number of affected files (0 means no limit), only meaningful when AllowCrossFile = true.
Definition: Rename.h:26
clang::clangd::Range
Definition: Protocol.h:185
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::renameRangeAdjustmentCost
size_t renameRangeAdjustmentCost(ArrayRef< Range > Indexed, ArrayRef< Range > Lexed, ArrayRef< size_t > MappedIndex)
Evaluates how good the mapped result is.
Definition: Rename.cpp:994
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:375
clang::clangd::FileEdits
llvm::StringMap< Edit > FileEdits
A mapping from absolute file path (the one used for accessing the underlying VFS) to edits.
Definition: SourceCode.h:204
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang::clangd::RenameInputs::Pos
Position Pos
Definition: Rename.h:36
clang::clangd::adjustRenameRanges
llvm::Optional< std::vector< Range > > adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier, std::vector< Range > Indexed, const LangOptions &LangOpts)
Adjusts indexed occurrences to match the current state of the file.
Definition: Rename.cpp:915
clang::clangd::RenameOptions::WantFormat
bool WantFormat
If true, format the rename edits, only meaningful in ClangdServer layer.
Definition: Rename.h:28
clang::clangd::RenameInputs::NewName
llvm::StringRef NewName
Definition: Rename.h:37