forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.cpp
101 lines (90 loc) · 3.19 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
/* 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 "Server.h"
#include <rct/Rct.h>
#include "RTags.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::key(Flags<KeyFlag> flags) 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);
extra += ctx.size();
}
const Path p = path();
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<KeyFlag> flags) const
{
const String code = path().readAll();
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 int col = column() - 1;
if (col + 1 < ret.size()) {
int 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;
}