unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: npostavs@users.sourceforge.net
To: Thierry Volpiatto <thierry.volpiatto@gmail.com>
Cc: 27272@debbugs.gnu.org, "Johan Bockgård" <bojohan@gnu.org>
Subject: bug#27272: 25.2; [patch] Fix positional args among keyword args in eldoc.
Date: Wed, 19 Jul 2017 22:22:04 -0400	[thread overview]
Message-ID: <87shhralv7.fsf@users.sourceforge.net> (raw)
In-Reply-To: <CAM-tV--veEuG67MR=aABiZqa7SJB03ybTKdESg4=GRt2Dn0sXA@mail.gmail.com> (Noam Postavsky's message of "Sat, 1 Jul 2017 17:00:19 -0400")

[-- Attachment #1: Type: text/plain, Size: 619 bytes --]

Noam Postavsky <npostavs@users.sourceforge.net> writes:

> On Fri, Jun 30, 2017 at 2:36 AM, Thierry Volpiatto
> <thierry.volpiatto@gmail.com> wrote:
>>
>> This patch is an adaptation of what I use in my .emacs.el, originally
>> the patch was correct, I have then deleted it (because the patch was
>> refused) and then created it again on your request, forgetting to merge
>> one line; Use let* instead of let.
>
> Thanks, we will have to test the patch to make sure it works since it
> hasn't been tested in isolation then.

I've added a commit message and tests, I will push in a few days if there
are no objections.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 3767 bytes --]

From 27ddcf55804d4e852b2572ec4b7cab7dacecc5d6 Mon Sep 17 00:00:00 2001
From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
Date: Thu, 15 Jun 2017 05:26:05 +0200
Subject: [PATCH v3 1/2] Fix eldoc highlighting for &key args (Bug#27272)

* lisp/progmodes/elisp-mode.el (elisp--highlight-function-argument):
Only switch to keyword-based searching if INDEX is greater than the
index of `&key' in the argument list.  All arguments prior to the
`&key' are position based.  Additionally, be more strict about what is
a keyword when searching for the current keyword.
---
 lisp/progmodes/elisp-mode.el | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index b3f452ca5b..c70d5baffe 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -1394,13 +1394,14 @@ (defun elisp--highlight-function-argument (sym args index prefix)
   ;; FIXME: This should probably work on the list representation of `args'
   ;; rather than its string representation.
   ;; FIXME: This function is much too long, we need to split it up!
-  (let ((start          nil)
-	(end            0)
-	(argument-face  'eldoc-highlight-function-argument)
-        (args-lst (mapcar (lambda (x)
-                            (replace-regexp-in-string
-                             "\\`[(]\\|[)]\\'" "" x))
-                          (split-string args))))
+  (let* ((start          nil)
+         (end            0)
+         (argument-face  'eldoc-highlight-function-argument)
+         (args-lst (mapcar (lambda (x)
+                             (replace-regexp-in-string
+                              "\\`[(]\\|[)]\\'" "" x))
+                           (split-string args)))
+         (start-key-pos (cl-position "&key" args-lst :test 'equal)))
     ;; Find the current argument in the argument string.  We need to
     ;; handle `&rest' and informal `...' properly.
     ;;
@@ -1412,11 +1413,12 @@ (defun elisp--highlight-function-argument (sym args index prefix)
     ;; When `&key' is used finding position based on `index'
     ;; would be wrong, so find the arg at point and determine
     ;; position in ARGS based on this current arg.
-    (when (string-match "&key" args)
+    (when (and start-key-pos
+               (> index start-key-pos))
       (let* (case-fold-search
              key-have-value
              (sym-name (symbol-name sym))
-             (cur-w (current-word))
+             (cur-w (current-word t))
              (args-lst-ak (cdr (member "&key" args-lst)))
              (limit (save-excursion
                       (when (re-search-backward sym-name nil t)
@@ -1425,7 +1427,7 @@ (defun elisp--highlight-function-argument (sym args index prefix)
                         (substring cur-w 1)
                       (save-excursion
                         (let (split)
-                          (when (re-search-backward ":\\([^()\n]*\\)" limit t)
+                          (when (re-search-backward ":\\([^ ()\n]*\\)" limit t)
                             (setq split (split-string (match-string 1) " " t))
                             (prog1 (car split)
                               (when (cdr split)
@@ -1437,7 +1439,7 @@ (defun elisp--highlight-function-argument (sym args index prefix)
                                  args-lst-ak
                                  (not (member (upcase cur-a) args-lst-ak))
                                  (upcase (car (last args-lst-ak))))))
-        (unless (string= cur-w sym-name)
+        (unless (or (null cur-w) (string= cur-w sym-name))
           ;; The last keyword have already a value
           ;; i.e :foo a b and cursor is at b.
           ;; If signature have also `&rest'
-- 
2.11.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: patch --]
[-- Type: text/x-diff, Size: 3399 bytes --]

From 2e15fa20c441d5ead95a416e7fd67c525bc5fbaf Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@gmail.com>
Date: Wed, 19 Jul 2017 22:06:02 -0400
Subject: [PATCH v3 2/2] Add tests for previous commit

* test/lisp/progmodes/elisp-mode-tests.el
(elisp-mode-tests--face-propertized-string): New function.
(elisp--highlight-function-argument-indexed)
(elisp--highlight-function-argument-keyed-1)
(elisp--highlight-function-argument-keyed-2): New tests.
---
 test/lisp/progmodes/elisp-mode-tests.el | 56 +++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/test/lisp/progmodes/elisp-mode-tests.el b/test/lisp/progmodes/elisp-mode-tests.el
index ee0837f2c4..675aa31a79 100644
--- a/test/lisp/progmodes/elisp-mode-tests.el
+++ b/test/lisp/progmodes/elisp-mode-tests.el
@@ -24,6 +24,7 @@
 
 (require 'ert)
 (require 'xref)
+(eval-when-compile (require 'cl-lib))
 
 ;;; Completion
 
@@ -180,6 +181,61 @@ (ert-deftest eval-last-sexp-print-format-large-int-echo ()
         (call-interactively #'eval-last-sexp)
         (should (equal (current-message) "66 (#o102, #x42, ?B)"))))))
 
+;;; eldoc
+
+(defun elisp-mode-tests--face-propertized-string (string)
+  "Return substring of STRING with a non-nil `face' property."
+  (let* ((start (next-single-property-change 0 'face string))
+         (end (and start (next-single-property-change start 'face string))))
+    (and end
+         (substring string start end))))
+
+(ert-deftest elisp--highlight-function-argument-indexed ()
+  (dotimes (i 3)
+    (should
+     (equal (elisp-mode-tests--face-propertized-string
+             (elisp--highlight-function-argument 'foo "(A B C)" (1+ i) "foo: "))
+            (propertize (nth i '("A" "B" "C"))
+                        'face 'eldoc-highlight-function-argument)))))
+
+(ert-deftest elisp--highlight-function-argument-keyed-1 ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "(foo prompt bar :b 2)")
+    (goto-char (1+ (point-min)))
+    (cl-flet ((bold-arg (i)
+               (elisp-mode-tests--face-propertized-string
+                (elisp--highlight-function-argument
+                 'foo "(PROMPT LST &key A B C)" i "foo: "))))
+      (should-not (bold-arg 0))
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 1) "PROMPT"))
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 2) "LST"))
+      ;; Both `:b' and `2' should highlight the `B' arg.
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 3) "B"))
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 4) "B")))))
+
+(ert-deftest elisp--highlight-function-argument-keyed-2 ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "(foo :b :a 1)")
+    (goto-char (1+ (point-min)))
+    (cl-flet ((bold-arg (i)
+               (elisp-mode-tests--face-propertized-string
+                (elisp--highlight-function-argument
+                 'foo "(X &key A B C)" i "foo: "))))
+      (should-not (bold-arg 0))
+      ;; The `:b' specifies positional arg `X'.
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 1) "X"))
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 2) "A"))
+      (progn (forward-sexp) (forward-char))
+      (should (equal (bold-arg 3) "A")))))
+
 ;;; xref
 
 (defun xref-elisp-test-descr-to-target (xref)
-- 
2.11.1


  reply	other threads:[~2017-07-20  2:22 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-07  9:31 bug#27272: 25.2; [patch] Fix positional args among keyword args in eldoc Thierry Volpiatto
2017-06-07 15:33 ` Eli Zaretskii
2017-06-07 16:02   ` Thierry Volpiatto
2017-06-07 17:00     ` Eli Zaretskii
     [not found]       ` <87shjbegwy.fsf@gmail.com>
     [not found]         ` <83fufb8avt.fsf@gnu.org>
2017-06-08  8:17           ` Thierry Volpiatto
2017-06-08 15:04             ` Eli Zaretskii
2017-06-07 16:15   ` Dmitry Gutov
2017-06-10 16:15 ` Johan Bockgård
2017-06-11  8:08   ` Thierry Volpiatto
2017-06-15  0:05     ` npostavs
2017-06-15  3:26       ` Thierry Volpiatto
2017-06-30  4:01         ` npostavs
2017-06-30  6:36           ` Thierry Volpiatto
2017-07-01 21:00             ` Noam Postavsky
2017-07-20  2:22               ` npostavs [this message]
2017-08-05  1:35                 ` npostavs
2017-08-16  1:30                   ` npostavs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87shhralv7.fsf@users.sourceforge.net \
    --to=npostavs@users.sourceforge.net \
    --cc=27272@debbugs.gnu.org \
    --cc=bojohan@gnu.org \
    --cc=thierry.volpiatto@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).