3#include <llvm/Support/Errc.h>
10template <
typename Callback>
11static bool iterateCodepoints(llvm::StringRef
U8,
const Callback &
CB) {
15 for (
size_t I = 0;
I <
U8.size();) {
16 unsigned char C =
static_cast<unsigned char>(
U8[
I]);
29 elog(
"File has invalid UTF-8 near offset {0}: {1}",
I, llvm::toHex(
U8));
52 switch (lspEncoding()) {
57 iterateCodepoints(Code, [&](
int U8Len,
int U16Len) {
63 iterateCodepoints(Code, [&](
int U8Len,
int U16Len) {
118 return error(llvm::errc::invalid_argument,
119 "Line value can't be negative ({0})", P.
line);
121 return error(llvm::errc::invalid_argument,
122 "Character value can't be negative ({0})", P.
character);
124 for (
int I = 0;
I != P.
line; ++
I) {
126 if (
NextNL == llvm::StringRef::npos)
127 return error(llvm::errc::invalid_argument,
128 "Line value is out of range ({0})", P.
line);
131 llvm::StringRef Line =
132 Code.substr(
StartOfLine).take_until([](
char C) {
return C ==
'\n'; });
138 return error(llvm::errc::invalid_argument,
139 "{0} offset {1} is invalid for line {2}", lspEncoding(),
145 Offset = std::min(Code.size(), Offset);
146 llvm::StringRef
Before = Code.substr(0, Offset);
174static void inferFinalNewline(llvm::Expected<size_t> &
Err,
175 std::string &Contents,
const Position &Pos) {
178 if (!Contents.empty() && Contents.back() ==
'\n')
180 if (Pos.character != 0)
182 if (Pos.line != llvm::count(Contents,
'\n') + 1)
184 log(
"Editor sent invalid change coordinates, inferring newline at EOF");
185 Contents.push_back(
'\n');
187 Err = Contents.size();
194 return llvm::Error::success();
205 inferFinalNewline(
EndIndex, Contents, End);
210 return error(llvm::errc::invalid_argument,
211 "Range's end position ({0}) is before start position ({1})",
225 return error(llvm::errc::invalid_argument,
226 "Change's rangeLength ({0}) doesn't match the "
227 "computed range length ({1}).",
232 return llvm::Error::success();
Whether current platform treats paths case insensitively.
llvm::unique_function< void(llvm::Expected< T >)> Callback
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
llvm::Error applyChange(std::string &Contents, const TextDocumentContentChangeEvent &Change)
Apply an incremental update to a text document.
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
size_t lspLength(llvm::StringRef Code)
void elog(const char *Fmt, Ts &&...Vals)
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength=true)
void log(const char *Fmt, Ts &&...Vals)
int line
Line position in a document (zero-based).