From a49b38678cafe6c06859d5c56b6f1666a9d9fba9 Mon Sep 17 00:00:00 2001 From: Jeremy Weed Date: Sat, 9 Nov 2024 23:12:32 -0800 Subject: [PATCH] Fix processing of multibyte messages (#4519) * Fix processing of multibyte messages * Too many characters are read from back-to-back multibyte messages due to a conversion from byte-length to character length * Updated to immediately convert messages to unibyte when processing * Update encoding logic to avoid copies --------- Co-authored-by: weedjer --- lsp-mode.el | 4 ++-- test/lsp-io-test.el | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lsp-mode.el b/lsp-mode.el index 0ea51e9411..f81851577f 100644 --- a/lsp-mode.el +++ b/lsp-mode.el @@ -7074,8 +7074,8 @@ server. WORKSPACE is the active workspace." leftovers body-length body chunk) (lambda (_proc input) (setf chunk (if (s-blank? leftovers) - input - (concat leftovers input))) + (encode-coding-string input 'utf-8-unix t) + (concat leftovers (encode-coding-string input 'utf-8-unix t)))) (let (messages) (while (not (s-blank? chunk)) diff --git a/test/lsp-io-test.el b/test/lsp-io-test.el index edf9daeb4a..46038a0e8f 100644 --- a/test/lsp-io-test.el +++ b/test/lsp-io-test.el @@ -66,6 +66,19 @@ (messages (funcall fn message-in))) (should (equal messages '("’"))))) +(ert-deftest lsp--parser-read--multiple-multibyte-messages () + (let* ((fn (lsp--create-process-message)) + (messages-in '("Content-Length: 3\r\n\r\n←" + "Content-Length: 3\r\n\r\n←" + "Content-Length:3\r\n\r\n←" + "Content-Length: 3\r\n\r\n←" + "Content-Length:3\r\n\r\n←" + "Content-Length: 3\r\n\r\n←" + "Content-Length: 3\r\n\r\n←" + )) + (messages (funcall fn (string-join messages-in)))) + (should (equal messages '("←" "←" "←" "←" "←" "←" "←"))))) + (ert-deftest lsp--parser-read--multibyte-nospace () (let* ((fn (lsp--create-process-message)) (message-in "Content-Length:3\r\n\r\n\xe2\x80\x99")