forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindFileJob.cpp
130 lines (121 loc) · 4 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
/* This file is part of RTags.
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 "RTags.h"
#include "Server.h"
#include "CursorInfo.h"
#include "FileManager.h"
#include "Project.h"
FindFileJob::FindFileJob(const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &project)
: Job(query, QuietJob, project)
{
const String q = query->query();
if (!q.isEmpty()) {
if (query->flags() & QueryMessage::MatchRegexp) {
mRegExp = q;
} else {
mPattern = q;
}
}
}
int FindFileJob::execute()
{
std::shared_ptr<Project> proj = project();
if (!proj || !proj->fileManager) {
return 1;
}
const Path srcRoot = proj->path();
enum Mode {
All,
RegExp,
Pattern
} mode = All;
String::CaseSensitivity cs = String::CaseSensitive;
if (mRegExp.isValid()) {
mode = RegExp;
} else if (!mPattern.isEmpty()) {
mode = Pattern;
}
if (queryFlags() & QueryMessage::MatchCaseInsensitive)
cs = String::CaseInsensitive;
String out;
out.reserve(PATH_MAX);
if (queryFlags() & QueryMessage::AbsolutePath) {
out.append(srcRoot);
assert(srcRoot.endsWith('/'));
}
const FilesMap& dirs = proj->files();
FilesMap::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;
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 RegExp:
ok = mRegExp.indexIn(out) != -1;
break;
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);
}
}
break;
}
if (ok) {
ret = 0;
if (preferExact && !foundExact) {
matches.append(out);
} else {
if (!write(out))
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 (!write(*it)) {
ret = 2;
break;
}
}
return ret;
}