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: Fri, 24 Feb 2023 19:08:14 +0100 Message-ID: <87fsav7x81.fsf@gmail.com> References: <87a614g628.fsf@gmail.com> <83v8jspgnr.fsf@gnu.org> <87lekodxja.fsf@gmail.com> <83a614p4sh.fsf@gnu.org> <87cz60dus9.fsf@gmail.com> <835ybrpnqj.fsf@gnu.org> <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> 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="36136"; 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 Fri Feb 24 19:09:59 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 1pVcW6-0009Fz-Ks for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 24 Feb 2023 19:09:58 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVcVE-000554-Rj; Fri, 24 Feb 2023 13:09: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 1pVcVC-00053Q-Lr for bug-gnu-emacs@gnu.org; Fri, 24 Feb 2023 13:09: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 1pVcVC-0004yD-AH for bug-gnu-emacs@gnu.org; Fri, 24 Feb 2023 13:09:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pVcVB-0005jz-Nb for bug-gnu-emacs@gnu.org; Fri, 24 Feb 2023 13:09:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 24 Feb 2023 18:09:01 +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.167726210722027 (code B ref 61726); Fri, 24 Feb 2023 18:09:01 +0000 Original-Received: (at 61726) by debbugs.gnu.org; 24 Feb 2023 18:08:27 +0000 Original-Received: from localhost ([127.0.0.1]:38222 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pVcUc-0005jD-FK for submit@debbugs.gnu.org; Fri, 24 Feb 2023 13:08:26 -0500 Original-Received: from mail-ed1-f49.google.com ([209.85.208.49]:35641) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pVcUZ-0005iy-PJ for 61726@debbugs.gnu.org; Fri, 24 Feb 2023 13:08:24 -0500 Original-Received: by mail-ed1-f49.google.com with SMTP id ee7so700422edb.2 for <61726@debbugs.gnu.org>; Fri, 24 Feb 2023 10:08:23 -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=9SuM9iC3OKkKk4vHI8ZKiWnePZiw2pohd3QbEa+XgXg=; b=jVgyq+olT0KKFqkVBc1t8xDZbwwI/RSWIE0SvId+GXsoMtuYgdWYDV+L9MbhObWQ96 gA83VRfXy4vKN9bK1pWeITkCnzEpe8Ynazf4WS2LY707kIT7PfREDFqGqB5eYLMwBDNa LGVhVxlNSUsgeHEmqmARkKP6AMALlgWbHNo3tAnJDH0hH2P7p6QB/0Xxdn/wUNBIcmFW rMQzQm9A9A8KNswxGZDGMMYtvdjjTmnevmxHpQWygNtlG8pSvrHnFyARx4IrFhbTy0aQ j2K0ZSr3y+f3S2ptUZaI7fzhWxEm1i2GLqK9cEbt62IV/+ipC1YODfOWwUTwN96vZ33y DdAQ== 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=9SuM9iC3OKkKk4vHI8ZKiWnePZiw2pohd3QbEa+XgXg=; b=Ajf+xxDClklbX6fGzpnLQA1RPuBGoeEiOZ/7dA8sbOwKxCAmFBUuu0KnqTy19/PycZ +F4KLvAtNbX0AL8hYb5BeYrb7rqr0JCU+ePMOk2dDVfI9U5aZxyUvqaQMt8IM+1Eyo7V /OAh19XMZH8bAz37nYjMc0w/tL2/Ovj7yu34H2WJGqMYwVuLrHUNpKy76PoopNS90OpU JRlSFvF8hMNUYOqRVa5hXIbXBHgfksantevRthzFrXedUiW97Okq0tOwfcyOOKHI6iDg l60srFqXxz8jbbZFj321GPHoupybgeALb+Y7HBtHcgSbqgA7COIZ6kkY9VwqMWVzhFTS rOTA== X-Gm-Message-State: AO0yUKUaeV1wg+Xt43JrwkqAMiwf8zciyB0e4gl7PoAPkgbrbKryYZBt h+HHK6K2xd9wi/k0eI+hoQqRETWev7o= X-Google-Smtp-Source: AK7set9ScM6TNVfYRfu8DnlMozsh9edI4xDNThyFoqvPMCroiWQNaREdBvQAPEIuXttIfNjaGWWFhA== X-Received: by 2002:a17:907:970b:b0:8ee:72c0:6ba3 with SMTP id jg11-20020a170907970b00b008ee72c06ba3mr9063295ejc.7.1677262097373; Fri, 24 Feb 2023 10:08:17 -0800 (PST) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::6fd0]) by smtp.gmail.com with ESMTPSA id w5-20020a1709064a0500b008bbc4f3bceesm8696324eju.118.2023.02.24.10.08.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 10:08:16 -0800 (PST) In-Reply-To: <83v8jrm4r0.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 24 Feb 2023 18:01:55 +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:256649 Archived-At: --=-=-= Content-Type: text/plain I've attached a minimal functioning version of the patch. I would prefer to merge this and do all possible refinements discussed here at a later stage. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Eglot-support-positionEncoding-LSP-capability.patch >From 9b6794a729ac27b02472d68f849066c3a10297d4 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 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index b569c03e8c2..9a4d5733d0b 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,10 @@ eglot--warn (defun eglot-current-column () (- (point) (line-beginning-position))) +(defun eglot-bytewise-column () + "Calculate current column using the LSP `utf-8' criterion." + (- (position-bytes (point)) (position-bytes (line-beginning-position)))) + (defvar eglot-current-column-function #'eglot-lsp-abiding-column "Function to calculate the current column. @@ -1505,6 +1512,12 @@ 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." + (goto-char (min (byte-to-position + (+ (position-bytes (line-beginning-position)) column)) + (line-end-position)))) + (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 +1771,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 For the 2 new functions I've followed current naming scheme, replacing "lsp-abiding" by "bytewise". We can then get rid of all the suboptimal nomenclature (lsp-abiding, column, bytewise) in one go at a later stage. I also didn't attempt to make 'eglot-move-to-bytewise-column' and 'eglot-bytewise-column' "correct" in the cases where 'json-serialize' throws an error. I think it's clear that Eli and I have different definitions "correctness" in this context, but Eli's proposal requires substantial further work, including recovering from possible errors raising from 'json-serialize'. Lastly, I used 'line-beginning-position' naively in the 2 new function because there are at least 4 other pre-existing naive uses in eglot.el. This refinement deserves a patch of its own, probably using something on the lines of (defalias eglot--pos-bol (if (fboundp 'pos-bol) 'pos-bol ...)) --=-=-=--