]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-semantic.el
Bumped version to 0.5.
[sojka/company-mode.git] / company-semantic.el
1 ;;; company-semantic.el --- a company-mode back-end using CEDET Semantic
2 ;;
3 ;; Copyright (C) 2009-2010 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.5.
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-analyze nil t)
22     (require 'semantic/analyze))
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       (and (require 'semantic-idle nil t)
38            (require 'semantic/idle nil t)
39            (funcall semantic-idle-summary-function tag nil t))))
40
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)))
44     (and (stringp doc)
45          (string-match "\n*\\(.*\\)$" doc)
46          (setq doc (match-string 1 doc)))
47     (concat (funcall semantic-idle-summary-function tag nil t)
48             (when doc
49                   (if (< (+ (length doc) (length summary) 4) (window-width))
50                       " -- "
51                     "\n"))
52             doc)))
53
54 (defun company-semantic-doc-buffer (tag)
55   (let ((doc (semantic-documentation-for-tag tag)))
56     (when doc
57       (with-current-buffer (company-doc-buffer)
58         (insert (funcall semantic-idle-summary-function tag nil t)
59                 "\n"
60                 doc)
61         (current-buffer)))))
62
63 (defsubst company-semantic-completions (prefix)
64   (ignore-errors
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))))
70
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)))
77
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 \"->\"."
81   (save-excursion
82     (let ((pos (point)))
83       (goto-char (- (point) prefix-length))
84       (while (looking-back "->\\|\\.")
85         (goto-char (match-beginning 0))
86         (skip-syntax-backward "w_"))
87       (- pos (point)))))
88
89 (defun company-semantic--grab ()
90   "Grab the semantic prefix, but return everything before -> or . as length."
91   (let ((symbol (company-grab-symbol)))
92     (when symbol
93       (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
94
95 ;;;###autoload
96 (defun company-semantic (command &optional arg &rest ignored)
97   "A `company-mode' completion back-end using CEDET Semantic."
98   (interactive (list 'interactive))
99   (case command
100     ('interactive (company-begin-backend 'company-semantic))
101     ('prefix (and (memq major-mode company-semantic-modes)
102                   (semantic-active-p)
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)))))))
119
120 (provide 'company-semantic)
121 ;;; company-semantic.el ends here