From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Dmitry Gutov Newsgroups: gmane.emacs.bugs Subject: bug#41531: 27.0.91; Better handle asynchronous eldoc backends Date: Tue, 26 May 2020 02:52:58 +0300 Message-ID: <4987863b-d390-5f87-eb1c-2cca4f4b7262@yandex.ru> References: <875zckuet9.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------F14F2C2007861A590D9DCEA9" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="5944"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Cc: Stefan Monnier , andreyk.mad@gmail.com To: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= , 41531@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue May 26 01:54:11 2020 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 1jdMv4-0001Tr-0I for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 26 May 2020 01:54:10 +0200 Original-Received: from localhost ([::1]:48126 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jdMv2-0006PY-D7 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 25 May 2020 19:54:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49354) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jdMuw-0006PQ-5s for bug-gnu-emacs@gnu.org; Mon, 25 May 2020 19:54:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59501) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jdMuv-0000tv-QS for bug-gnu-emacs@gnu.org; Mon, 25 May 2020 19:54:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jdMuv-0007EZ-OU for bug-gnu-emacs@gnu.org; Mon, 25 May 2020 19:54:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Dmitry Gutov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 25 May 2020 23:54:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41531 X-GNU-PR-Package: emacs Original-Received: via spool by 41531-submit@debbugs.gnu.org id=B41531.159045080127761 (code B ref 41531); Mon, 25 May 2020 23:54:01 +0000 Original-Received: (at 41531) by debbugs.gnu.org; 25 May 2020 23:53:21 +0000 Original-Received: from localhost ([127.0.0.1]:42814 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jdMu5-0007DV-Ly for submit@debbugs.gnu.org; Mon, 25 May 2020 19:53:21 -0400 Original-Received: from mail-wm1-f44.google.com ([209.85.128.44]:55515) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jdMu3-0007D8-R8 for 41531@debbugs.gnu.org; Mon, 25 May 2020 19:53:08 -0400 Original-Received: by mail-wm1-f44.google.com with SMTP id c71so1474084wmd.5 for <41531@debbugs.gnu.org>; Mon, 25 May 2020 16:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=uY7i8LZ608OAGt0/V8UNJ/NV0/inMYqlVYcTGmho2Uw=; b=nPRtAAGEHbMPgY039745srawVQ6BEeqfYmmQ8InOljYMn3ILtryqTRUojsslspg8I9 noxRJXBY86lv+/rX/yA2FZ0Sfu4hFQ9XjoMfz1fH+WBPWm1XwZOEwL7bXF0vTXWe3tYX H3/UPA7LvWa1Aj0iM6dQqYbU1G7tcxvxzlGIXfa0Pcflz2E5XVlKRsKzKCdeeLJXq1X9 vciggJW3pwOl1KcLMcqCTi+jypJp8WHSeqimxvjnlVcHRbUzmkBOKN+YyKqmYMg65sc1 TKs5loP84CN6toKtvkTWcSuSEdv47U1YDICM0VRyz2iJQha+YNrQhvt2Ca+GZQd3Xzgs ARvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-language; bh=uY7i8LZ608OAGt0/V8UNJ/NV0/inMYqlVYcTGmho2Uw=; b=IBKCYB8wNp5QbakOfJbMISujaNrIff2PHH+WZEff5+RdV16Pb8sT3XuARcQIdRYG2x Eio9xPuiLgVGSLJtkpeMElVs4qvn9iaypA2+W5lRbJLBNlYpsYEVZY+01Yym0fO5fJRc LM5UdbnoG8OKyWxOd+SueJGGk/I7S74daEQYhfF/EYd7FYPcPVpVXFQeJQ2yZSecVp3g p1Rb0TYDeveelKbzjEjNQ7sQtCuYASx85iNbUhr4Xfi2kuZ+SeGuPr5tttpUsLdPm5Rh n1aQk2M59bZEkN+IikJW53cMuyXxxI8kwg8D5qesns+Ki/txceWHLlmeK+xQqQPh/FGW +Iqg== X-Gm-Message-State: AOAM5324rQheL3JaWogNg/CdgEsETd2R8hwQ5+I4muWlPFnO/0hrWFTT 3HTDCFF/vZvOu0rcynIdyH4= X-Google-Smtp-Source: ABdhPJzqrFln9wf4mjYg2IkC7bjMj9m2sIe8l8vLvwJGc1ZqcAkYo0sA6o6bX6tkbMdhiZEuIDSDiA== X-Received: by 2002:a1c:7c0b:: with SMTP id x11mr28293442wmc.149.1590450780988; Mon, 25 May 2020 16:53:00 -0700 (PDT) Original-Received: from [192.168.0.3] ([66.205.73.129]) by smtp.googlemail.com with ESMTPSA id n19sm4633907wmi.33.2020.05.25.16.52.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 May 2020 16:53:00 -0700 (PDT) In-Reply-To: <875zckuet9.fsf@gmail.com> Content-Language: en-US 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" Xref: news.gmane.io gmane.emacs.bugs:181009 Archived-At: This is a multi-part message in MIME format. --------------F14F2C2007861A590D9DCEA9 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 25.05.2020 20:04, João Távora wrote: > Hi Stefan, Dmitry, Andrii and maintainers, > > Moving the discussion that started in > https://github.com/joaotavora/eglot/pull/459 to the bug tracker, and > attaching the two patches that contain what I think is a decent > short-term solution to the eldoc/async problems. Here's a modified approach that doesn't use a global var and should make it easier to transition to using futures. Patch attached. Example of usage: (add-hook 'eldoc-documentation-functions #'test-eldoc-async 0 t) (defun test-eldoc-async () (cons :async (lambda (cb) (funcall cb "doc here!")))) If you like, we could simplify the returned objects to be just FETCHER (as documented in the patch) rather than (:async . FETCHER). But the latter seems more explicit. There also exist a possible modification of this patch with a bit of functional programming where both calls to eldoc--handle-multiline happen from inside of eldoc-documentation-default's definition. --------------F14F2C2007861A590D9DCEA9 Content-Type: text/x-patch; charset=UTF-8; name="eldoc-async-with-lexical-callback.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="eldoc-async-with-lexical-callback.diff" diff --git a/lisp/emacs-lisp/eldoc.el b/lisp/emacs-lisp/eldoc.el index ef5dbf8103..8909e8d431 100644 --- a/lisp/emacs-lisp/eldoc.el +++ b/lisp/emacs-lisp/eldoc.el @@ -5,7 +5,7 @@ ;; Author: Noah Friedman ;; Keywords: extensions ;; Created: 1995-10-06 -;; Version: 1.0.0 +;; Version: 1.1.0 ;; Package-Requires: ((emacs "26.3")) ;; This is a GNU ELPA :core package. Avoid functionality that is not @@ -338,12 +338,26 @@ eldoc-display-message-no-interference-p (defvar eldoc-documentation-functions nil - "Hook for functions to call to return doc string. -Each function should accept no arguments and return a one-line -string for displaying doc about a function etc. appropriate to -the context around point. It should return nil if there's no doc -appropriate for the context. Typically doc is returned if point -is on a function-like name or in its arg list. + "Hook of functions that produce doc strings. +Each hook function should accept no arguments and decide whether +to display a doc short string about the context around point. + +Typically doc is returned if point is on a function-like name or +in its arg list. + +If the decision and the doc string can be produced quickly, the +hook function should immediately return the doc string, or nil if +there's no doc appropriate for the context. Otherwise, if its +computation is expensive or can't be performed directly, the +function can instead return a cons (:async . FETCHER) where where +FETCHER is a function of one argument, CALLBACK. When the result +arrives, FETCHER must call CALLBACK and pass it the doc string to +be displayed. + +A current limitation of the asynchronous case is that it is only +guaranteed to work correctly if the value of +`eldoc-documentation-function' (notice the singular) is +`eldoc-documentation-default'. Major modes should modify this hook locally, for example: (add-hook \\='eldoc-documentation-functions #\\='foo-mode-eldoc nil t) @@ -351,14 +365,18 @@ eldoc-documentation-functions taken into account if the major mode specific function does not return any documentation.") +(defun eldoc--handle-multiline (res) + "Helper for handling a bit of `eldoc-echo-area-use-multiline-p'." + (if eldoc-echo-area-use-multiline-p res + (truncate-string-to-width + res (1- (window-width (minibuffer-window)))))) + (defun eldoc-documentation-default () "Show first doc string for item at point. Default value for `eldoc-documentation-function'." (let ((res (run-hook-with-args-until-success 'eldoc-documentation-functions))) - (when res - (if eldoc-echo-area-use-multiline-p res - (truncate-string-to-width - res (1- (window-width (minibuffer-window)))))))) + (cond ((stringp res) (eldoc--handle-multiline res)) + (t res)))) (defun eldoc-documentation-compose () "Show multiple doc string results at once. @@ -368,13 +386,11 @@ eldoc-documentation-compose 'eldoc-documentation-functions (lambda (f) (let ((str (funcall f))) - (when str (push str res)) + (when (stringp str) (push str res)) nil))) (when res (setq res (mapconcat #'identity (nreverse res) ", ")) - (if eldoc-echo-area-use-multiline-p res - (truncate-string-to-width - res (1- (window-width (minibuffer-window)))))))) + (eldoc--handle-multiline res)))) (defcustom eldoc-documentation-function #'eldoc-documentation-default "Function to call to return doc string. @@ -417,11 +433,30 @@ eldoc-print-current-symbol-info ;; Erase the last message if we won't display a new one. (when eldoc-last-message (eldoc-message nil)) - (let ((non-essential t)) + (let ((non-essential t) + (buffer (current-buffer))) ;; Only keep looking for the info as long as the user hasn't ;; requested our attention. This also locally disables inhibit-quit. (while-no-input - (eldoc-message (funcall eldoc-documentation-function))))))) + (let* + ((waiting-for-callback t) + (callback + (lambda (string) + (with-current-buffer buffer + ;; JT@2020-05-25: Currently, we expect one single + ;; docstring from the client, we silently swallow + ;; anything the client unexpectedly gives us, + ;; including updates. This could change. + (when waiting-for-callback + (setq waiting-for-callback nil) + (eldoc-message (eldoc--handle-multiline string)))))) + (res + (funcall eldoc-documentation-function))) + (cond ((stringp res) (eldoc-message res)) + ((and (consp res) + (eq (car res) :async)) + (funcall (cdr res) callback)) + (t (eldoc-message nil))))))))) ;; If the entire line cannot fit in the echo area, the symbol name may be ;; truncated or eliminated entirely from the output to make room for the --------------F14F2C2007861A590D9DCEA9--