forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncludeFileJob.cpp
151 lines (140 loc) · 5.69 KB
/
IncludeFileJob.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 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 "IncludeFileJob.h"
#include "Project.h"
#include "RTags.h"
#include "Server.h"
IncludeFileJob::IncludeFileJob(const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &project)
: QueryJob(query, project)
{
const uint32_t fileId = Location::fileId(query->currentFile());
mSource = project->sources(fileId).value(query->buildIndex());
if (mSource.isNull()) {
for (const uint32_t dep : project->dependencies(fileId, Project::DependsOnArg)) {
mSource = project->sources(dep).value(query->buildIndex());
if (!mSource.isNull())
break;
}
}
mSymbol = query->query();
}
static inline List<Path> headersForSymbol(const std::shared_ptr<Project> &project, Location loc)
{
List<Path> ret;
const Path &path = loc.path();
if (path.isHeader()) {
ret.append(path);
if (const DependencyNode *node = project->dependencies().value(loc.fileId())) {
for (const auto &dependent : node->dependents) {
const Path p = Location::path(dependent.first);
if (p.isHeader() && dependent.second->includes.size() == 1) {
ret.append(p);
// allow headers that only include one header if we don't
// find anything for the real header
}
}
}
}
return ret;
}
int IncludeFileJob::execute()
{
if (mSource.isNull()) {
return 1;
}
const Path directory = mSource.sourceFile().parentDir();
Set<Location> last;
int matches = 0;
Set<String> all;
auto process = [&directory, this, &all](const Set<Location> &locations) {
for (Location loc : locations) {
bool first = true;
for (const Path &path : headersForSymbol(project(), loc)) {
bool found = false;
const Symbol sym = project()->findSymbol(loc);
switch (sym.kind) {
case CXCursor_ClassDecl:
case CXCursor_StructDecl:
case CXCursor_ClassTemplate:
case CXCursor_TypedefDecl:
if (!sym.isDefinition())
break;
RCT_FALL_THROUGH;
case CXCursor_FunctionDecl:
case CXCursor_FunctionTemplate: {
List<String> alternatives;
if (path.startsWith(directory))
alternatives << String::format<256>("#include \"%s\"", path.mid(directory.size()).constData());
for (const Source::Include &inc : mSource.includePaths) {
const Path p = inc.path.ensureTrailingSlash();
if (path.startsWith(p)) {
const String str = String::format<256>("#include <%s>", path.mid(p.size()).constData());
alternatives << str;
}
}
const int tail = strlen(path.fileName()) + 1;
List<String>::const_iterator it = alternatives.begin();
while (it != alternatives.end()) {
bool drop = false;
for (List<String>::const_iterator it2 = it + 1; it2 != alternatives.end(); ++it2) {
if (it2->size() < it->size()) {
if (!strncmp(it2->constData() + 9, it->constData() + 9, it2->size() - tail - 9)) {
drop = true;
break;
}
}
}
if (!drop) {
found = true;
all.insert(*it);
}
++it;
}
break; }
default:
break;
}
if (first) {
if (found)
break;
first = false;
}
}
}
};
project()->findSymbols(mSymbol, [&](Project::SymbolMatchType type, const String &symbolName, const Set<Location> &locations) {
++matches;
bool fuzzy = false;
if (type == Project::StartsWith) {
fuzzy = true;
const size_t paren = symbolName.indexOf('(');
if (paren == mSymbol.size() && !RTags::isFunctionVariable(symbolName))
fuzzy = false;
}
if (!fuzzy) {
process(locations);
} else if (matches == 1) {
last = locations;
}
}, queryFlags());
if (matches == 1 && !last.isEmpty()) {
process(last);
}
List<String> alternatives = all.toList();
std::sort(alternatives.begin(), alternatives.end(), [](const String &a, const String &b) {
return a.size() < b.size();
});
for (const auto &a : alternatives) {
write(a);
}
return 0;
}