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: Thu, 23 Feb 2023 09:05:35 +0100 Message-ID: <87a614g628.fsf@gmail.com> 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="4559"; mail-complaints-to="usenet@ciao.gmane.io" Cc: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= To: 61726@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Feb 23 09:06:19 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 1pV6cM-00011v-T6 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 23 Feb 2023 09:06:19 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pV6c8-0002Yd-KQ; Thu, 23 Feb 2023 03:06:04 -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 1pV6c6-0002YV-Fc for bug-gnu-emacs@gnu.org; Thu, 23 Feb 2023 03:06:02 -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 1pV6c6-0006Ga-7U for bug-gnu-emacs@gnu.org; Thu, 23 Feb 2023 03:06:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pV6c5-0008FQ-Oy; Thu, 23 Feb 2023 03:06:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: joaotavora@gmail.com, bug-gnu-emacs@gnu.org Resent-Date: Thu, 23 Feb 2023 08:06:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 61726 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-Received: via spool by submit@debbugs.gnu.org id=B.167713955131684 (code B ref -1); Thu, 23 Feb 2023 08:06:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 23 Feb 2023 08:05:51 +0000 Original-Received: from localhost ([127.0.0.1]:32851 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pV6bv-0008Ey-7j for submit@debbugs.gnu.org; Thu, 23 Feb 2023 03:05:51 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:60262) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pV6bs-0008Ep-Qy for submit@debbugs.gnu.org; Thu, 23 Feb 2023 03:05:49 -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 1pV6bo-0002W6-4Z for bug-gnu-emacs@gnu.org; Thu, 23 Feb 2023 03:05:46 -0500 Original-Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pV6bl-00066n-Qs for bug-gnu-emacs@gnu.org; Thu, 23 Feb 2023 03:05:43 -0500 Original-Received: by mail-ed1-x52c.google.com with SMTP id b12so39244306edd.4 for ; Thu, 23 Feb 2023 00:05:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=XuhPDwjpmMDpWUYuubi333Pl15q/yvqjRidB+mJTjQM=; b=U7TFeyBCSOo3s3shoys4/XHcWy5N8WOMugP8CZMXDeBafYM9ESqGOQC3WpNbNpL1J2 GF5giPejhIPzpRrBJrtvWI6RccKMdHlf2p38T6U349lFd1hEQ40/ScEaluP3DTi2IZ2e repznMFdp6569UrslpkF/wLBmcgv+bk7N9b5EZMhnXnOGDTuawnOlpblWO2ZU1eiAXfN 4nJ4gDL/IfiFUe1tBIu/4om1ADsNxOvi0xhUlR2DekfJJLv5MfJTN3aPvZKSzXiQpTlq 8XyNUDWqKGY9WlQmX/Kws+8+i+q7nJIuRVEa4rxBSVKAAvZ2h0WtcEuoIWeLjdREqITe cOSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XuhPDwjpmMDpWUYuubi333Pl15q/yvqjRidB+mJTjQM=; b=0CzupM6mDgd0kpZQH2yXl86NdwQWVmUh0Vd3ActPSsf9itmIqu9Ipycwr7G80xu8eS uJkMv8km5wuqG9KL6s3Fqsscz8/lI4yYVVwcZ0pydXSLUSQkHopG84Uwo7lImPjCunMh 4VuBK5lmUu+hUOODKS+zr6y0tdB76Hn1p3KEn/evF1WOfLbYzYpDULnkVKxiA14O4/Ap gdnAsThCjv5av6S7Mc/10biErO5eGt8azeX171qnbdYjnIpTC5AEaB5h31+nBtbI3XRO +9SvG375nSHwPQ1jMmEAvZFKPuqO2fgtbVPZH3U9/JTOZWYc4hzd0+N98a7U2ACRFbrW qB0Q== X-Gm-Message-State: AO0yUKUGNYRyMqAxd7uJOheX1Gcgl6f1L5Y70NQS06nRn4uJRIsnJAJS owJz8YxT5IGfcgjnZcJKZKBtNp+fLoI= X-Google-Smtp-Source: AK7set84F0p4lu41m84cNjk1uslLNxdk2TnEgOp3wVWZx9gO6ncwX9JTKJKEkyQ35cDPwbctJ9nsZw== X-Received: by 2002:a17:907:ea2:b0:888:b764:54e5 with SMTP id ho34-20020a1709070ea200b00888b76454e5mr24190390ejc.71.1677139537549; Thu, 23 Feb 2023 00:05:37 -0800 (PST) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::6fd0]) by smtp.gmail.com with ESMTPSA id w13-20020a1709064a0d00b008d1693c212csm5056512eju.8.2023.02.23.00.05.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 00:05:36 -0800 (PST) Received-SPF: pass client-ip=2a00:1450:4864:20::52c; envelope-from=arstoffel@gmail.com; helo=mail-ed1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:256425 Archived-At: --=-=-= Content-Type: text/plain Tags: patch There is a new LSP capability allowing the server and client to agree on a way to count character offsets. What do you think fo the attached patch? It expresses Eglot's preferences as counting character offsets, then byte offsets, then the UTF-16 nonsense, in that order. I would also suggest preparing the stage to eventually make `eglot-current-column-function' and `eglot-move-to-column-function' obsolete. For that, I suggest renaming - eglot-current-column -> eglot--current-column-utf-32 - eglot-lsp-abiding-column -> eglot--current-columns-utf-16 - eglot-move-to-column -> eglot--move-to-columns-utf-32 - eglot-move-to-lsp-abiding-column -> eglot--move-to-columns-utf-16 and then making the old names obsolete aliases of the new names. (I have tested the utf-32 case superficially, and not at all the utf-8. So this is a RFC.) --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-lisp-progmodes-eglot.el-Support-positionEncoding-cap.patch >From a86601f4e80dfbf21a84230433c431375e3012aa Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Thu, 23 Feb 2023 08:55:58 +0100 Subject: [PATCH] * lisp/progmodes/eglot.el: Support positionEncoding capability --- lisp/progmodes/eglot.el | 65 +++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index b569c03e8c2..0268fbf63a5 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) @@ -1439,20 +1442,26 @@ eglot--warn (let ((warning-minimum-level :error)) (display-warning 'eglot (apply #'format format args) :warning))) -(defun eglot-current-column () (- (point) (line-beginning-position))) +(defun eglot-current-column () + "Calculate current column, counting Unicode codepoints." + (- (point) (line-beginning-position))) + +(defun eglot--current-column-utf-8 () + "Calculate current column, counting bytes." + (- (position-bytes (point)) (position-bytes (line-beginning-position)))) -(defvar eglot-current-column-function #'eglot-lsp-abiding-column +(defvar eglot-current-column-function nil "Function to calculate the current column. This is the inverse operation of `eglot-move-to-column-function' (which see). It is a function of no arguments returning a column number. For buffers managed by -fully LSP-compliant servers, this should be set to -`eglot-lsp-abiding-column' (the default), and -`eglot-current-column' for all others.") +fully LSP-compliant servers, this should be nil. For others, it +can be set to `eglot-current-colum' or +`eglot--current-column-utf-8'.") (defun eglot-lsp-abiding-column (&optional lbp) - "Calculate current COLUMN as defined by the LSP spec. + "Calculate current column, counting UTF-16 code units as in the original LSP spec. LBP defaults to `line-beginning-position'." (/ (- (length (encode-coding-region (or lbp (line-beginning-position)) ;; Fix github#860 @@ -1462,13 +1471,19 @@ eglot-lsp-abiding-column (defun eglot--pos-to-lsp-position (&optional pos) "Convert point POS to LSP position." - (eglot--widening - ;; LSP line is zero-origin; emacs is one-origin. - (list :line (1- (line-number-at-pos pos t)) - :character (progn (when pos (goto-char pos)) - (funcall eglot-current-column-function))))) - -(defvar eglot-move-to-column-function #'eglot-move-to-lsp-abiding-column + (let ((columnfn (or eglot-current-column-function + (pcase (plist-get (eglot--capabilities (eglot-current-server)) + :positionEncoding) + ("utf-32" #'eglot-current-column) + ("utf-8" #'eglot--current-column-utf-8) + (_ #'eglot-lsp-abiding-column))))) + (eglot--widening + ;; LSP line is zero-origin; emacs is one-origin. + (list :line (1- (line-number-at-pos pos t)) + :character (progn (when pos (goto-char pos)) + (funcall columnfn)))))) + +(defvar eglot-move-to-column-function nil "Function to move to a column reported by the LSP server. According to the standard, LSP column/character offsets are based @@ -1478,11 +1493,11 @@ eglot-move-to-column-function `c'. However, many servers don't follow the spec this closely. For buffers managed by fully LSP-compliant servers, this should -be set to `eglot-move-to-lsp-abiding-column' (the default), and -`eglot-move-to-column' for all others.") +be letft nil. For others, it can be set to +`eglot-move-to-column' or `eglot--move-to-column-utf-8'.") (defun eglot-move-to-column (column) - "Move to COLUMN without closely following the LSP spec." + "Move to COLUMN, counting Unicode codepoints." ;; We cannot use `move-to-column' here, because it moves to *visual* ;; columns, which can be different from LSP columns in case of ;; `whitespace-mode', `prettify-symbols-mode', etc. (github#296, @@ -1490,8 +1505,14 @@ eglot-move-to-column (goto-char (min (+ (line-beginning-position) column) (line-end-position)))) +(defun eglot--move-to-column-utf-8 (column) + "Move to COLUMN, regarded as a byte offset." + (goto-char (min (byte-to-position + (+ (position-bytes (line-beginning-position)) column)) + (line-end-position)))) + (defun eglot-move-to-lsp-abiding-column (column) - "Move to COLUMN abiding by the LSP spec." + "Move to COLUMN, counting UTF-16 code units as in the original LSP spec." (save-restriction (cl-loop with lbp = (line-beginning-position) @@ -1515,14 +1536,20 @@ eglot--lsp-position-to-point (forward-line (min most-positive-fixnum (plist-get pos-plist :line))) (unless (eobp) ;; if line was excessive leave point at eob - (let ((tab-width 1) + (let ((movefn (or eglot-move-to-column-function + (pcase (plist-get (eglot--capabilities (eglot-current-server)) + :positionEncoding) + ("utf-32" #'eglot-move-to-column) + ("utf-8" #'eglot--move-to-column-utf-8) + (_ #'eglot-move-to-lsp-abiding-column)))) + (tab-width 1) (col (plist-get pos-plist :character))) (unless (wholenump col) (eglot--warn "Caution: LSP server sent invalid character position %s. Using 0 instead." col) (setq col 0)) - (funcall eglot-move-to-column-function col))) + (funcall movefn col))) (if marker (copy-marker (point-marker)) (point))))) (defconst eglot--uri-path-allowed-chars -- 2.39.2 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable PS: Jo=C3=A3o, since you may have had trouble receiving some emails, did you notice bug#58141? --=-=-=--