From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#61726: [PATCH] Eglot: Support positionEncoding capability Date: Sat, 25 Feb 2023 19:10:24 +0100 Message-ID: <875ybp1ur3.fsf@gmail.com> References: <87a614g628.fsf@gmail.com> <87y1oncz09.fsf@gmail.com> <83r0ufo3uc.fsf@gnu.org> <87356vbf0b.fsf@gmail.com> <83pm9znw0i.fsf@gnu.org> <87lekn9ss3.fsf@gmail.com> <83h6vbntqd.fsf@gnu.org> <878rgn9r6u.fsf@gmail.com> <83edqfnq5u.fsf@gnu.org> <83bkljnpck.fsf@gnu.org> <874jrb9l6d.fsf@gmail.com> <834jrbnlaf.fsf@gnu.org> <87v8jr83ic.fsf@gmail.com> <83v8jrm4r0.fsf@gnu.org> <87fsav7x81.fsf@gmail.com> <83a612ko73.fsf@gnu.org> <87ttza3rwh.fsf@gmail.com> <83356tluvq.fsf@gnu.org> <87a61125ox.fsf@gmail.com> <83wn45k8yu.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24332"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 61726@debbugs.gnu.org, joaotavora@gmail.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Feb 25 19:11:25 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVz12-0006F9-1H for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 25 Feb 2023 19:11:24 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVz0s-0000sK-GS; Sat, 25 Feb 2023 13:11:14 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVz0q-0000rg-P9 for bug-gnu-emacs@gnu.org; Sat, 25 Feb 2023 13:11:12 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pVz0g-0000WU-Qf for bug-gnu-emacs@gnu.org; Sat, 25 Feb 2023 13:11:12 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pVz0g-0006Mb-Eb for bug-gnu-emacs@gnu.org; Sat, 25 Feb 2023 13:11:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 25 Feb 2023 18:11:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61726 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 61726-submit@debbugs.gnu.org id=B61726.167734863424421 (code B ref 61726); Sat, 25 Feb 2023 18:11:02 +0000 Original-Received: (at 61726) by debbugs.gnu.org; 25 Feb 2023 18:10:34 +0000 Original-Received: from localhost ([127.0.0.1]:41380 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pVz0E-0006Lo-Al for submit@debbugs.gnu.org; Sat, 25 Feb 2023 13:10:34 -0500 Original-Received: from mail-ed1-f41.google.com ([209.85.208.41]:39480) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pVz0D-0006LZ-3a for 61726@debbugs.gnu.org; Sat, 25 Feb 2023 13:10:33 -0500 Original-Received: by mail-ed1-f41.google.com with SMTP id f13so9791751edz.6 for <61726@debbugs.gnu.org>; Sat, 25 Feb 2023 10:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=X1HAt0ca4GXa2K36AD1/JGw/VqZIMr9VHQUkpwjG+mo=; b=fTQAjEYS3sUdAyyJfgzCe0dZ4lyDGgR1H+A0NJ6qHy4dZbXbPJDIPAEIRG/R4GmTA5 DE7645iMlwgIga8Lev2h6prqUXIUyYBcGkH3qxVMsnR1iDgfvK93fwzE/MPttZ1VAace q5TfNnxC1XJn3NRN+OuHLccVwNXkvYC7/JskcGqRQDL5J1hofiXlxZKPOs72Z0lGfSeD TaHW37llXJ5ZZEJNkFUl0rdBse3HzZoM6a49oTbqKEmFqMJFd1A8MtlDfdEVa2v0XIjL 2C7/uTBIkW2/dXP2qcCBhtEke9E+qI7Ge+Pntidg7kikr2rciLWWi3zrscgXTxFcokLC GhXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=X1HAt0ca4GXa2K36AD1/JGw/VqZIMr9VHQUkpwjG+mo=; b=SLWbts1CG1NqAxPww7hYOPWwpl+28a+LI8c+R9Y3uJ0xLMWrx0ShuxEspynD0yPfKc dT5IlhUE8R0F+cJNi2ObIw6HehCMlQy3pPGj2agN0NMjTy495gPpsVFMthfNzLShf9yE p5v8zkKTn+cEPeopJpHHKw3s+Iqmp33TvGgpzUrjUX2DbKaqnzzMGZMW3UoTQUb1F8DX oWhvRMFbEKKBrBFKgcfsAi0Nq5G/kkl4ORrRi8TDLXuWN4+qoAItGRonKOgj//sG0+75 tK0ekMO5Iv9ECtxfyuV1XQlSCJ4pSjCBag836zWq8Bfdsb8k7jElm+f+9YBN0J6Qt2Co f7lQ== X-Gm-Message-State: AO0yUKVE9UMd0J5f/VkUszQUz/P0KTC/VEqMfmEMarsjRK58ZWSY82xE U9QbRwdO8bJkmHRwbl1i1tbnA1VWgCsC9g== X-Google-Smtp-Source: AK7set/oa8GaT+URVBZl4kDBjTIU5qAKV8109h38Qzln07BfGaO2UiKT+votw7QbOmMbpOeHd7C4hA== X-Received: by 2002:aa7:d6c2:0:b0:4ac:c57f:e19c with SMTP id x2-20020aa7d6c2000000b004acc57fe19cmr20048164edr.39.1677348626359; Sat, 25 Feb 2023 10:10:26 -0800 (PST) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::8b3a]) by smtp.gmail.com with ESMTPSA id u4-20020a170906068400b008f883765c98sm1056001ejb.14.2023.02.25.10.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Feb 2023 10:10:25 -0800 (PST) In-Reply-To: <83wn45k8yu.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 25 Feb 2023 18:26:01 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:256735 Archived-At: --=-=-= Content-Type: text/plain Okay, I've attached a new patch with your suggested implementation, which to the extent I'm able to test works correctly. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Eglot-support-positionEncoding-LSP-capability.patch >From 0a2f143cae00db4488a56a5f9e27f0e3589a08ef Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Thu, 23 Feb 2023 08:55:58 +0100 Subject: [PATCH] Eglot: support positionEncoding LSP capability * lisp/progmodes/eglot.el(eglot-client-capabilities): Announce the new capability. (eglot-bytewise-column, eglot-move-to-bytewise-column): New functions. (eglot--managed-mode): Set 'eglot-current-column-function' and 'eglot-move-to-bytewise-column' appropriately. --- lisp/progmodes/eglot.el | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index b569c03e8c2..6b38d39c7b7 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -816,6 +816,9 @@ eglot-client-capabilities `(:valueSet [,@(mapcar #'car eglot--tag-faces)]))) + :general + (list + :positionEncodings ["utf-32" "utf-8" "utf-16"]) :experimental eglot--{}))) (cl-defgeneric eglot-workspace-folders (server) @@ -1441,6 +1444,11 @@ eglot--warn (defun eglot-current-column () (- (point) (line-beginning-position))) +(defun eglot-bytewise-column () + "Calculate current column using the LSP `utf-8' criterion." + (length (encode-coding-region (line-beginning-position) (point) + 'utf-8-unix t))) + (defvar eglot-current-column-function #'eglot-lsp-abiding-column "Function to calculate the current column. @@ -1505,6 +1513,18 @@ eglot-move-to-lsp-abiding-column (forward-char (/ (if (> diff 0) (1+ diff) (1- diff)) 2)) (end-of-buffer (cl-return eob-err)))))) +(defun eglot-move-to-bytewise-column (column) + "Move to COLUMN as computed using the LSP `utf-8' criterion." + (let* ((bol (line-beginning-position)) + (goal-byte (+ (position-bytes bol) column)) + (eol (line-end-position))) + (goto-char bol) + (while (and (< (position-bytes (point)) goal-byte) + (< (point) eol)) + (if (>= (char-after) #x3fff80) ; raw bytes take 2 bytes in the buffer + (setq goal-byte (1+ goal-byte))) + (forward-char 1)))) + (defun eglot--lsp-position-to-point (pos-plist &optional marker) "Convert LSP position POS-PLIST to Emacs point. If optional MARKER, return a marker instead" @@ -1758,6 +1778,14 @@ eglot--managed-mode :init-value nil :lighter nil :keymap eglot-mode-map (cond (eglot--managed-mode + (pcase (plist-get (eglot--capabilities (eglot-current-server)) + :positionEncoding) + ("utf-32" + (eglot--setq-saving eglot-current-column-function #'eglot-current-column) + (eglot--setq-saving eglot-move-to-column-function #'eglot-move-to-column)) + ("utf-8" + (eglot--setq-saving eglot-current-column-function #'eglot-bytewise-column) + (eglot--setq-saving eglot-move-to-column-function #'eglot-move-to-bytewise-column))) (add-hook 'after-change-functions 'eglot--after-change nil t) (add-hook 'before-change-functions 'eglot--before-change nil t) (add-hook 'kill-buffer-hook #'eglot--managed-mode-off nil t) -- 2.39.2 --=-=-= Content-Type: text/plain I still maintain that we are doing a lot of extra work (LOC and CPU-wise) just to guard against the impossible. On the other hand there are probably better places to look for optimization opportunities. For instance, I noticed that when completions arrive form the server, we call eglot-current-column once for each candidate, although most of those will never be used. Also, in a few years every serious server should support the codepoint-based counting method anyway. --=-=-=--