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