From 7aab47898933c308873742a568c3fca14b2a7327 Mon Sep 17 00:00:00 2001 From: Nikolaj Schumacher Date: Wed, 22 Apr 2009 14:44:55 +0200 Subject: [PATCH] Added option to return separate prefix and prefix length. --- company-semantic.el | 19 ++++++++++++++++++- company.el | 18 +++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/company-semantic.el b/company-semantic.el index ab28541a4..3118e61bb 100644 --- a/company-semantic.el +++ b/company-semantic.el @@ -65,6 +65,23 @@ (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 diff --git a/company.el b/company.el index 793af1a47..7bbf4e5b0 100644 --- a/company.el +++ b/company.el @@ -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) -- 2.39.2