A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
offsets.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef A11_FLOW_OFFSETS_H_
18#define A11_FLOW_OFFSETS_H_
19
20#include <cstddef>
21#include <string>
22#include <string_view>
23#include <utility>
24#include <vector>
25
26#include <absl/container/flat_hash_map.h>
27#include <nlohmann/json_fwd.hpp>
28
29namespace a11::flow {
30
31// A document, and the offset arithmetic every editor protocol needs over it. So
32// the conversion lives here, once, and both protocol adapters use it.
41class TextIndex {
42 public:
43 TextIndex() = default;
44 explicit TextIndex(std::string text);
45
46 [[nodiscard]] const std::string& Text() const { return text_; }
47
48 [[nodiscard]] size_t LineCount() const { return line_starts_.size(); }
49
51 [[nodiscard]] size_t Utf16Of(size_t byte_offset) const;
52
54 [[nodiscard]] size_t ByteOf(size_t utf16_offset) const;
55
58 [[nodiscard]] std::pair<int, int> PositionOf(size_t byte_offset) const;
59
61 [[nodiscard]] size_t ByteOfPosition(int line, int character) const;
62
64 [[nodiscard]] size_t ByteOfSourcePosition(int line, int column) const;
65
66 private:
70 [[nodiscard]] std::pair<size_t, size_t> Step(size_t offset) const;
71
72 [[nodiscard]] size_t LineEnd(size_t line) const;
73
74 std::string text_;
76 std::vector<size_t> line_starts_;
80 std::vector<size_t> line_utf16_starts_;
81};
82
84enum class OffsetBasis {
86 kBytes,
89 kUtf16,
90};
91
96bool OffsetBasisFromName(std::string_view name, OffsetBasis& basis);
97
99std::string_view OffsetBasisName(OffsetBasis basis);
100
116void RebaseToUtf16(nlohmann::json& answer, const TextIndex& index);
117
119void RebaseToUtf16(
120 nlohmann::json& answer, const TextIndex& index,
121 const absl::flat_hash_map<std::string, TextIndex>& documents);
122
123} // namespace a11::flow
124
125#endif // A11_FLOW_OFFSETS_H_
A document, and the offset arithmetic every editor protocol needs over it.
Definition offsets.h:41
size_t Utf16Of(size_t byte_offset) const
How many UTF-16 units of the document precede this byte offset.
Definition offsets.cc:69
const std::string & Text() const
Definition offsets.h:46
std::pair< int, int > PositionOf(size_t byte_offset) const
The LSP position – zero-based line, UTF-16 units into it – of a byte offset.
Definition offsets.cc:101
size_t ByteOfPosition(int line, int character) const
The byte offset of an LSP position, clamped into the document.
Definition offsets.cc:129
size_t LineCount() const
Definition offsets.h:48
size_t ByteOf(size_t utf16_offset) const
The byte offset that many UTF-16 units into the document.
Definition offsets.cc:86
size_t ByteOfSourcePosition(int line, int column) const
Byte offset of a one-based source line and code-point column.
Definition offsets.cc:116
std::optional< std::string > text
The text, where the value is a string literal or a name bound to one.
Definition discover.cc:596
std::string name
The declaration name and its colon.
Definition format.cc:50
Definition catalogue.cc:29
OffsetBasis
Which basis the offsets in a request and its answer are counted in.
Definition offsets.h:84
@ kUtf16
UTF-16 code units from the start of the file: what a JVM or JavaScript editor host indexes its docume...
@ kBytes
Bytes from the start of the file: the language's own, and the default.
void RebaseToUtf16(nlohmann::json &answer, const TextIndex &index)
Rewrite every document offset in an answer from bytes into UTF-16 units.
Definition offsets.cc:163
std::string_view OffsetBasisName(OffsetBasis basis)
The name of a basis, for a message and for the JSON.
Definition offsets.cc:159
bool OffsetBasisFromName(std::string_view name, OffsetBasis &basis)
The basis a request named, or kBytes when it named none.
Definition offsets.cc:147
std::uint32_t offset
Definition runtime_streams.cc:36
One resolved statement.
Definition graph.h:238
int line
Definition main.cc:71
int column
Definition main.cc:72