]> code.delx.au - gnu-emacs-elpa/commitdiff
company-elisp-locals: Differentiate local function and variable bindings
authorDmitry Gutov <dgutov@yandex.ru>
Fri, 29 Mar 2013 07:48:03 +0000 (11:48 +0400)
committerDmitry Gutov <dgutov@yandex.ru>
Fri, 29 Mar 2013 07:48:03 +0000 (11:48 +0400)
company-elisp.el
company-tests.el

index b6e5961336a47a29fc7841da00cc40f22f51a960..42e3fef849d653341af1abe138c3ea83058ce8a3 100644 (file)
@@ -54,51 +54,63 @@ Functions are offered for completion only after ' and \(."
 (defvar company-elisp-parse-limit 30)
 (defvar company-elisp-parse-depth 100)
 
-(defvar company-elisp-binding-regexp
-  (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
-                                        "lambda" "lexical-let" "flet" "labels"))
-          "\\*?")
-  "Regular expression matching sexps containing variable bindings.")
+(defvar company-elisp-var-binding-regexp
+  (concat "\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
+                               "lambda" "lexical-let"))
+          "\\*?\\_>")
+  "Regular expression matching head of a multiple variable bindings form.")
 
-(defvar company-elisp-binding-regexp-1
-  (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
-  "Regular expression matching sexps containing one variable binding.")
+(defvar company-elisp-var-binding-regexp-1
+  (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("dolist" "dotimes")) "\\_>")
+  "Regular expression matching head of a form with one variable binding.")
 
-(defun company-elisp-locals (prefix)
+(defvar company-elisp-fun-binding-regexp
+  (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("flet" "labels")) "\\_>")
+  "Regular expression matching head of a function bindings form.")
+
+(defun company-elisp-locals (prefix functions-p)
   (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
                         "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
         (pos (point))
         res)
-    (ignore-errors
-      (save-excursion
-        (dotimes (i company-elisp-parse-depth)
-          (up-list -1)
-          (save-excursion
-            (cond
-             ((looking-at company-elisp-binding-regexp)
-              (down-list 2)
-              (ignore-errors
-                (dotimes (i company-elisp-parse-limit)
-                  (save-excursion
-                    (when (looking-at "[ \t\n]*(")
-                      (down-list 1))
-                    (and (looking-at regexp)
-                         ;; Don't add incomplete text as candidate.
-                         (not (eq (match-end 0) pos))
-                         (push (match-string-no-properties 1) res)))
-                  (forward-sexp))))
-             ((looking-at company-elisp-binding-regexp-1)
-              (down-list 2)
-              (and (looking-at regexp)
-                   ;; Don't add incomplete text as candidate.
-                   (not (eq (match-end 0) pos))
-                   (pushnew (match-string-no-properties 1) res))))))))
+    (condition-case nil
+        (save-excursion
+          (dotimes (i company-elisp-parse-depth)
+            (up-list -1)
+            (save-excursion
+              (when (eq (char-after) ?\()
+                (forward-char 1)
+                (skip-chars-forward " \t\n")
+                (cond
+                 ((looking-at (if functions-p
+                                  company-elisp-fun-binding-regexp
+                                company-elisp-var-binding-regexp))
+                  (down-list 1)
+                  (condition-case nil
+                      (dotimes (i company-elisp-parse-limit)
+                        (save-excursion
+                          (when (looking-at "[ \t\n]*(")
+                            (down-list 1))
+                          (and (looking-at regexp)
+                               ;; Don't add incomplete text as candidate.
+                               (not (eq (match-end 0) pos))
+                               (pushnew (match-string-no-properties 1) res)))
+                        (forward-sexp))
+                    (scan-error nil)))
+                 ((unless functions-p
+                    (looking-at company-elisp-var-binding-regexp-1))
+                  (down-list 1)
+                  (and (looking-at regexp)
+                       ;; Don't add incomplete text as candidate.
+                       (not (eq (match-end 0) pos))
+                       (pushnew (match-string-no-properties 1) res))))))))
+      (scan-error nil))
     res))
 
 (defun company-elisp-candidates (prefix)
-  (append (company-elisp-locals prefix)
-          (company-elisp-globals prefix
-                                 (company-elisp-candidates-predicate prefix))))
+  (let ((predicate (company-elisp-candidates-predicate prefix)))
+    (append (company-elisp-locals prefix (eq predicate 'fboundp))
+            (company-elisp-globals prefix predicate))))
 
 (defun company-elisp-globals (prefix predicate)
   (all-completions prefix obarray predicate))
index 30d898bc556dc00b9587ec98985e9b85a585663b..77ccee7e7f192dd35ef1dce2725d1b914f7cc589 100644 (file)
                   (company-elisp-candidates-predicate "b"))
                 'company-elisp-predicate))))
 
-;; Mix it up with an integration test.
+;; This one's also an integration test.
 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
   (company-elisp-with-buffer
     "(let ((foo 7) (wh| )))"
     (should (equal '("what" "whelp" "when")
                    (sort (company-elisp-globals "wh" 'company-elisp-predicate)
                          'string<)))))
+
+(ert-deftest company-elisp-locals-vars ()
+  (company-elisp-with-buffer
+    "(let ((foo 5) (bar 6))
+       (cl-labels ((borg ()))
+         (lambda (boo baz)
+           b|)))"
+    (should (equal '("bar" "baz" "boo")
+                   (company-elisp-locals "b" nil)))))
+
+(ert-deftest company-elisp-locals-single-var ()
+  (company-elisp-with-buffer
+    "(dotimes (itk 100)
+       (dolist (item items)
+         it|))"
+    (should (equal '("itk" "item")
+                   (company-elisp-locals "it" nil)))))
+
+(ert-deftest company-elisp-locals-funs ()
+  (company-elisp-with-buffer
+    "(cl-labels ((foo ())
+                 (fee ()))
+       (let ((fun 4))
+         (f| )))"
+    (should (equal '("fee" "foo")
+                   (sort (company-elisp-locals "f" t) 'string<)))))