forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FollowLocationJob.cpp
110 lines (96 loc) · 3.67 KB
/
FollowLocationJob.cpp
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
/* This file is part of RTags (http://rtags.net).
RTags 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.
RTags 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 RTags. If not, see <http://www.gnu.org/licenses/>. */
#include "FollowLocationJob.h"
#include "Project.h"
#include "rct/SignalSlot.h"
#include "RTags.h"
#include "Server.h"
FollowLocationJob::FollowLocationJob(Location loc,
const std::shared_ptr<QueryMessage> &query,
const std::shared_ptr<Project> &project)
: QueryJob(query, project), location(loc)
{
}
int FollowLocationJob::execute()
{
int idx = 0;
Symbol symbol = project()->findSymbol(location, &idx);
if (symbol.isNull()) {
return 1;
}
// if you invoke a destructor explicitly there's a typeref on the class
// name. This finds the destructor instead.
if ((symbol.kind == CXCursor_TypeRef || symbol.kind == CXCursor_TemplateRef) && idx > 0) {
auto symbols = project()->openSymbols(location.fileId());
if (!symbols || !symbols->count())
return 1;
const Symbol prev = symbols->valueAt(idx - 1);
if (prev.kind == CXCursor_MemberRefExpr
&& prev.location.column() == symbol.location.column() - 1
&& prev.location.line() == symbol.location.line()
&& prev.symbolName.contains("~")) {
symbol = prev;
}
}
if (queryFlags() & QueryMessage::TargetUsrs) {
const Set<String> usrs = project()->findTargetUsrs(location);
for (const String &usr : usrs) {
for (const Symbol &s : project()->findByUsr(usr, location.fileId(), Project::All)) {
write(s.toString());
}
}
return 0;
}
auto targets = RTags::sortTargets(project()->findTargets(symbol));
int rank = -1;
Set<Location> seen;
auto writeTarget = [&rank, this, &seen](const Symbol &target) {
if (seen.insert(target.location)) {
write(target.location);
rank = RTags::targetRank(target.kind);
}
};
for (const auto &target : targets) {
if (target.location.isNull())
continue;
if (symbol.usr == target.usr) {
writeTarget(target);
if (queryFlags() & QueryMessage::AllTargets)
continue;
break;
}
if (queryFlags() & QueryMessage::DeclarationOnly ? target.isDefinition() : !target.isDefinition()) {
const auto others = RTags::sortTargets(project()->findTargets(target));
bool found = false;
for (auto other : others) {
if (!other.isNull() && other.usr == target.usr) {
found = true;
writeTarget(other);
if (!(queryFlags() & QueryMessage::AllTargets)) {
break;
}
}
}
if (found) {
if (queryFlags() & QueryMessage::AllTargets) {
continue;
} else {
break;
}
}
}
writeTarget(target);
if (!(queryFlags() & QueryMessage::AllTargets))
break;
}
return 0;
}