forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Location.cpp
132 lines (116 loc) · 3.99 KB
/
Location.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
/* 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 "Location.h"
#include "rct/Rct.h"
#include "RTags.h"
#include "Server.h"
#include "Project.h"
Hash<Path, uint32_t> Location::sPathsToIds;
Hash<uint32_t, Path> Location::sIdsToPaths;
uint32_t Location::sLastId = 0;
std::mutex Location::sMutex;
static inline uint64_t createMask(int startBit, int bitCount)
{
uint64_t mask = 0;
for (int i=startBit; i<startBit + bitCount; ++i) {
mask |= (static_cast<uint64_t>(1) << i);
}
return mask;
}
const uint64_t Location::FILEID_MASK = createMask(0, FileBits);
const uint64_t Location::LINE_MASK = createMask(FileBits, LineBits);
const uint64_t Location::COLUMN_MASK = createMask(FileBits + LineBits, ColumnBits);
String Location::toString(Flags<ToStringFlag> flags, Hash<Path, String> *contextCache) const
{
if (isNull())
return String();
const unsigned int l = line();
const unsigned int c = column();
int extra = RTags::digits(l) + RTags::digits(c) + 3;
String ctx;
if (flags & Location::ShowContext) {
ctx += '\t';
ctx += context(flags, contextCache);
extra += ctx.size();
}
Path p = path();
if (!(flags & AbsolutePath) && Server::instance()) {
Server *server = Server::instance();
if (std::shared_ptr<Project> pp = server->currentProject()) {
const Path projectPath = pp->path();
if (!projectPath.isEmpty() && p.startsWith(projectPath))
p.remove(0, projectPath.size());
}
}
String ret(p.size() + extra, ' ');
const int w = snprintf(ret.data(), ret.size() + extra + 1, "%s:%d:%d:", p.constData(), l, c);
if (!ctx.isEmpty()) {
memcpy(ret.data() + w, ctx.constData(), ctx.size());
}
return ret;
}
String Location::context(Flags<ToStringFlag> flags, Hash<Path, String> *cache) const
{
String copy;
String *code = 0;
const Path p = path();
if (cache) {
String &ref = (*cache)[p];
if (ref.isEmpty()) {
ref = p.readAll();
}
code = &ref;
} else {
copy = p.readAll();
code = ©
}
String ret;
if (!code->isEmpty()) {
unsigned int l = line();
if (!l)
return String();
const char *ch = code->constData();
while (--l) {
ch = strchr(ch, '\n');
if (!ch)
return String();
++ch;
}
const char *end = strchr(ch, '\n');
if (!end)
return String();
ret.assign(ch, end - ch);
// error() << "foobar" << ret << bool(flags & NoColor);
if (!(flags & NoColor)) {
const size_t col = column() - 1;
if (col + 1 < ret.size()) {
size_t last = col;
if (ret.at(last) == '~')
++last;
while (ret.size() > last && (isalnum(ret.at(last)) || ret.at(last) == '_'))
++last;
static const char *color = "\x1b[32;1m"; // dark yellow
static const char *resetColor = "\x1b[0;0m";
// error() << "foobar"<< end << col << ret.size();
ret.insert(last, resetColor);
ret.insert(col, color);
}
// printf("[%s]\n", ret.constData());
}
}
return ret;
}
void Location::saveFileIds()
{
assert(Server::instance());
Server::instance()->saveFileIds();
}