[clangd] Implement LSP 3.17 positionEncoding (#142903)
This PR adds support for the `positionEncoding` client capability introduced in LSP 3.17. Clangd can now negotiate the position encoding with the client during initialization. Fix https://github.com/clangd/clangd/issues/1746 Co-authored-by: kadir çetinkaya <kadircetinkaya.06.tr@gmail.com>
This commit is contained in:
@@ -494,9 +494,9 @@ static std::vector<llvm::StringRef> semanticTokenModifiers() {
|
||||
void ClangdLSPServer::onInitialize(const InitializeParams &Params,
|
||||
Callback<llvm::json::Value> Reply) {
|
||||
// Determine character encoding first as it affects constructed ClangdServer.
|
||||
if (Params.capabilities.offsetEncoding && !Opts.Encoding) {
|
||||
if (Params.capabilities.PositionEncodings && !Opts.Encoding) {
|
||||
Opts.Encoding = OffsetEncoding::UTF16; // fallback
|
||||
for (OffsetEncoding Supported : *Params.capabilities.offsetEncoding)
|
||||
for (OffsetEncoding Supported : *Params.capabilities.PositionEncodings)
|
||||
if (Supported != OffsetEncoding::UnsupportedEncoding) {
|
||||
Opts.Encoding = Supported;
|
||||
break;
|
||||
@@ -686,6 +686,9 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
|
||||
ServerCaps["executeCommandProvider"] =
|
||||
llvm::json::Object{{"commands", Commands}};
|
||||
|
||||
if (Opts.Encoding)
|
||||
ServerCaps["positionEncoding"] = *Opts.Encoding;
|
||||
|
||||
llvm::json::Object Result{
|
||||
{{"serverInfo",
|
||||
llvm::json::Object{
|
||||
@@ -693,6 +696,9 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
|
||||
{"version", llvm::formatv("{0} {1} {2}", versionString(),
|
||||
featureString(), platformString())}}},
|
||||
{"capabilities", std::move(ServerCaps)}}};
|
||||
|
||||
// TODO: offsetEncoding capability is a deprecated clangd extension and should
|
||||
// be deleted.
|
||||
if (Opts.Encoding)
|
||||
Result["offsetEncoding"] = *Opts.Encoding;
|
||||
Reply(std::move(Result));
|
||||
|
||||
@@ -497,10 +497,19 @@ bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R,
|
||||
if (auto Cancel = StaleRequestSupport->getBoolean("cancel"))
|
||||
R.CancelsStaleRequests = *Cancel;
|
||||
}
|
||||
if (auto *PositionEncodings = General->get("positionEncodings")) {
|
||||
R.PositionEncodings.emplace();
|
||||
if (!fromJSON(*PositionEncodings, *R.PositionEncodings,
|
||||
P.field("general").field("positionEncodings")))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (auto *OffsetEncoding = O->get("offsetEncoding")) {
|
||||
R.offsetEncoding.emplace();
|
||||
if (!fromJSON(*OffsetEncoding, *R.offsetEncoding,
|
||||
R.PositionEncodings.emplace();
|
||||
elog("offsetEncoding capability is a deprecated clangd extension that'll "
|
||||
"go away with clangd 23. Migrate to standard positionEncodings "
|
||||
"capability introduced by LSP 3.17");
|
||||
if (!fromJSON(*OffsetEncoding, *R.PositionEncodings,
|
||||
P.field("offsetEncoding")))
|
||||
return false;
|
||||
}
|
||||
@@ -536,8 +545,11 @@ bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R,
|
||||
}
|
||||
}
|
||||
if (auto *OffsetEncoding = Experimental->get("offsetEncoding")) {
|
||||
R.offsetEncoding.emplace();
|
||||
if (!fromJSON(*OffsetEncoding, *R.offsetEncoding,
|
||||
R.PositionEncodings.emplace();
|
||||
elog("offsetEncoding capability is a deprecated clangd extension that'll "
|
||||
"go away with clangd 23. Migrate to standard positionEncodings "
|
||||
"capability introduced by LSP 3.17");
|
||||
if (!fromJSON(*OffsetEncoding, *R.PositionEncodings,
|
||||
P.field("offsetEncoding")))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -528,8 +528,9 @@ struct ClientCapabilities {
|
||||
/// textDocument.semanticHighlightingCapabilities.semanticHighlighting
|
||||
bool TheiaSemanticHighlighting = false;
|
||||
|
||||
/// Supported encodings for LSP character offsets. (clangd extension).
|
||||
std::optional<std::vector<OffsetEncoding>> offsetEncoding;
|
||||
/// Supported encodings for LSP character offsets.
|
||||
/// general.positionEncodings
|
||||
std::optional<std::vector<OffsetEncoding>> PositionEncodings;
|
||||
|
||||
/// The content format that should be used for Hover requests.
|
||||
/// textDocument.hover.contentEncoding
|
||||
|
||||
32
clang-tools-extra/clangd/test/positionencoding.test
Normal file
32
clang-tools-extra/clangd/test/positionencoding.test
Normal file
@@ -0,0 +1,32 @@
|
||||
# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
|
||||
# This test verifies that we can negotiate UTF-8 offsets via the positionEncodings capability introduced in LSP 3.17.
|
||||
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"general":{"positionEncodings":["utf-8","utf-16"]}},"trace":"off"}}
|
||||
# CHECK: "positionEncoding": "utf-8"
|
||||
---
|
||||
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///main.cpp","languageId":"cpp","version":1,"text":"/*ö*/int x;\nint y=x;"}}}
|
||||
---
|
||||
{"jsonrpc":"2.0","id":1,"method":"textDocument/definition","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":1,"character":6}}}
|
||||
# /*ö*/int x;
|
||||
# 01234567890
|
||||
# x is character (and utf-16) range [9,10) but byte range [10,11).
|
||||
# CHECK: "id": 1,
|
||||
# CHECK-NEXT: "jsonrpc": "2.0",
|
||||
# CHECK-NEXT: "result": [
|
||||
# CHECK-NEXT: {
|
||||
# CHECK-NEXT: "range": {
|
||||
# CHECK-NEXT: "end": {
|
||||
# CHECK-NEXT: "character": 11,
|
||||
# CHECK-NEXT: "line": 0
|
||||
# CHECK-NEXT: },
|
||||
# CHECK-NEXT: "start": {
|
||||
# CHECK-NEXT: "character": 10,
|
||||
# CHECK-NEXT: "line": 0
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: },
|
||||
# CHECK-NEXT: "uri": "file://{{.*}}/main.cpp"
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: ]
|
||||
---
|
||||
{"jsonrpc":"2.0","id":10000,"method":"shutdown"}
|
||||
---
|
||||
{"jsonrpc":"2.0","method":"exit"}
|
||||
Reference in New Issue
Block a user