clang-tools  10.0.0
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 "Path.h"
13 #include "Protocol.h"
14 #include "SourceCode.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Tooling/Core/Replacement.h"
17 #include "llvm/Support/Error.h"
18 
19 namespace clang {
20 namespace clangd {
21 class ParsedAST;
22 class SymbolIndex;
23 
24 /// Gets dirty buffer for a given file \p AbsPath.
25 /// Returns None if there is no dirty buffer for the given file.
26 using DirtyBufferGetter =
27  llvm::function_ref<llvm::Optional<std::string>(PathRef AbsPath)>;
28 
29 struct RenameInputs {
30  Position Pos; // the position triggering the rename
31  llvm::StringRef NewName;
32 
34  llvm::StringRef MainFilePath;
35 
36  const SymbolIndex *Index = nullptr;
37 
38  bool AllowCrossFile = false;
39  // When set, used by the rename to get file content for all rename-related
40  // files.
41  // If there is no corresponding dirty buffer, we will use the file content
42  // from disk.
44 };
45 
46 /// Renames all occurrences of the symbol.
47 /// If AllowCrossFile is false, returns an error if rename a symbol that's used
48 /// in another file (per the index).
49 llvm::Expected<FileEdits> rename(const RenameInputs &RInputs);
50 
51 /// Generates rename edits that replaces all given occurrences with the
52 /// NewName.
53 /// Exposed for testing only.
54 /// REQUIRED: Occurrences is sorted and doesn't have duplicated ranges.
55 llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
56  llvm::StringRef InitialCode,
57  std::vector<Range> Occurrences,
58  llvm::StringRef NewName);
59 
60 /// Adjusts indexed occurrences to match the current state of the file.
61 ///
62 /// The Index is not always up to date. Blindly editing at the locations
63 /// reported by the index may mangle the code in such cases.
64 /// This function determines whether the indexed occurrences can be applied to
65 /// this file, and heuristically repairs the occurrences if necessary.
66 ///
67 /// The API assumes that Indexed contains only named occurrences (each
68 /// occurrence has the same length).
69 /// REQUIRED: Indexed is sorted.
70 llvm::Optional<std::vector<Range>>
71 adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier,
72  std::vector<Range> Indexed, const LangOptions &LangOpts);
73 
74 /// Calculates the lexed occurrences that the given indexed occurrences map to.
75 /// Returns None if we don't find a mapping.
76 ///
77 /// Exposed for testing only.
78 ///
79 /// REQUIRED: Indexed and Lexed are sorted.
80 llvm::Optional<std::vector<Range>> getMappedRanges(ArrayRef<Range> Indexed,
81  ArrayRef<Range> Lexed);
82 /// Evaluates how good the mapped result is. 0 indicates a perfect match.
83 ///
84 /// Exposed for testing only.
85 ///
86 /// REQUIRED: Indexed and Lexed are sorted, Indexed and MappedIndex have the
87 /// same size.
88 size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed, ArrayRef<Range> Lexed,
89  ArrayRef<size_t> MappedIndex);
90 
91 } // namespace clangd
92 } // namespace clang
93 
94 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_RENAME_H
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:594
const SymbolIndex * Index
Definition: Rename.h:36
DirtyBufferGetter GetDirtyBuffer
Definition: Rename.h:43
llvm::function_ref< llvm::Optional< std::string >(PathRef AbsPath)> DirtyBufferGetter
Gets dirty buffer for a given file AbsPath.
Definition: Rename.h:27
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
llvm::StringRef MainFilePath
Definition: Rename.h:34
llvm::StringRef NewName
Definition: Rename.h:31
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
size_t renameRangeAdjustmentCost(ArrayRef< Range > Indexed, ArrayRef< Range > Lexed, ArrayRef< size_t > MappedIndex)
Evaluates how good the mapped result is.
Definition: Rename.cpp:655
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:584
llvm::Expected< FileEdits > rename(const RenameInputs &RInputs)
Renames all occurrences of the symbol.
Definition: Rename.cpp:427
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:519