forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindFileJob.cpp
170 lines (156 loc) · 5.34 KB
/
FindFileJob.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* 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 "FindFileJob.h"
#include "FileManager.h"
#include "Project.h"
#include "rct/SignalSlot.h"
#include "RTags.h"
#include "Server.h"
static Flags<QueryJob::JobFlag> flags(Flags<QueryMessage::Flag> queryFlags)
{
Flags<QueryJob::JobFlag> flags = QueryJob::QuietJob;
if (queryFlags & QueryMessage::Elisp)
flags |= QueryJob::QuoteOutput;
return flags;
}
FindFileJob::FindFileJob(const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &project)
: QueryJob(query, project, ::flags(query->flags()))
{
const String q = query->query();
if (!q.isEmpty()) {
if (query->flags() & QueryMessage::MatchRegex) {
mRegex = q.ref();
} else {
mPattern = q;
}
}
}
int FindFileJob::execute()
{
std::shared_ptr<Project> proj = project();
if (!proj) {
return 1;
}
if (!proj->fileManager())
return 1;
const Path srcRoot = proj->path();
assert(srcRoot.endsWith('/'));
enum Mode {
All,
FilePath,
Regex,
Pattern,
} mode = All;
String::CaseSensitivity cs = String::CaseSensitive;
if (queryFlags() & QueryMessage::MatchRegex) {
mode = Regex;
} else if (!mPattern.isEmpty()) {
mode = mPattern[0] == '/' ? FilePath : Pattern;
}
if (queryFlags() & QueryMessage::MatchCaseInsensitive)
cs = String::CaseInsensitive;
String out;
out.reserve(PATH_MAX);
const bool absolutePath = queryFlags() & QueryMessage::AbsolutePath;
if (absolutePath)
out.append(srcRoot);
const Files& dirs = proj->files();
assert(proj->fileManager());
if (dirs.isEmpty())
proj->fileManager()->load(FileManager::Synchronous);
Files::const_iterator dirit = dirs.begin();
bool foundExact = false;
const int patternSize = mPattern.size();
List<String> matches;
const bool preferExact = queryFlags() & QueryMessage::FindFilePreferExact;
int ret = 1;
bool firstElisp = queryFlags() & QueryMessage::Elisp;
auto writeFile = [this, &firstElisp](const Path &path) {
if (firstElisp) {
firstElisp = false;
if (!write("(list", DontQuote))
return false;
}
return write(path);
};
while (dirit != dirs.end()) {
const Path &dir = dirit->first;
if (dir.size() < srcRoot.size()) {
continue;
} else {
out.append(dir.constData() + srcRoot.size(), dir.size() - srcRoot.size());
}
const Set<String> &files = dirit->second;
for (Set<String>::const_iterator it = files.begin(); it != files.end(); ++it) {
const String &key = *it;
out.append(key);
bool ok;
switch (mode) {
case All:
ok = true;
break;
case Regex:
ok = Rct::contains(out, mRegex);
break;
case FilePath:
case Pattern:
if (!preferExact) {
ok = out.contains(mPattern, cs);
} else {
const int outSize = out.size();
const bool exact = (outSize > patternSize && out.endsWith(mPattern) && out.at(outSize - (patternSize + 1)) == '/');
if (exact) {
ok = true;
if (!foundExact) {
matches.clear();
foundExact = true;
}
} else {
ok = !foundExact && out.contains(mPattern, cs);
}
}
if (!ok && mode == FilePath) {
Path p(out);
if (!absolutePath)
p.prepend(srcRoot);
p.resolve();
if (p == mPattern)
ok = true;
}
break;
}
if (ok) {
ret = 0;
Path matched = out;
if (absolutePath)
matched.resolve();
if (preferExact && !foundExact) {
matches.append(matched);
} else {
if (!writeFile(matched))
return 1;
}
}
out.chop(key.size());
}
out.chop(dir.size() - srcRoot.size());
++dirit;
}
for (List<String>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
if (!writeFile(*it)) {
return 1;
}
}
if (queryFlags() & QueryMessage::Elisp && !firstElisp && !write(")", DontQuote))
return 1;
return ret;
}