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