forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenameSymbol.h
119 lines (107 loc) · 3.93 KB
/
RenameSymbol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/lsp/HandlerBase.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/ASTVisitor.h>
namespace solidity::lsp
{
class RenameSymbol: public HandlerBase
{
public:
explicit RenameSymbol(LanguageServer& _server): HandlerBase(_server) {}
void operator()(MessageID, Json const&);
protected:
// Nested class because otherwise `RenameSymbol` couldn't be easily used
// with LanguageServer::m_handlers as `ASTConstVisitor` deletes required
// c'tors
struct Visitor: public frontend::ASTConstVisitor
{
explicit Visitor(RenameSymbol& _outer): m_outer(_outer) {}
void endVisit(frontend::ImportDirective const& _node) override;
void endVisit(frontend::MemberAccess const& _node) override;
void endVisit(frontend::Identifier const& _node) override;
void endVisit(frontend::IdentifierPath const& _node) override;
void endVisit(frontend::FunctionCall const& _node) override;
void endVisit(frontend::InlineAssembly const& _node) override;
void endVisit(frontend::ContractDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::StructDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::EnumDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::EnumValue const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::UserDefinedValueTypeDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::VariableDeclaration const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::FunctionDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::ModifierDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::EventDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
void endVisit(frontend::ErrorDefinition const& _node) override
{
handleGenericDeclaration(_node);
}
bool handleGenericDeclaration(frontend::Declaration const& _declaration)
{
if (
m_outer.m_symbolName == _declaration.name() &&
*m_outer.m_declarationToRename == _declaration
)
{
m_outer.m_locations.emplace_back(_declaration.nameLocation());
return true;
}
return false;
}
private:
RenameSymbol& m_outer;
};
void extractNameAndDeclaration(frontend::ASTNode const& _node, int _cursorBytePosition);
void extractNameAndDeclaration(frontend::IdentifierPath const& _identifierPath, int _cursorBytePosition);
void extractNameAndDeclaration(frontend::ImportDirective const& _importDirective, int _cursorBytePosition);
void extractNameAndDeclaration(frontend::FunctionCall const& _functionCall, int _cursorBytePosition);
void extractNameAndDeclaration(frontend::InlineAssembly const& _inlineAssembly, int _cursorBytePosition);
// Node to rename
frontend::Declaration const* m_declarationToRename = nullptr;
// Original name
frontend::ASTString m_symbolName = {};
// SourceUnits to search & replace symbol in
std::set<frontend::SourceUnit const*, frontend::ASTNode::CompareByID> m_sourceUnits = {};
// Source locations that need to be replaced
std::vector<langutil::SourceLocation> m_locations = {};
};
}