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