[clang-tools-extra] [clangd] Implement LSP 3.17 positionEncoding (PR #142903)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 4 22:54:16 PDT 2025
https://github.com/someoneinjd created https://github.com/llvm/llvm-project/pull/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.
>From 5717ca8166a019e4c007099ce243d4f4bd88b599 Mon Sep 17 00:00:00 2001
From: someoneinjd <someoneinjd at outlook.com>
Date: Thu, 5 Jun 2025 13:51:40 +0800
Subject: [PATCH] [clangd] Implement LSP 3.17 positionEncoding
---
clang-tools-extra/clangd/ClangdLSPServer.cpp | 3 ++
clang-tools-extra/clangd/Protocol.cpp | 6 ++++
.../clangd/test/positionencoding.test | 32 +++++++++++++++++++
3 files changed, 41 insertions(+)
create mode 100644 clang-tools-extra/clangd/test/positionencoding.test
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index 29321f7cd3fa2..e7b8cb5f6c79d 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -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{
diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp
index c9e8a175b5d76..bf733af18ad3a 100644
--- a/clang-tools-extra/clangd/Protocol.cpp
+++ b/clang-tools-extra/clangd/Protocol.cpp
@@ -497,6 +497,12 @@ bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R,
if (auto Cancel = StaleRequestSupport->getBoolean("cancel"))
R.CancelsStaleRequests = *Cancel;
}
+ if (auto *OffsetEncoding = General->get("positionEncodings")) {
+ R.offsetEncoding.emplace();
+ if (!fromJSON(*OffsetEncoding, *R.offsetEncoding,
+ P.field("general").field("positionEncodings")))
+ return false;
+ }
}
if (auto *OffsetEncoding = O->get("offsetEncoding")) {
R.offsetEncoding.emplace();
diff --git a/clang-tools-extra/clangd/test/positionencoding.test b/clang-tools-extra/clangd/test/positionencoding.test
new file mode 100644
index 0000000000000..eea7a1a596e9a
--- /dev/null
+++ b/clang-tools-extra/clangd/test/positionencoding.test
@@ -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"}
More information about the cfe-commits
mailing list