]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-semantic.el
(debbugs-emacs): New function and modes for listing the Emacs bugs, reading them...
[gnu-emacs-elpa] / packages / company-0.5 / company-semantic.el
1 ;;; company-semantic.el --- a company-mode back-end using CEDET Semantic
2
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 (require 'company)
25 (or (require 'semantic-analyze nil t)
26 (require 'semantic/analyze))
27 (eval-when-compile (require 'cl))
28
29 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
30 "*The function turning a semantic tag into doc information."
31 :group 'company
32 :type 'function)
33
34 (defvar company-semantic-modes '(c-mode c++-mode jde-mode java-mode))
35
36 (defvar company-semantic--current-tags nil
37 "Tags for the current context")
38
39 (defun company-semantic-doc-or-summary (tag)
40 (or (semantic-documentation-for-tag tag)
41 (and (require 'semantic-idle nil t)
42 (require 'semantic/idle nil t)
43 (funcall semantic-idle-summary-function tag nil t))))
44
45 (defun company-semantic-summary-and-doc (tag)
46 (let ((doc (semantic-documentation-for-tag tag))
47 (summary (funcall semantic-idle-summary-function tag nil t)))
48 (and (stringp doc)
49 (string-match "\n*\\(.*\\)$" doc)
50 (setq doc (match-string 1 doc)))
51 (concat (funcall semantic-idle-summary-function tag nil t)
52 (when doc
53 (if (< (+ (length doc) (length summary) 4) (window-width))
54 " -- "
55 "\n"))
56 doc)))
57
58 (defun company-semantic-doc-buffer (tag)
59 (let ((doc (semantic-documentation-for-tag tag)))
60 (when doc
61 (with-current-buffer (company-doc-buffer)
62 (insert (funcall semantic-idle-summary-function tag nil t)
63 "\n"
64 doc)
65 (current-buffer)))))
66
67 (defsubst company-semantic-completions (prefix)
68 (ignore-errors
69 (let ((completion-ignore-case nil)
70 (context (semantic-analyze-current-context)))
71 (setq company-semantic--current-tags
72 (semantic-analyze-possible-completions context))
73 (all-completions prefix company-semantic--current-tags))))
74
75 (defun company-semantic-completions-raw (prefix)
76 (setq company-semantic--current-tags nil)
77 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
78 (unless (eq (semantic-tag-class tag) 'include)
79 (push tag company-semantic--current-tags)))
80 (delete "" (mapcar 'semantic-tag-name company-semantic--current-tags)))
81
82 (defun company-semantic--pre-prefix-length (prefix-length)
83 "Sum up the length of all chained symbols before POS.
84 Symbols are chained by \".\" or \"->\"."
85 (save-excursion
86 (let ((pos (point)))
87 (goto-char (- (point) prefix-length))
88 (while (looking-back "->\\|\\.")
89 (goto-char (match-beginning 0))
90 (skip-syntax-backward "w_"))
91 (- pos (point)))))
92
93 (defun company-semantic--grab ()
94 "Grab the semantic prefix, but return everything before -> or . as length."
95 (let ((symbol (company-grab-symbol)))
96 (when symbol
97 (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
98
99 ;;;###autoload
100 (defun company-semantic (command &optional arg &rest ignored)
101 "A `company-mode' completion back-end using CEDET Semantic."
102 (interactive (list 'interactive))
103 (case command
104 ('interactive (company-begin-backend 'company-semantic))
105 ('prefix (and (memq major-mode company-semantic-modes)
106 (semantic-active-p)
107 (not (company-in-string-or-comment))
108 (or (company-semantic--grab) 'stop)))
109 ('candidates (if (and (equal arg "")
110 (not (looking-back "->\\|\\.")))
111 (company-semantic-completions-raw arg)
112 (company-semantic-completions arg)))
113 ('meta (funcall company-semantic-metadata-function
114 (assoc arg company-semantic--current-tags)))
115 ('doc-buffer (company-semantic-doc-buffer
116 (assoc arg company-semantic--current-tags)))
117 ;; because "" is an empty context and doesn't return local variables
118 ('no-cache (equal arg ""))
119 ('location (let ((tag (assoc arg company-semantic--current-tags)))
120 (when (buffer-live-p (semantic-tag-buffer tag))
121 (cons (semantic-tag-buffer tag)
122 (semantic-tag-start tag)))))))
123
124 (provide 'company-semantic)
125 ;;; company-semantic.el ends here