🐛 fix(core): convert diagnostics to UTF-16 positions

This commit is contained in:
csh
2026-07-14 09:35:34 +08:00
parent 71b519793a
commit 12e1a87b2b
3 changed files with 59 additions and 4 deletions
@@ -21,6 +21,7 @@ export namespace lsp::test::provider
static TestResult TestMultilineAndClampedPositions();
static TestResult TestMalformedUtf8IsBounded();
static TestResult TestCalculateEndPointUsesByteColumns();
static TestResult TestBytePointsToUtf16Positions();
};
}
@@ -37,6 +38,14 @@ namespace lsp::test::provider
assertEqual(row, actual.point.row, "Tree-sitter row should match");
assertEqual(column, actual.point.column, "Tree-sitter byte column should match");
}
void ExpectLspPosition(const protocol::Position& actual,
protocol::uinteger line,
protocol::uinteger character)
{
assertEqual(line, actual.line, "LSP line should match");
assertEqual(character, actual.character, "LSP UTF-16 character should match");
}
}
void TextCoordinatesTests::Register(TestRunner& runner)
@@ -46,6 +55,7 @@ namespace lsp::test::provider
runner.addTest("text coordinates clamp multiline positions", TestMultilineAndClampedPositions);
runner.addTest("text coordinates bound malformed UTF-8", TestMalformedUtf8IsBounded);
runner.addTest("text coordinates calculate byte end points", TestCalculateEndPointUsesByteColumns);
runner.addTest("text coordinates convert byte points to UTF-16 positions", TestBytePointsToUtf16Positions);
}
TestResult TextCoordinatesTests::TestAsciiAndBmpPositions()
@@ -93,4 +103,16 @@ namespace lsp::test::provider
assertEqual(5U, end.column, "multibyte text should advance the byte column");
return { "", true, "ok" };
}
TestResult TextCoordinatesTests::TestBytePointsToUtf16Positions()
{
const protocol::string content = "A中😀Z\n😀x";
ExpectLspPosition(utils::text_coordinates::ToPosition({ 0U, 0U }, content), 0U, 0U);
ExpectLspPosition(utils::text_coordinates::ToPosition({ 0U, 4U }, content), 0U, 2U);
ExpectLspPosition(utils::text_coordinates::ToPosition({ 0U, 8U }, content), 0U, 4U);
ExpectLspPosition(utils::text_coordinates::ToPosition({ 1U, 4U }, content), 1U, 2U);
ExpectLspPosition(utils::text_coordinates::ToPosition({ 0U, 6U }, content), 0U, 2U);
return { "", true, "ok" };
}
}