]> code.delx.au - gnu-emacs-elpa/blob - company-semantic.el
Added compatibility for CEDET in Emacs 23.2.
[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.3.
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 (or (require 'semantic-ia nil t)
22 (require 'semantic/ia))
23 (eval-when-compile (require 'cl))
24
25 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
26 "*The function turning a semantic tag into doc information."
27 :group 'company
28 :type 'function)
29
30 (defvar company-semantic-modes '(c-mode c++-mode jde-mode java-mode))
31
32 (defun company-semantic-doc-or-summary (tag)
33 (or (semantic-documentation-for-tag tag)
34 (funcall semantic-idle-summary-function tag nil t)))
35
36 (defun company-semantic-summary-and-doc (tag)
37 (let ((doc (semantic-documentation-for-tag tag))
38 (summary (funcall semantic-idle-summary-function tag nil t)))
39 (and (stringp doc)
40 (string-match "\n*\\(.*\\)$" doc)
41 (setq doc (match-string 1 doc)))
42 (concat (funcall semantic-idle-summary-function tag nil t)
43 (when doc
44 (if (< (+ (length doc) (length summary) 4) (window-width))
45 " -- "
46 "\n"))
47 doc)))
48
49 (defun company-semantic-doc-buffer (tag)
50 (let ((doc (semantic-documentation-for-tag tag)))
51 (when doc
52 (with-current-buffer (company-doc-buffer)
53 (insert (funcall semantic-idle-summary-function tag nil t)
54 "\n"
55 doc)
56 (current-buffer)))))
57
58 (defsubst company-semantic-completions (prefix)
59 (ignore-errors
60 (let ((completion-ignore-case nil)
61 (context (semantic-analyze-current-context)))
62 (all-completions prefix (semantic-ia-get-completions context (point))))))
63
64 (defun company-semantic-completions-raw (prefix)
65 (let (candidates)
66 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
67 (unless (eq (semantic-tag-class tag) 'include)
68 (push (semantic-tag-name tag) candidates)))
69 (delete "" candidates)))
70
71 (defun company-semantic--pre-prefix-length (prefix-length)
72 "Sum up the length of all chained symbols before POS.
73 Symbols are chained by \".\" or \"->\"."
74 (save-excursion
75 (let ((pos (point)))
76 (goto-char (- (point) prefix-length))
77 (while (looking-back "->\\|\\.")
78 (goto-char (match-beginning 0))
79 (skip-syntax-backward "w_"))
80 (- pos (point)))))
81
82 (defun company-semantic--grab ()
83 "Grab the semantic prefix, but return everything before -> or . as length."
84 (let ((symbol (company-grab-symbol)))
85 (when symbol
86 (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
87
88 ;;;###autoload
89 (defun company-semantic (command &optional arg &rest ignored)
90 "A `company-mode' completion back-end using CEDET Semantic."
91 (interactive (list 'interactive))
92 (case command
93 ('interactive (company-begin-backend 'company-semantic))
94 ('prefix (and (memq major-mode company-semantic-modes)
95 (semantic-active-p)
96 (not (company-in-string-or-comment))
97 (or (company-semantic--grab) 'stop)))
98 ('candidates (if (and (equal arg "")
99 (not (looking-back "->\\|\\.")))
100 (company-semantic-completions-raw arg)
101 (company-semantic-completions arg)))
102 ('meta (funcall company-semantic-metadata-function
103 (semantic-analyze-find-tag arg)))
104 ('doc-buffer (company-semantic-doc-buffer (semantic-analyze-find-tag arg)))
105 ;; because "" is an empty context and doesn't return local variables
106 ('no-cache (equal arg ""))
107 ('location (let ((tag (semantic-analyze-find-tag arg)))
108 (when (buffer-live-p (semantic-tag-buffer tag))
109 (cons (semantic-tag-buffer tag)
110 (semantic-tag-start tag)))))))
111
112 (provide 'company-semantic)
113 ;;; company-semantic.el ends here