-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathdocument_links.cpp
More file actions
64 lines (52 loc) · 1.88 KB
/
Copy pathdocument_links.cpp
File metadata and controls
64 lines (52 loc) · 1.88 KB
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
#include <cstdint>
#include <string>
#include <vector>
#include "feature/feature.h"
#include "syntax/lexer.h"
namespace clice::feature {
auto document_links(CompilationUnitRef unit, PositionEncoding encoding)
-> std::vector<protocol::DocumentLink> {
std::vector<protocol::DocumentLink> links;
auto interested = unit.interested_file();
auto directives_it = unit.directives().find(interested);
if(directives_it == unit.directives().end()) {
return links;
}
auto content = unit.interested_content();
auto line_starts = lsp::build_line_starts(content);
auto& directives = directives_it->second;
auto* lang_opts = &unit.lang_options();
auto add_link = [&](clang::SourceLocation loc, llvm::StringRef target) {
auto [fid, offset] = unit.decompose_location(loc);
if(fid != interested || offset >= content.size())
return;
auto range = find_directive_argument(content, offset, lang_opts);
if(!range)
return;
protocol::DocumentLink link{.range = to_range(content, line_starts, encoding, *range)};
link.target = target.str();
links.push_back(std::move(link));
};
for(const auto& include: directives.includes) {
if(include.fid.isValid()) {
add_link(include.location, unit.file_path(include.fid));
}
}
for(const auto& has_include: directives.has_includes) {
if(has_include.fid.isValid()) {
add_link(has_include.location, unit.file_path(has_include.fid));
}
}
for(const auto& embed: directives.embeds) {
if(embed.file) {
add_link(embed.loc, embed.file->getName());
}
}
for(const auto& has_embed: directives.has_embeds) {
if(has_embed.file) {
add_link(has_embed.loc, has_embed.file->getName());
}
}
return links;
}
} // namespace clice::feature