clang-tools 20.0.0git
Annotations.cpp
Go to the documentation of this file.
1//===--- Annotations.cpp - Annotated source code for unit tests --*- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Annotations.h"
10#include "SourceCode.h"
11
12namespace clang {
13namespace clangd {
14
15Position Annotations::point(llvm::StringRef Name) const {
16 return pointWithPayload(Name).first;
17}
18
19std::pair<Position, llvm::StringRef>
20Annotations::pointWithPayload(llvm::StringRef Name) const {
21 auto [BasePoint, Payload] = Base::pointWithPayload(Name);
22 return {offsetToPosition(code(), BasePoint), Payload};
23}
24
25std::vector<Position> Annotations::points(llvm::StringRef Name) const {
26 auto BasePoints = Base::points(Name);
27
28 std::vector<Position> Ps;
29 Ps.reserve(BasePoints.size());
30 for (const auto Point : BasePoints)
31 Ps.push_back(offsetToPosition(code(), Point));
32
33 return Ps;
34}
35
36std::vector<std::pair<Position, llvm::StringRef>>
37Annotations::pointsWithPayload(llvm::StringRef Name) const {
38 auto BasePoints = Base::pointsWithPayload(Name);
39
40 std::vector<std::pair<Position, llvm::StringRef>> Ps;
41 Ps.reserve(BasePoints.size());
42 for (const auto &[Point, Payload] : BasePoints)
43 Ps.push_back({offsetToPosition(code(), Point), Payload});
44
45 return Ps;
46}
47
48static clangd::Range toLSPRange(llvm::StringRef Code,
49 llvm::Annotations::Range R) {
50 clangd::Range LSPRange;
51 LSPRange.start = offsetToPosition(Code, R.Begin);
52 LSPRange.end = offsetToPosition(Code, R.End);
53 return LSPRange;
54}
55
56Range Annotations::range(llvm::StringRef Name) const {
57 return rangeWithPayload(Name).first;
58}
59
60std::pair<clangd::Range, llvm::StringRef>
61Annotations::rangeWithPayload(llvm::StringRef Name) const {
62 auto [BaseRange, Payload] = Base::rangeWithPayload(Name);
63 return {toLSPRange(code(), BaseRange), Payload};
64}
65
66std::vector<Range> Annotations::ranges(llvm::StringRef Name) const {
67 auto OffsetRanges = Base::ranges(Name);
68
69 std::vector<clangd::Range> Rs;
70 Rs.reserve(OffsetRanges.size());
71 for (const auto &R : OffsetRanges)
72 Rs.push_back(toLSPRange(code(), R));
73
74 return Rs;
75}
76
77std::vector<std::pair<clangd::Range, llvm::StringRef>>
78Annotations::rangesWithPayload(llvm::StringRef Name) const {
79 auto OffsetRanges = Base::rangesWithPayload(Name);
80
81 std::vector<std::pair<clangd::Range, llvm::StringRef>> Rs;
82 Rs.reserve(OffsetRanges.size());
83 for (const auto &[R, Payload] : OffsetRanges)
84 Rs.push_back({toLSPRange(code(), R), Payload});
85
86 return Rs;
87}
88
89} // namespace clangd
90} // namespace clang
llvm::SmallString< 256U > Name
std::string Code
std::string Payload
Definition: SourceCode.cpp:673
clangd::Range range(llvm::StringRef Name="") const
Definition: Annotations.cpp:56
std::vector< Position > points(llvm::StringRef Name="") const
Definition: Annotations.cpp:25
std::vector< clangd::Range > ranges(llvm::StringRef Name="") const
Definition: Annotations.cpp:66
std::vector< std::pair< Position, llvm::StringRef > > pointsWithPayload(llvm::StringRef Name="") const
Definition: Annotations.cpp:37
Position point(llvm::StringRef Name="") const
Definition: Annotations.cpp:15
std::pair< clangd::Range, llvm::StringRef > rangeWithPayload(llvm::StringRef Name="") const
Definition: Annotations.cpp:61
std::vector< std::pair< clangd::Range, llvm::StringRef > > rangesWithPayload(llvm::StringRef Name="") const
Definition: Annotations.cpp:78
std::pair< Position, llvm::StringRef > pointWithPayload(llvm::StringRef Name="") const
Definition: Annotations.cpp:20
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
Definition: SourceCode.cpp:202
static clangd::Range toLSPRange(llvm::StringRef Code, llvm::Annotations::Range R)
Definition: Annotations.cpp:48
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Position start
The range's start position.
Definition: Protocol.h:187
Position end
The range's end position.
Definition: Protocol.h:190