]> code.delx.au - gnu-emacs-elpa/commitdiff
Added option to return separate prefix and prefix length.
authorNikolaj Schumacher <git@nschum.de>
Wed, 22 Apr 2009 12:44:55 +0000 (14:44 +0200)
committerNikolaj Schumacher <git@nschum.de>
Thu, 23 Apr 2009 17:49:06 +0000 (19:49 +0200)
company-semantic.el
company.el

index ab28541a494230b7d183544a32a99962980bd4ac..3118e61bbd04f4226da5c8d1d1f9f9b15b343555 100644 (file)
         (push (semantic-tag-name tag) candidates)))
     (delete "" candidates)))
 
+(defun company-semantic--pre-prefix-length (prefix-length)
+  "Sum up the length of all chained symbols before POS.
+Symbols are chained by \".\" or \"->\"."
+  (save-excursion
+    (let ((pos (point)))
+      (goto-char (- (point) prefix-length))
+      (while (looking-back "->\\|\\.")
+        (goto-char (match-beginning 0))
+        (skip-syntax-backward "w_"))
+      (- pos (point)))))
+
+(defun company-semantic--grab ()
+  "Grab the semantic prefix, but return everything before -> or . as length."
+  (let ((symbol (company-grab-symbol)))
+    (when symbol
+      (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
+
 ;;;###autoload
 (defun company-semantic (command &optional arg &rest ignored)
   "A `company-mode' completion back-end using CEDET Semantic."
@@ -74,7 +91,7 @@
     ('prefix (and (memq major-mode '(c-mode c++-mode jde-mode java-mode))
                   (semantic-active-p)
                   (not (company-in-string-or-comment))
-                  (or (company-grab-symbol) 'stop)))
+                  (or (company-semantic--grab) 'stop)))
     ('candidates (or (company-semantic-completions arg)
                      (company-semantic-completions-raw arg)))
     ('meta (funcall company-semantic-metadata-function
index 793af1a4778e8a339cfd036a9015f3435fc8778e..7bbf4e5b070e1491ca98f1711feca80978e5bf68 100644 (file)
@@ -65,6 +65,7 @@
 ;;
 ;;; Change Log:
 ;;
+;;    In C modes . and -> now count towards `company-minimum-prefix-length'.
 ;;    Reverted default front-end back to `company-preview-if-just-one-frontend'.
 ;;    The pseudo tooltip will no longer be clipped at the right window edge.
 ;;    Added `company-tooltip-minimum'.
@@ -304,7 +305,10 @@ of the following:
 'prefix: The back-end should return the text to be completed.  It must be
 text immediately before `point'.  Returning nil passes control to the next
 back-end.  The function should return 'stop if it should complete but cannot
-\(e.g. if it is in the middle of a string\).
+\(e.g. if it is in the middle of a string\).  If the returned value is only
+part of the prefix (e.g. the part after \"->\" in C), the back-end may return a
+cons of prefix and prefix length, which is then used in the
+`company-minimum-prefix-length' test.
 
 'candidates: The second argument is the prefix to be completed.  The
 return value should be a list of candidates that start with the prefix.
@@ -910,9 +914,10 @@ keymap during active completions (`company-active-map'):
         nil)))))
 
 (defun company--good-prefix-p (prefix)
-  (and (stringp prefix)
-       (or (company-explicit-action-p)
-           (>= (length prefix) company-minimum-prefix-length))))
+  (and (or (company-explicit-action-p)
+           (>= (or (cdr-safe prefix) (length prefix))
+               company-minimum-prefix-length))
+       (stringp (or (car-safe prefix) prefix))))
 
 (defun company--continue ()
   (when (company-call-backend 'no-cache company-prefix)
@@ -920,8 +925,10 @@ keymap during active completions (`company-active-map'):
     (setq company-candidates-cache nil))
   (let* ((new-prefix (company-call-backend 'prefix))
          (c (when (and (company--good-prefix-p new-prefix)
+                       (setq new-prefix (or (car-safe new-prefix) new-prefix))
                        (= (- (point) (length new-prefix))
                           (- company-point (length company-prefix))))
+              (setq new-prefix (or (car-safe new-prefix) new-prefix))
               (company-calculate-candidates new-prefix))))
     (or (cond
          ((eq c t)
@@ -953,7 +960,8 @@ keymap during active completions (`company-active-map'):
               (company--multi-backend-adapter backend 'prefix)))
       (when prefix
         (when (company--good-prefix-p prefix)
-          (setq company-backend backend
+          (setq prefix (or (car-safe prefix) prefix)
+                company-backend backend
                 c (company-calculate-candidates prefix))
           ;; t means complete/unique.  We don't start, so no hooks.
           (when (consp c)