1 ;;; company-semantic.el --- company-mode completion back-end using Semantic
3 ;; Copyright (C) 2009-2011, 2013 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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")
42 (defgroup company-semantic nil
43 "Completion back-end using Semantic."
46 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
47 "The function turning a semantic tag into doc information."
50 (defvar company-semantic-modes '(c-mode c++-mode jde-mode java-mode))
52 (defvar company-semantic--current-tags nil
53 "Tags for the current context.")
54 (make-variable-buffer-local 'company-semantic--current-tags)
56 (defun company-semantic-documentation-for-tag (tag)
57 (when (semantic-tag-buffer tag)
58 ;; When TAG's buffer is unknown, the function below raises an error.
59 (semantic-documentation-for-tag tag)))
61 (defun company-semantic-doc-or-summary (tag)
62 (or (company-semantic-documentation-for-tag tag)
63 (and (require 'semantic-idle nil t)
64 (require 'semantic/idle nil t)
65 (funcall semantic-idle-summary-function tag nil t))))
67 (defun company-semantic-summary-and-doc (tag)
68 (let ((doc (company-semantic-documentation-for-tag tag))
69 (summary (funcall semantic-idle-summary-function tag nil t)))
71 (string-match "\n*\\(.*\\)$" doc)
72 (setq doc (match-string 1 doc)))
75 (if (< (+ (length doc) (length summary) 4) (window-width))
80 (defun company-semantic-doc-buffer (tag)
81 (let ((doc (company-semantic-documentation-for-tag tag)))
84 (concat (funcall semantic-idle-summary-function tag nil t)
88 (defsubst company-semantic-completions (prefix)
90 (let ((completion-ignore-case nil)
91 (context (semantic-analyze-current-context)))
92 (setq company-semantic--current-tags
93 (semantic-analyze-possible-completions context))
94 (all-completions prefix company-semantic--current-tags))))
96 (defun company-semantic-completions-raw (prefix)
97 (setq company-semantic--current-tags nil)
98 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
99 (unless (eq (semantic-tag-class tag) 'include)
100 (push tag company-semantic--current-tags)))
101 (delete "" (mapcar 'semantic-tag-name company-semantic--current-tags)))
103 (defun company-semantic--pre-prefix-length (prefix-length)
104 "Sum up the length of all chained symbols before POS.
105 Symbols are chained by \".\" or \"->\"."
108 (goto-char (- (point) prefix-length))
109 (while (looking-back "->\\|\\.")
110 (goto-char (match-beginning 0))
111 (skip-syntax-backward "w_"))
114 (defun company-semantic--grab ()
115 "Grab the semantic prefix, but return everything before -> or . as length."
116 (let ((symbol (company-grab-symbol)))
118 (cons symbol (company-semantic--pre-prefix-length (length symbol))))))
121 (defun company-semantic (command &optional arg &rest ignored)
122 "`company-mode' completion back-end using CEDET Semantic."
123 (interactive (list 'interactive))
125 (interactive (company-begin-backend 'company-semantic))
126 (prefix (and (featurep 'semantic)
128 (memq major-mode company-semantic-modes)
129 (not (company-in-string-or-comment))
130 (or (company-semantic--grab) 'stop)))
131 (candidates (if (and (equal arg "")
132 (not (looking-back "->\\|\\.")))
133 (company-semantic-completions-raw arg)
134 (company-semantic-completions arg)))
135 (meta (funcall company-semantic-metadata-function
136 (assoc arg company-semantic--current-tags)))
137 (doc-buffer (company-semantic-doc-buffer
138 (assoc arg company-semantic--current-tags)))
139 ;; Because "" is an empty context and doesn't return local variables.
140 (no-cache (equal arg ""))
141 (location (let ((tag (assoc arg company-semantic--current-tags)))
142 (when (buffer-live-p (semantic-tag-buffer tag))
143 (cons (semantic-tag-buffer tag)
144 (semantic-tag-start tag)))))))
146 (provide 'company-semantic)
147 ;;; company-semantic.el ends here