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: code(), Offset: 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(n: BasePoints.size());
30 for (const auto Point : BasePoints)
31 Ps.push_back(x: offsetToPosition(Code: code(), Offset: 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(n: BasePoints.size());
42 for (const auto &[Point, Payload] : BasePoints)
43 Ps.push_back(x: {offsetToPosition(Code: code(), Offset: 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, Offset: R.Begin);
52 LSPRange.end = offsetToPosition(Code, Offset: 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: code(), R: 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(n: OffsetRanges.size());
71 for (const auto &R : OffsetRanges)
72 Rs.push_back(x: toLSPRange(Code: 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(n: OffsetRanges.size());
83 for (const auto &[R, Payload] : OffsetRanges)
84 Rs.push_back(x: {toLSPRange(Code: code(), R), Payload});
85
86 return Rs;
87}
88
89} // namespace clangd
90} // namespace clang
91

source code of clang-tools-extra/clangd/unittests/Annotations.cpp