From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#60338: [PATCH] Add option to present server changes as diffs Date: Mon, 26 Dec 2022 13:42:04 +0000 Message-ID: <87ilhy1dub.fsf@posteo.net> 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="7590"; mail-complaints-to="usenet@ciao.gmane.io" To: 60338@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 26 14:43:23 2022 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 1p9nlC-0001oO-R6 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 26 Dec 2022 14:43:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p9nkz-0002RJ-5E; Mon, 26 Dec 2022 08:43:11 -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 1p9nkt-0002R3-0M for bug-gnu-emacs@gnu.org; Mon, 26 Dec 2022 08:43:04 -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 1p9nks-000282-Oy for bug-gnu-emacs@gnu.org; Mon, 26 Dec 2022 08:43:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p9nks-0002Yg-DI for bug-gnu-emacs@gnu.org; Mon, 26 Dec 2022 08:43:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 26 Dec 2022 13:43:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 60338 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16720621289771 (code B ref -1); Mon, 26 Dec 2022 13:43:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 26 Dec 2022 13:42:08 +0000 Original-Received: from localhost ([127.0.0.1]:50961 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p9njz-0002XU-KW for submit@debbugs.gnu.org; Mon, 26 Dec 2022 08:42:08 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:52456) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p9njx-0002XN-Tk for submit@debbugs.gnu.org; Mon, 26 Dec 2022 08:42:06 -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 1p9njx-0002Km-JS for bug-gnu-emacs@gnu.org; Mon, 26 Dec 2022 08:42:05 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p9nju-0001hf-BP for bug-gnu-emacs@gnu.org; Mon, 26 Dec 2022 08:42:04 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id F313F2400B2 for ; Mon, 26 Dec 2022 14:41:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1672062120; bh=bS8nNnXEL6mkVLVpquN/9XlkcPcxAp+u3lHMTwvpS5E=; h=From:To:Subject:Date:From; b=VhF5rNMIlavkdzA4oAq4B02mT+RXFk/mc3xmqgi6nF8cFPQxJGD9O5fEJBxGW1Id6 381FyAdrYi+dh83Q/1mUjPuEJfqcEHd/DAilzB4VngyizcfTWMZmQTDI2kWKWZC1GZ mw3NpKCK3RfaPEO90ibxNDmNx/NRP4kRZJ4VJZxU0r7dEksWTW5UP8XDGwcG07VhGP 9oWozC68naQmVLe5WSqqZK0InTjbrE8NJreUB25pnA7Na4/Rb94Yl4MSVD2Y/DVyHQ e5BVervNbPtVaLGiphDMk3xndzqmO/iXD5cL4EOCdCHg9FsMd+GnyN/E9oBB0D3doZ aRGrqoQY5Al2g== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Ngf8l45Ctz9rxG for ; Mon, 26 Dec 2022 14:41:59 +0100 (CET) Received-SPF: pass client-ip=185.67.36.65; envelope-from=philipk@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, 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:251904 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-CC-Debbugs: Jo=C3=A3o T=C3=A1vora I'd like to propose adding an option that makes server modifications by Eglot less invasive. The current behaviour is to make the changes directly in a buffer and open the remaining files to make the modifications in those as well (?). If `eglot-use-diffs' is enabled, all confirmations are prepared as patches in a pop-up buffer that the user can review and apply at will. To my knowledge there is no general `diff-apply-hunk' that will apply all the changes from a buffer, but that is a separate issue that can be fixed in a separate patch. (Note, I'm still testing emacs-29, so the patch was developed on that branch. But it should be applied to master) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment Content-Description: [PATCH] Add option to present server changes as diffs >From 88ba620b47b7987d203fc5b42716fa08be3b1d8f Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Mon, 26 Dec 2022 11:06:09 +0100 Subject: [PATCH] Add option to present server changes as diffs * lisp/progmodes/eglot.el (eglot-use-diffs): Add new user option. (eglot--apply-workspace-edit): Respect 'eglot-use-diffs'. * doc/misc/eglot.texi (Customizing Eglot): Document 'eglot-use-diffs'. --- doc/misc/eglot.texi | 7 ++++++ lisp/progmodes/eglot.el | 54 ++++++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/doc/misc/eglot.texi b/doc/misc/eglot.texi index 2aff038b9a..9150e1a879 100644 --- a/doc/misc/eglot.texi +++ b/doc/misc/eglot.texi @@ -955,6 +955,13 @@ Customizing Eglot to use Eglot in your @code{eglot-managed-mode-hook} or via some other mechanism. +@vindex eglot-use-diffs +@item eglot-use-diffs +If this option is enabled, any server modifications (renames, code +actions, refactoring, @dots{}) will be presented as diffs that the +user can selectively apply. The default is that all modifications are +made in place. + @vindex eglot-report-progress @cindex progress @item eglot-report-progress diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 15cb1b6fad..74472b25ce 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -111,6 +111,8 @@ (require 'ert) (require 'array) (require 'external-completion) +(require 'diff-mode) +(require 'diff) ;; ElDoc is preloaded in Emacs, so `require'-ing won't guarantee we are ;; using the latest version from GNU Elpa when we load eglot.el. Use an @@ -3160,6 +3162,11 @@ eglot--apply-text-edits (undo-amalgamate-change-group change-group) (progress-reporter-done reporter)))) +(defcustom eglot-use-diffs nil + "Non-nil means that server changes are presented as diffs." + :type 'boolean + :version "30.1") + (defun eglot--apply-workspace-edit (wedit &optional confirm) "Apply the workspace edit WEDIT. If CONFIRM, ask user first." (eglot--dbind ((WorkspaceEdit) changes documentChanges) wedit @@ -3175,18 +3182,41 @@ eglot--apply-workspace-edit ;; prefer documentChanges over changes. (cl-loop for (uri edits) on changes by #'cddr do (push (list (eglot--uri-to-path uri) edits) prepared))) - (if (or confirm - (cl-notevery #'find-buffer-visiting - (mapcar #'car prepared))) - (unless (y-or-n-p - (format "[eglot] Server wants to edit:\n %s\n Proceed? " - (mapconcat #'identity (mapcar #'car prepared) "\n "))) - (jsonrpc-error "User canceled server edit"))) - (cl-loop for edit in prepared - for (path edits version) = edit - do (with-current-buffer (find-file-noselect path) - (eglot--apply-text-edits edits version)) - finally (eldoc) (eglot--message "Edit successful!"))))) + (if eglot-use-diffs + (with-current-buffer (get-buffer-create " *Server Changes*") + (buffer-disable-undo (current-buffer)) + (let ((buffer-read-only t)) + (diff-mode)) + (let ((inhibit-read-only t) + (target (current-buffer))) + (erase-buffer) + (pcase-dolist (`(,path ,edits ,_) prepared) + (with-temp-buffer + (let ((diff (current-buffer))) + (with-temp-buffer + (insert-file-contents path) + (eglot--apply-text-edits edits) + (diff-no-select path (current-buffer) + nil t diff)) + (with-current-buffer target + (insert-buffer-substring diff)))))) + (setq-local buffer-read-only t) + (buffer-enable-undo (current-buffer)) + (goto-char (point-min)) + (pop-to-buffer (current-buffer)) + (font-lock-ensure)) + (if (or confirm + (cl-notevery #'find-buffer-visiting + (mapcar #'car prepared))) + (unless (y-or-n-p + (format "[eglot] Server wants to edit:\n %s\n Proceed? " + (mapconcat #'identity (mapcar #'car prepared) "\n "))) + (jsonrpc-error "User canceled server edit"))) + (cl-loop for edit in prepared + for (path edits version) = edit + do (with-current-buffer (find-file-noselect path) + (eglot--apply-text-edits edits version)) + finally (eldoc) (eglot--message "Edit successful!")))))) (defun eglot-rename (newname) "Rename the current symbol to NEWNAME." -- 2.35.1 --=-=-=--