1 ;;; company-semantic.el --- a company-mode back-end using CEDET Semantic
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; This file is part of company 0.3.
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.
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.
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/>.
21 (require 'semantic-ia)
22 (eval-when-compile (require 'cl))
24 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
29 (defun company-semantic-doc-or-summary (tag)
30 (or (semantic-documentation-for-tag tag)
31 (funcall semantic-idle-summary-function tag nil t)))
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)))
37 (string-match "\n*\\(.*\\)$" doc)
38 (setq doc (match-string 1 doc)))
39 (concat (funcall semantic-idle-summary-function tag nil t)
41 (if (< (+ (length doc) (length summary) 4) (window-width))
46 (defun company-semantic-doc-buffer (tag)
47 (let ((doc (semantic-documentation-for-tag tag)))
49 (with-current-buffer (company-doc-buffer)
50 (insert (funcall semantic-idle-summary-function tag nil t)
55 (defsubst company-semantic-completions (prefix)
57 (let ((completion-ignore-case nil)
58 (context (semantic-analyze-current-context)))
59 (all-completions prefix (semantic-ia-get-completions context (point))))))
62 (defun company-semantic (command &optional arg &rest ignored)
63 "A `company-mode' completion back-end using CEDET Semantic."
64 (interactive (list 'interactive))
66 ('interactive (company-begin-backend 'company-semantic))
67 ('prefix (and (memq major-mode '(c-mode c++-mode jde-mode java-mode))
69 (not (company-in-string-or-comment))
70 (company-grab-symbol)))
71 ('candidates (or (company-semantic-completions arg)
72 (mapcar 'semantic-tag-name
73 (semantic-analyze-find-tags-by-prefix arg))))
74 ('meta (funcall company-semantic-metadata-function
75 (semantic-analyze-find-tag arg)))
76 ('doc-buffer (company-semantic-doc-buffer (semantic-analyze-find-tag arg)))
77 ;; because "" is an empty context and doesn't return local variables
78 ('no-cache (equal arg ""))
79 ('location (let ((tag (semantic-analyze-find-tag arg)))
80 (when (buffer-live-p (semantic-tag-buffer tag))
81 (cons (semantic-tag-buffer tag)
82 (semantic-tag-start tag)))))))
84 (provide 'company-semantic)
85 ;;; company-semantic.el ends here