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 <nlohmann/json_fwd.hpp>
27
28namespace a11::flow {
29
30// A document, and the offset arithmetic every editor protocol needs over it. So
31// the conversion lives here, once, and both protocol adapters use it.
40class TextIndex {
41 public:
42 TextIndex() = default;
43 explicit TextIndex(std::string text);
44
45 [[nodiscard]] const std::string& Text() const { return text_; }
46
47 [[nodiscard]] size_t LineCount() const { return line_starts_.size(); }
48
50 [[nodiscard]] size_t Utf16Of(size_t byte_offset) const;
51
53 [[nodiscard]] size_t ByteOf(size_t utf16_offset) const;
54
57 [[nodiscard]] std::pair<int, int> PositionOf(size_t byte_offset) const;
58
60 [[nodiscard]] size_t ByteOfPosition(int line, int character) const;
61
62 private:
66 [[nodiscard]] std::pair<size_t, size_t> Step(size_t offset) const;
67
68 [[nodiscard]] size_t LineEnd(size_t line) const;
69
70 std::string text_;
72 std::vector<size_t> line_starts_;
76 std::vector<size_t> line_utf16_starts_;
77};
78
80enum class OffsetBasis {
82 kBytes,
85 kUtf16,
86};
87
92bool OffsetBasisFromName(std::string_view name, OffsetBasis& basis);
93
95std::string_view OffsetBasisName(OffsetBasis basis);
96
112void RebaseToUtf16(nlohmann::json& answer, const TextIndex& index);
113
114} // namespace a11::flow
115
116#endif // A11_FLOW_OFFSETS_H_
A document, and the offset arithmetic every editor protocol needs over it.
Definition offsets.h:40
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:45
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:116
size_t LineCount() const
Definition offsets.h:47
size_t ByteOf(size_t utf16_offset) const
The byte offset that many UTF-16 units into the document.
Definition offsets.cc:86
std::optional< std::string > text
The text, where the value is a string literal or a name bound to one.
Definition discover.cc:612
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
Definition boundary.cc:32
OffsetBasis
Which basis the offsets in a request and its answer are counted in.
Definition offsets.h:80
@ 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:150
std::string_view OffsetBasisName(OffsetBasis basis)
The name of a basis, for a message and for the JSON.
Definition offsets.cc:146
bool OffsetBasisFromName(std::string_view name, OffsetBasis &basis)
The basis a request named, or kBytes when it named none.
Definition offsets.cc:134
int line
Definition main.cc:70
long long offset
Definition main.cc:69