🐛 fix(core): convert diagnostics to UTF-16 positions
This commit is contained in:
@@ -16,6 +16,7 @@ import lsp.manager.manager_hub;
|
||||
import lsp.protocol;
|
||||
import lsp.provider.manifest;
|
||||
import lsp.scheduler.async_executor;
|
||||
import lsp.utils.text_coordinates;
|
||||
|
||||
namespace transform = lsp::codec;
|
||||
|
||||
@@ -753,10 +754,10 @@ namespace lsp::core
|
||||
for (const auto& error : errors)
|
||||
{
|
||||
protocol::Diagnostic diagnostic;
|
||||
diagnostic.range.start.line = error.location.start_line;
|
||||
diagnostic.range.start.character = error.location.start_column;
|
||||
diagnostic.range.end.line = error.location.end_line;
|
||||
diagnostic.range.end.character = error.location.end_column;
|
||||
diagnostic.range.start = utils::text_coordinates::ToPosition(
|
||||
TSPoint{ error.location.start_line, error.location.start_column }, content);
|
||||
diagnostic.range.end = utils::text_coordinates::ToPosition(
|
||||
TSPoint{ error.location.end_line, error.location.end_column }, content);
|
||||
|
||||
switch (error.severity)
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@ export namespace lsp::utils::text_coordinates
|
||||
};
|
||||
|
||||
BytePosition ToBytePosition(const protocol::Position& position, const protocol::string& content);
|
||||
protocol::Position ToPosition(TSPoint point, const protocol::string& content);
|
||||
protocol::uinteger ToOffset(const protocol::Position& position, const protocol::string& content);
|
||||
TSPoint CalculateEndPoint(const protocol::string& text, TSPoint start);
|
||||
}
|
||||
@@ -120,6 +121,37 @@ namespace lsp::utils::text_coordinates
|
||||
};
|
||||
}
|
||||
|
||||
protocol::Position ToPosition(TSPoint point, const protocol::string& content)
|
||||
{
|
||||
std::size_t offset = 0;
|
||||
std::uint32_t row = 0;
|
||||
|
||||
while (offset < content.size() && row < point.row)
|
||||
{
|
||||
if (content[offset++] == '\n')
|
||||
++row;
|
||||
}
|
||||
|
||||
protocol::uinteger utf16_units = 0;
|
||||
std::size_t byte_column = 0;
|
||||
while (offset < content.size() && content[offset] != '\n' &&
|
||||
byte_column < point.column)
|
||||
{
|
||||
const auto decoded = DecodeCharacter(content, offset);
|
||||
if (decoded.byte_count > point.column - byte_column)
|
||||
break;
|
||||
|
||||
offset += decoded.byte_count;
|
||||
byte_column += decoded.byte_count;
|
||||
utf16_units += decoded.utf16_units;
|
||||
}
|
||||
|
||||
return {
|
||||
.line = row,
|
||||
.character = utf16_units,
|
||||
};
|
||||
}
|
||||
|
||||
protocol::uinteger ToOffset(const protocol::Position& position, const protocol::string& content)
|
||||
{
|
||||
return ToBytePosition(position, content).offset;
|
||||
|
||||
Reference in New Issue
Block a user