From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Felician Nemeth Newsgroups: gmane.emacs.bugs Subject: bug#70193: [PATCH] Re: bug#70193: Acknowledgement (eglot: RFE: recenter buffer upon showDocument request) Date: Wed, 17 Apr 2024 11:15:25 +0200 Message-ID: <87sezkcpua.fsf_-_@betli.tmit.bme.hu> References: <1658f0ac-472f-4039-bf83-7abc31d77803@gmx.at> <878r1rgblr.fsf@betli.tmit.bme.hu> <4cdce8b3-66ea-4f16-b9ef-036466650eef@gmx.at> <867ch1r8g3.fsf@gnu.org> <8734rpmxup.fsf@betli.tmit.bme.hu> 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="13864"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Cc: martin rudalics , Eli Zaretskii , Alan Donovan , 70193@debbugs.gnu.org To: joaotavora@gmail.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Apr 17 11:15:58 2024 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 1rx1OW-0003Nk-V2 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 17 Apr 2024 11:15:57 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rx1OS-00057K-77; Wed, 17 Apr 2024 05:15:52 -0400 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 1rx1OQ-000579-VZ for bug-gnu-emacs@gnu.org; Wed, 17 Apr 2024 05:15:51 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rx1OQ-0000Do-No for bug-gnu-emacs@gnu.org; Wed, 17 Apr 2024 05:15:50 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rx1Od-0000VI-7j for bug-gnu-emacs@gnu.org; Wed, 17 Apr 2024 05:16:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Felician Nemeth Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 17 Apr 2024 09:16:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70193 X-GNU-PR-Package: emacs Original-Received: via spool by 70193-submit@debbugs.gnu.org id=B70193.17133453591916 (code B ref 70193); Wed, 17 Apr 2024 09:16:02 +0000 Original-Received: (at 70193) by debbugs.gnu.org; 17 Apr 2024 09:15:59 +0000 Original-Received: from localhost ([127.0.0.1]:45740 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rx1OU-0000U5-Bs for submit@debbugs.gnu.org; Wed, 17 Apr 2024 05:15:59 -0400 Original-Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]:61815) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rx1ON-0000SL-To for 70193@debbugs.gnu.org; Wed, 17 Apr 2024 05:15:52 -0400 Original-Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-5193363d255so1555956e87.3 for <70193@debbugs.gnu.org>; Wed, 17 Apr 2024 02:15:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713345327; x=1713950127; darn=debbugs.gnu.org; h=mime-version:face:user-agent:message-id:in-reply-to:date:references :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=IxxvMo/wb60uqJDU5LzSZsnkYxz8o7ICX+rjkNAhpZI=; b=YVM/tt7fxeC/N1TpiuayfinUJLA3Rx7waMNNdFXMJC2qYZuEoq4yTmFjqs/9a0DZJu fSMbo/HcmDldGP1w1wKMzWT31pZtniPhKOL0E7AAMaS95bgkinFGi/mohrR6VlNobCpw 3f4HraKKMp8+BNeBiu3sP7WnZZ1trb3c0xjtpO7eGyPNKGCrRAFBXAPUOHvhgFn2HiIX lDKNe9zu5E1dR04hTEfNBpFA62+jJFZvQNYtWWXd/Ukg07BNjFXCIUZYNeN4YKmuShIb Ps+OHFuuoYf2GEKBU7tHBK4DkFt5YMZXi2Qe6H4g22xgxznuYOfwTt1od8qoofB6IKOg V2DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713345327; x=1713950127; h=mime-version:face:user-agent:message-id:in-reply-to:date:references :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=IxxvMo/wb60uqJDU5LzSZsnkYxz8o7ICX+rjkNAhpZI=; b=HmzmsEZjxzAWoaN5SmgZPiEKymx1f/ya4XyS6YKorDvZ+qMgKvi3A4AvslGsI6UENi Ds+KoEwV5ohDJHgXjItNBodFIsfuoNNPQ3hyf8kzBWBoERM22PkQHPV+XdbRcZtg0yIZ eSl4xMoe/JhcpAcYi9StJwunWLnQOf47iM4E/0LCimgknFg3LCFjLjoMOEF6LVjl/cQm NdrOgb/+CiV2Co+SNiuMzpc9VZInVcAQyv4AliL8hGZdhikAhLber7Bi7WuHQccd1KBa QPwJthOO2bcH1MiNBaOC6EJGJbTbZjH7jWnCOqEgPHcmQrUuUFmqDgrH0OwTNFOQLgXx SY2A== X-Forwarded-Encrypted: i=1; AJvYcCWBMsoCXh6brUczwwhz7Vqr3GrKf0HSjdOslHIhBUa1K1JJEmlveXeQOb2erI4HpTHEAqInyWHd/ICPY8n4CGNzEnzZ2Qw= X-Gm-Message-State: AOJu0YyvYFO0aFh2IsBP51gI5eHaq6aDACpZ9n4XLN6aU9Q31Ue671Kk UPk/cy0NAODa+kQEEDdX7Z3T0yMWPq88KJHN+qebGQFeIg1iFo68FqJ+hA== X-Google-Smtp-Source: AGHT+IHam/U9YYk4OxTj1kqYbxp/w7OTebvNhVPzrxrgbjYgiYnYmY9vHqgX5E6hEfhQnHBDhG/vnQ== X-Received: by 2002:a2e:a58a:0:b0:2d8:144e:c464 with SMTP id m10-20020a2ea58a000000b002d8144ec464mr10534861ljp.36.1713345326916; Wed, 17 Apr 2024 02:15:26 -0700 (PDT) Original-Received: from betli.gmail.com (catv-213-222-160-137.catv.fixed.vodafone.hu. [213.222.160.137]) by smtp.gmail.com with ESMTPSA id g2-20020adff3c2000000b00343300a4eb8sm16994801wrp.49.2024.04.17.02.15.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 02:15:26 -0700 (PDT) In-Reply-To: (Alan Donovan's message of "Sat, 13 Apr 2024 12:36:41 -0400") Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAgMAAAAqbBEUAAAABGdBTUEAALGPC/xhBQAAACBj SFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAADFBMVEX5+fmhoaEwMDD/ ///TMNVWAAAAAWJLR0QDEQxM8gAAAAlwSFlzAAAPEgAADxIBIZvyMwAAAAd0SU1FB+AICBUfHgLs gGoAAAGXSURBVCjPRdK/b5tAFAfw753gBEwM2ApMbuVIqf+Ko0qiyhOu4sj2xJBYMn/FUdX7UUUZ OjHgyvf+yj6IcW6Bjx53934ADEvs8bmEr8UVoTYTOyJO9KoYsVofN8kILdbeJ8Li6YpZWop4xOK0 VdfIoXmkHn5/5D7/Ts/8THacSqnkKTcMTxgUkVzFnEIRTKwwYYSCvzfg16f0i8YApW/XG/Pm8R49 dXjxKmRnxv3OwooQWcv4RUYem1fsNe/WU63uk7AmYxk78y32/ee2tZB4fO+WcZ7lnIGEolXW1EGw LfkSuQ0XTgRefgNlfNwRNV6QhBxJ8JNxTMUPyBqTd0bjaAP5G7NJRU39z80hLOZTjqB7K3tEEFSj aEsuQew6qBxxyhHjVUR7H7NpC9iHJZGLMCEuweqAqE1BHbfK2oRIz9EHYA/+wiFWru9smeVfuWNZ 2+NFtX80UA1TvJNdytM4DwO4kY7bJz8Qcd0G0ceslZGkkeoBsjUHwF1+jjM3XHaXEZ7mGLfwPFO+ RV9QLY2iEdmDo78D/gNPaXVYqd+pyQAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxNi0wOC0wOFQyMzoz MDoyOCswMjowMGy/yHYAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTYtMDgtMDhUMjM6MzA6MjgrMDI6 MDAd4nDKAAAAAElFTkSuQmCC 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:283479 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Jo=C3=A3o, I've attached a patch fixing this issue. It includes Martin's new recenter-region. This is the original report: > In eglot 1.17, the LSP showDocument downcall opens the designated > file, moves the cursor to the designated position, and raises the > frame. One other thing it could do to make it easier to see where the > cursor is would be to recenter the buffer. >=20 > The patch below is a minimal fix; the discussion at > https://github.com/joaotavora/eglot/discussions/1382 suggests a couple > of possible refinements. Thanks. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Eglot-Recenter-buffer-upon-showDocument-request-bug-.patch >From 9496ee48c77f60c17387b4331911ba7ad5642c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felici=C3=A1n=20N=C3=A9meth?= Date: Wed, 17 Apr 2024 10:47:03 +0200 Subject: [PATCH] Eglot: Recenter buffer upon showDocument request (bug#70193) * lisp/progmodes/eglot.el (eglot--window-recenter-region): New defun. (eglot-handle-request window/showDocument): Try to show the whole selection centered; in prog modes, try to show preceding comments as well. Co-authored-by: Martin Rudalics --- lisp/progmodes/eglot.el | 91 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 5e4f7bba679..cbadd495b28 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -2488,6 +2488,76 @@ eglot-handle-request "Handle server request workspace/workspaceFolders." (eglot-workspace-folders server)) +(defun eglot--window-recenter-region (&optional from to window buffer) + "Center region and return window start position for recentered region. +WINDOW specifies the window whose start position to return and defaults +to the selected window. BUFFER specifies the buffer supposed to contain +the region and defaults to WINDOW's buffer. FROM and TO specify the +beginning and end of the region in BUFFER and default to the active +region or the entire text of BUFFER." + (let* ((window (or window (selected-window))) + (buffer (or buffer (window-buffer window))) + (from (or from + (and (eq buffer (current-buffer)) + (region-active-p) + (region-beginning)) + (with-current-buffer buffer + (point-min)))) + (to (or to + (and (eq buffer (current-buffer)) + (region-active-p) + (region-end)) + (with-current-buffer buffer + (point-max)))) + (body-width (window-body-width window t)) + (body-height (window-body-height window t)) + old-buffer old-point + remainder height start prev) + ;; If WINDOW doesn't show BUFFER, save its buffer, start and point + ;; positions. + (unless (eq (window-buffer window) buffer) + (setq old-buffer (window-buffer window)) + (setq old-point (window-point window)) + (set-window-buffer window buffer)) + + ;; The amount of pixels by which the region can be scrolled down in + ;; the window. Initially, half of the difference of the height of + ;; the window's body and that of the region. + (setq remainder + (round + (/ (- body-height + (cdr (window-text-pixel-size + window from to body-width body-height))) + 2.0))) + + ;; Now move back and subtract the height of one line preceding the + ;; region until the remainder has been used. + (save-excursion + (goto-char from) + (setq start (pos-bol)) + (while (and (not (bobp)) + (>= remainder 0) + (setq prev (pos-bol 0)) + (setq height (cdr (window-text-pixel-size + window prev start + body-width body-height))) + ;; At least half of the line should fit. + (>= remainder (/ height 2.0))) + (setq remainder (- remainder height)) + (setq start prev) + (goto-char start))) + + ;; Restore WINDOW's old buffer, start and point. + (when old-buffer + (set-window-buffer window old-buffer) + (set-window-point window old-point) + (set-window-start window old-point)) + + (set-window-start window start) + + ;; Return the new start position. + start)) + (cl-defmethod eglot-handle-request (_server (_method (eql window/showDocument)) &key uri external takeFocus selection) @@ -2510,10 +2580,23 @@ eglot-handle-request ((display-buffer (current-buffer)))) (when selection (pcase-let ((`(,beg . ,end) (eglot-range-region selection))) - ;; FIXME: it is very naughty to use someone else's `--' - ;; function, but `xref--goto-char' happens to have - ;; exactly the semantics we want vis-a-vis widening. - (xref--goto-char beg) + (with-selected-window (get-buffer-window (current-buffer)) + ;; FIXME: it is very naughty to use someone else's `--' + ;; function, but `xref--goto-char' happens to have + ;; exactly the semantics we want vis-a-vis widening. + (xref--goto-char end) + (xref--goto-char beg) + (eglot--window-recenter-region beg end) + + (when (derived-mode-p 'prog-mode) + (let ((recentered-visible (pos-visible-in-window-p end)) + (recentered-start (window-start))) + (reposition-window) ; Try to show preceding comments. + (when (and recentered-visible + (not (pos-visible-in-window-p end))) + ;; reposition-window ruined visibility of selection. + (set-window-start nil recentered-start))))) + (pulse-momentary-highlight-region beg end 'highlight))))))) (t (setq success :json-false))) `(:success ,success))) -- 2.39.2 --=-=-=--