1 ;;; company-semantic.el --- a company-mode back-end using CEDET Semantic
3 ;; Copyright (C) 2009-2010 Nikolaj Schumacher
5 ;; This file is part of company 0.4.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 (or (require 'semantic-analyze nil t)
22 (require 'semantic/analyze))
23 (eval-when-compile (require 'cl))
25 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
26 "*The function turning a semantic tag into doc information."
30 (defvar company-semantic-modes '(c-mode c++-mode jde-mode java-mode))
32 (defvar company-semantic--current-tags nil
33 "Tags for the current context")
35 (defun company-semantic-doc-or-summary (tag)
36 (or (semantic-documentation-for-tag tag)
37 (and (require 'semantic-idle nil t)
38 (require 'semantic/idle nil t)
39 (funcall semantic-idle-summary-function tag nil t))))
41 (defun company-semantic-summary-and-doc (tag)
42 (let ((doc (semantic-documentation-for-tag tag))
43 (summary (funcall semantic-idle-summary-function tag nil t)))
45 (string-match "\n*\\(.*\\)$" doc)
46 (setq doc (match-string 1 doc)))
47 (concat (funcall semantic-idle-summary-function tag nil t)
49 (if (< (+ (length doc) (length summary) 4) (window-width))
54 (defun company-semantic-doc-buffer (tag)
55 (let ((doc (semantic-documentation-for-tag tag)))
57 (with-current-buffer (company-doc-buffer)
58 (insert (funcall semantic-idle-summary-function tag nil t)
63 (defsubst company-semantic-completions (prefix)
65 (let ((completion-ignore-case nil)
66 (context (semantic-analyze-current-context)))
67 (setq company-semantic--current-tags
68 (semantic-analyze-possible-completions context))
69 (all-completions prefix company-semantic--current-tags))))
71 (defun company-semantic-completions-raw (prefix)
72 (setq company-semantic--current-tags nil)
73 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
74 (unless (eq (semantic-tag-class tag) 'include)
75 (push tag company-semantic--current-tags)))
76 (delete "" (mapcar 'semantic-tag-name company-semantic--current-tags)))
78 (defun company-semantic--pre-prefix-length (prefix-length)
79 "Sum up the length of all chained symbols before POS.
80 Symbols are chained by \".\" or \"->\"."
83 (goto-char (- (point) prefix-length))
84 (while (looking-back "->\\|\\.")
85 (goto-char (match-beginning 0))
86 (skip-syntax-backward "w_"))
89 (defun company-semantic--grab ()
90 "Grab the semantic prefix, but return everything before -> or . as length."
91 (let ((symbol (company-grab-symbol)))
93 (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
96 (defun company-semantic (command &optional arg &rest ignored)
97 "A `company-mode' completion back-end using CEDET Semantic."
98 (interactive (list 'interactive))
100 ('interactive (company-begin-backend 'company-semantic))
101 ('prefix (and (memq major-mode company-semantic-modes)
103 (not (company-in-string-or-comment))
104 (or (company-semantic--grab) 'stop)))
105 ('candidates (if (and (equal arg "")
106 (not (looking-back "->\\|\\.")))
107 (company-semantic-completions-raw arg)
108 (company-semantic-completions arg)))
109 ('meta (funcall company-semantic-metadata-function
110 (assoc arg company-semantic--current-tags)))
111 ('doc-buffer (company-semantic-doc-buffer
112 (assoc arg company-semantic--current-tags)))
113 ;; because "" is an empty context and doesn't return local variables
114 ('no-cache (equal arg ""))
115 ('location (let ((tag (assoc arg company-semantic--current-tags)))
116 (when (buffer-live-p (semantic-tag-buffer tag))
117 (cons (semantic-tag-buffer tag)
118 (semantic-tag-start tag)))))))
120 (provide 'company-semantic)
121 ;;; company-semantic.el ends here