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