]> code.delx.au - gnu-emacs-elpa/blob - company-semantic.el
Don't use raw candidates list in member contexts.
[gnu-emacs-elpa] / company-semantic.el
1 ;;; company-semantic.el --- a company-mode back-end using CEDET Semantic
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.4.1.
6 ;;
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License
9 ;; as published by the Free Software Foundation; either version 2
10 ;; of the License, or (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 (require 'company)
21 (require 'semantic-ia)
22 (eval-when-compile (require 'cl))
23
24 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
25 "*The function turning a semantic tag into doc information."
26 :group 'company
27 :type 'function)
28
29 (defun company-semantic-doc-or-summary (tag)
30 (or (semantic-documentation-for-tag tag)
31 (funcall semantic-idle-summary-function tag nil t)))
32
33 (defun company-semantic-summary-and-doc (tag)
34 (let ((doc (semantic-documentation-for-tag tag))
35 (summary (funcall semantic-idle-summary-function tag nil t)))
36 (and (stringp doc)
37 (string-match "\n*\\(.*\\)$" doc)
38 (setq doc (match-string 1 doc)))
39 (concat (funcall semantic-idle-summary-function tag nil t)
40 (when doc
41 (if (< (+ (length doc) (length summary) 4) (window-width))
42 " -- "
43 "\n"))
44 doc)))
45
46 (defun company-semantic-doc-buffer (tag)
47 (let ((doc (semantic-documentation-for-tag tag)))
48 (when doc
49 (with-current-buffer (company-doc-buffer)
50 (insert (funcall semantic-idle-summary-function tag nil t)
51 "\n"
52 doc)
53 (current-buffer)))))
54
55 (defsubst company-semantic-completions (prefix)
56 (ignore-errors
57 (let ((completion-ignore-case nil)
58 (context (semantic-analyze-current-context)))
59 (all-completions prefix (semantic-ia-get-completions context (point))))))
60
61 (defun company-semantic-completions-raw (prefix)
62 (let (candidates)
63 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
64 (unless (eq (semantic-tag-class tag) 'include)
65 (push (semantic-tag-name tag) candidates)))
66 (delete "" candidates)))
67
68 (defun company-semantic--pre-prefix-length (prefix-length)
69 "Sum up the length of all chained symbols before POS.
70 Symbols are chained by \".\" or \"->\"."
71 (save-excursion
72 (let ((pos (point)))
73 (goto-char (- (point) prefix-length))
74 (while (looking-back "->\\|\\.")
75 (goto-char (match-beginning 0))
76 (skip-syntax-backward "w_"))
77 (- pos (point)))))
78
79 (defun company-semantic--grab ()
80 "Grab the semantic prefix, but return everything before -> or . as length."
81 (let ((symbol (company-grab-symbol)))
82 (when symbol
83 (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
84
85 ;;;###autoload
86 (defun company-semantic (command &optional arg &rest ignored)
87 "A `company-mode' completion back-end using CEDET Semantic."
88 (interactive (list 'interactive))
89 (case command
90 ('interactive (company-begin-backend 'company-semantic))
91 ('prefix (and (memq major-mode '(c-mode c++-mode jde-mode java-mode))
92 (semantic-active-p)
93 (not (company-in-string-or-comment))
94 (or (company-semantic--grab) 'stop)))
95 ('candidates (if (and (equal arg "")
96 (not (looking-back "->\\|\\.")))
97 (company-semantic-completions-raw arg)
98 (company-semantic-completions arg)))
99 ('meta (funcall company-semantic-metadata-function
100 (semantic-analyze-find-tag arg)))
101 ('doc-buffer (company-semantic-doc-buffer (semantic-analyze-find-tag arg)))
102 ;; because "" is an empty context and doesn't return local variables
103 ('no-cache (equal arg ""))
104 ('location (let ((tag (semantic-analyze-find-tag arg)))
105 (when (buffer-live-p (semantic-tag-buffer tag))
106 (cons (semantic-tag-buffer tag)
107 (semantic-tag-start tag)))))))
108
109 (provide 'company-semantic)
110 ;;; company-semantic.el ends here