emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/dart-mode b4e592f 098/192: Add more search commands


From: ELPA Syncer
Subject: [nongnu] elpa/dart-mode b4e592f 098/192: Add more search commands
Date: Sun, 29 Aug 2021 11:01:58 -0400 (EDT)

branch: elpa/dart-mode
commit b4e592f86ed4efa31ed200ce8236265d91ca57b5
Author: Natalie Weizenbaum <nex342@gmail.com>
Commit: Natalie Weizenbaum <nex342@gmail.com>

    Add more search commands
---
 CHANGELOG.md |   9 ++++
 README.md    |  26 ++++++++--
 dart-mode.el | 157 ++++++++++++++++++++++++++++++++++++++---------------------
 3 files changed, 132 insertions(+), 60 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8691acf..bec44d2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,5 +9,14 @@
 * Added a `dart-find-refs` command bound to `C-c C-f` which shows a list of
   references to the Dart API under the cursor.
 
+* Added a `dart-find-member-decls` command bound to `C-c C-e` which shows a 
list
+  of declarations of members with a given name.
+
+* Added a `dart-find-top-level-decls` command bound to `C-c C-t` which shows a
+  list of declarations of top-level elements with a given name.
+
+* Added a `dart-find-member-refs` command bound to `C-c C-r` which shows a list
+  of references to members with a given name.
+
 * `dart-executable-path`'s default value is now set correctly even if the
   executable named `dart` on the user's path is a symlink or a wrapper script.
diff --git a/README.md b/README.md
index 46f3681..3fc7104 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,25 @@ you're there, though: any edits may corrupt your package 
cache!
 
 ### Search
 
-You can search for all references to the identifier under your cursor by
-pressing `C-c C-f`. This will show you everywhere a method, getter, or setter 
is
-called; everywhere a class is used as a type, constructed, or has static 
methods
-called on it; everywhere a named argument is passed; and so on.
+There are several ways to use the analyzer to search through your Dart code. 
All
+of these pop up a search results window listing every use with handy links to
+take you right to the code. Note that for large codebases, additional results
+may be added to these results pages as the analyzer finds them. But once you 
see
+the last "Found X results" line, you know for sure you're seeing everything!
+
+* You can search for all references to the identifier under your cursor by
+  pressing `C-c C-f`. This will show you everywhere a method, getter, or setter
+  is called; everywhere a class is used as a type, constructed, or has static
+  methods called on it; everywhere a named argument is passed; and so on.
+
+* You can search for all member declarations with a given name by pressing `C-c
+  C-e`. This will list all declarations within classes that have the name, but
+  not any declarations at the top level.
+
+* If you want to search for top-level declarations instead, you can press `C-c
+  C-t`.
+
+* If you want to find all *references to* members with a given name, you can
+  press `C-c C-r`. This will show you everywhere a member with that name is
+  called, even if it's in a dynamic context and the analyzer can't figure out
+  what it's referring to.
diff --git a/dart-mode.el b/dart-mode.el
index 7fb0a13..20be986 100644
--- a/dart-mode.el
+++ b/dart-mode.el
@@ -419,6 +419,9 @@ Any stderr is logged using dart-log. Returns nil if the 
exit code is non-0."
 (define-key dart-mode-map (kbd "C-c ?") 'dart-show-hover)
 (define-key dart-mode-map (kbd "C-c C-g") 'dart-goto)
 (define-key dart-mode-map (kbd "C-c C-f") 'dart-find-refs)
+(define-key dart-mode-map (kbd "C-c C-e") 'dart-find-member-decls)
+(define-key dart-mode-map (kbd "C-c C-r") 'dart-find-member-refs)
+(define-key dart-mode-map (kbd "C-c C-t") 'dart-find-top-level-decls)
 
 ;;; CC indentation support
 
@@ -1209,66 +1212,108 @@ minibuffer."
        ("includePotential" . ,(or include-potential json-false)))
      (lambda (response)
        (-when-let (result (cdr (assoc 'result response)))
-         (lexical-let* (buffer
-                        (search-id (cdr (assoc 'id result))))
-           (with-current-buffer-window
-            "*Dart Search*" nil nil
-            (setq buffer (current-buffer))
-
-            (lexical-let* ((element (cdr (assoc 'element result)))
-                           (name (cdr (assoc 'name element)))
-                           (location (cdr (assoc 'location element))))
+         (lexical-let* ((element (cdr (assoc 'element result)))
+                        (name (cdr (assoc 'name element)))
+                        (location (cdr (assoc 'location element))))
+           (dart--display-search-results
+            (cdr (assoc 'id result))
+            (lambda () 
               (insert "References to ")
               (insert-button
                name
                'action (lambda (_) (dart--goto-location location)))
-              (insert ":\n\n"))
-
-            (dart--analysis-server-subscribe
-             "search.results"
-             (lambda (event subscription)
-               (with-current-buffer buffer
-                 (dart--json-let event (id results (is-last isLast))
-                   (when (equal id search-id)
-                     (when (eq is-last t)
-                       (dart--analysis-server-unsubscribe subscription))
-
-                     (save-excursion
-                       (goto-char (point-max))
-                       (loop
-                        for result across results
-                        do (lexical-let* ((location (cdr (assoc 'location 
result)))
-                                          (path (cdr (assoc 'path result))))
-                             (let ((start (point))
-                                   (buffer-read-only nil))
-                               (dart--fontify-excursion '(compilation-info 
underline)
-                                 (when (cl-some
-                                        (lambda (element)
-                                          (equal (cdr (assoc 'kind element)) 
"CONSTRUCTOR"))
-                                        path)
-                                   (insert "new "))
-
-                                 (insert
-                                  (loop for element across path
-                                        unless (member (cdr (assoc 'kind 
element))
-                                                       '("COMPILATION_UNIT" 
"FILE" "LIBRARY" "PARAMETER"))
-                                        unless (string-empty-p (cdr (assoc 
'name element)))
-                                        collect (cdr (assoc 'name element)) 
into names
-                                        finally return (mapconcat 'identity 
(reverse names) ".")))
-
-                                 (make-text-button
-                                  start (point)
-                                  'action (lambda (_) (dart--goto-location 
location))))
-
-                               (let ((file (cdr (assoc 'file location)))
-                                     (line (cdr (assoc 'startLine location)))
-                                     (column (cdr (assoc 'startColumn 
location))))
-                                 (insert " " file ":"
-                                         (dart--face-string line 
'compilation-line-number) ":"
-                                         (dart--face-string column 
'compilation-column-number) ?\n))))))))))))
-
-               (select-window (get-buffer-window buffer))
-               (forward-line 2)))))))
+              (insert ":\n\n")))))))))
+
+(defun dart-find-member-decls (name)
+  "Find member declarations named NAME."
+  (interactive "sMember name: ")
+  (dart--find-by-name
+   "search.findMemberDeclarations" "name" name "Members named "))
+
+(defun dart-find-member-refs (name)
+  "Find member references named NAME."
+  (interactive "sMember name: ")
+  (dart--find-by-name
+   "search.findMemberReferences" "name" name "References to "))
+
+(defun dart-find-top-level-decls (name)
+  "Find top-level declarations named NAME."
+  (interactive "sDeclaration name: ")
+  (dart--find-by-name
+   "search.findTopLevelDeclarations" "pattern" name "Declarations matching "))
+
+(defun dart--find-by-name (method argument name header)
+  "A helper function for running an analysis server search for NAME.
+
+Calls the given analysis server METHOD passing NAME to the given
+ARGUMENT. Displays a header beginning with HEADER in the results."
+  (dart--analysis-server-send
+   method
+   (list (cons argument name))
+   (lexical-let ((name name) (header header))
+     (lambda (response)
+       (-when-let (result (cdr (assoc 'result response)))
+         (dart--display-search-results
+          (cdr (assoc 'id result))
+          (lambda () (insert header name ":\n\n"))))))))  
+
+(defun dart--display-search-results (search-id callback)
+  "Displays search results with the given SEARCH-ID.
+
+CALLBACK is called with no arguments in the search result buffer
+to add a header and otherwise prepare it for displaying results."
+  (lexical-let* (buffer (search-id search-id) beginning-of-results)
+    (with-current-buffer-window
+     "*Dart Search*" nil nil
+     (setq buffer (current-buffer))
+     (apply callback nil)
+     (setq beginning-of-results (point))
+
+     (dart--analysis-server-subscribe
+      "search.results"
+      (lambda (event subscription)
+        (with-current-buffer buffer
+          (dart--json-let event (id results (is-last isLast))
+            (when (equal id search-id)
+              (when (eq is-last t)
+                (dart--analysis-server-unsubscribe subscription))
+
+              (save-excursion
+                (goto-char (point-max))
+                (loop
+                 for result across results
+                 do (lexical-let* ((location (cdr (assoc 'location result)))
+                                   (path (cdr (assoc 'path result))))
+                      (let ((start (point))
+                            (buffer-read-only nil))
+                        (dart--fontify-excursion '(compilation-info underline)
+                          (when (cl-some
+                                 (lambda (element)
+                                   (equal (cdr (assoc 'kind element)) 
"CONSTRUCTOR"))
+                                 path)
+                            (insert "new "))
+
+                          (insert
+                           (loop for element across path
+                                 unless (member (cdr (assoc 'kind element))
+                                                '("COMPILATION_UNIT" "FILE" 
"LIBRARY" "PARAMETER"))
+                                 unless (string-empty-p (cdr (assoc 'name 
element)))
+                                 collect (cdr (assoc 'name element)) into names
+                                 finally return (mapconcat 'identity (reverse 
names) ".")))
+
+                          (make-text-button
+                           start (point)
+                           'action (lambda (_) (dart--goto-location 
location))))
+
+                        (let ((file (cdr (assoc 'file location)))
+                              (line (cdr (assoc 'startLine location)))
+                              (column (cdr (assoc 'startColumn location))))
+                          (insert " " file ":"
+                                  (dart--face-string line 
'compilation-line-number) ":"
+                                  (dart--face-string column 
'compilation-column-number) ?\n))))))))))))
+
+    (select-window (get-buffer-window buffer))
+    (goto-char beginning-of-results)))
 
 (defun dart--goto-location (location)
   "Sends the user to the analysis server LOCATION."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]