]> code.delx.au - gnu-emacs-elpa/blob - company-semantic.el
Tag company-dabbrev-ignore-buffers with package-version
[gnu-emacs-elpa] / company-semantic.el
1 ;;; company-semantic.el --- company-mode completion backend using Semantic
2
3 ;; Copyright (C) 2009-2011, 2013-2016 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 (require 'company-template)
30 (require 'cl-lib)
31
32 (defvar semantic-idle-summary-function)
33 (declare-function semantic-documentation-for-tag "semantic/doc" )
34 (declare-function semantic-analyze-current-context "semantic/analyze")
35 (declare-function semantic-analyze-possible-completions "semantic/complete")
36 (declare-function semantic-analyze-find-tags-by-prefix "semantic/analyze/fcn")
37 (declare-function semantic-tag-class "semantic/tag")
38 (declare-function semantic-tag-name "semantic/tag")
39 (declare-function semantic-tag-start "semantic/tag")
40 (declare-function semantic-tag-buffer "semantic/tag")
41 (declare-function semantic-active-p "semantic")
42 (declare-function semantic-format-tag-prototype "semantic/format")
43
44 (defgroup company-semantic nil
45 "Completion backend using Semantic."
46 :group 'company)
47
48 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
49 "The function turning a semantic tag into doc information."
50 :type 'function)
51
52 (defcustom company-semantic-begin-after-member-access t
53 "When non-nil, automatic completion will start whenever the current
54 symbol is preceded by \".\", \"->\" or \"::\", ignoring
55 `company-minimum-prefix-length'.
56
57 If `company-begin-commands' is a list, it should include `c-electric-lt-gt'
58 and `c-electric-colon', for automatic completion right after \">\" and
59 \":\".")
60
61 (defcustom company-semantic-insert-arguments t
62 "When non-nil, insert function arguments as a template after completion."
63 :type 'boolean
64 :package-version '(company . "0.9.0"))
65
66 (defvar company-semantic-modes '(c-mode c++-mode jde-mode java-mode))
67
68 (defvar-local company-semantic--current-tags nil
69 "Tags for the current context.")
70
71 (defun company-semantic-documentation-for-tag (tag)
72 (when (semantic-tag-buffer tag)
73 ;; When TAG's buffer is unknown, the function below raises an error.
74 (semantic-documentation-for-tag tag)))
75
76 (defun company-semantic-doc-or-summary (tag)
77 (or (company-semantic-documentation-for-tag tag)
78 (and (require 'semantic-idle nil t)
79 (require 'semantic/idle nil t)
80 (funcall semantic-idle-summary-function tag nil t))))
81
82 (defun company-semantic-summary-and-doc (tag)
83 (let ((doc (company-semantic-documentation-for-tag tag))
84 (summary (funcall semantic-idle-summary-function tag nil t)))
85 (and (stringp doc)
86 (string-match "\n*\\(.*\\)$" doc)
87 (setq doc (match-string 1 doc)))
88 (concat summary
89 (when doc
90 (if (< (+ (length doc) (length summary) 4) (window-width))
91 " -- "
92 "\n"))
93 doc)))
94
95 (defun company-semantic-doc-buffer (tag)
96 (let ((doc (company-semantic-documentation-for-tag tag)))
97 (when doc
98 (company-doc-buffer
99 (concat (funcall semantic-idle-summary-function tag nil t)
100 "\n"
101 doc)))))
102
103 (defsubst company-semantic-completions (prefix)
104 (ignore-errors
105 (let ((completion-ignore-case nil)
106 (context (semantic-analyze-current-context)))
107 (setq company-semantic--current-tags
108 (semantic-analyze-possible-completions context 'no-unique))
109 (all-completions prefix company-semantic--current-tags))))
110
111 (defun company-semantic-completions-raw (prefix)
112 (setq company-semantic--current-tags nil)
113 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
114 (unless (eq (semantic-tag-class tag) 'include)
115 (push tag company-semantic--current-tags)))
116 (delete "" (mapcar 'semantic-tag-name company-semantic--current-tags)))
117
118 (defun company-semantic-annotation (argument tags)
119 (let* ((tag (assq argument tags))
120 (kind (when tag (elt tag 1))))
121 (cl-case kind
122 (function (let* ((prototype (semantic-format-tag-prototype tag nil nil))
123 (par-pos (string-match "(" prototype)))
124 (when par-pos (substring prototype par-pos)))))))
125
126 (defun company-semantic--prefix ()
127 (if company-semantic-begin-after-member-access
128 (company-grab-symbol-cons "\\.\\|->\\|::" 2)
129 (company-grab-symbol)))
130
131 ;;;###autoload
132 (defun company-semantic (command &optional arg &rest ignored)
133 "`company-mode' completion backend using CEDET Semantic."
134 (interactive (list 'interactive))
135 (cl-case command
136 (interactive (company-begin-backend 'company-semantic))
137 (prefix (and (featurep 'semantic)
138 (semantic-active-p)
139 (memq major-mode company-semantic-modes)
140 (not (company-in-string-or-comment))
141 (or (company-semantic--prefix) 'stop)))
142 (candidates (if (and (equal arg "")
143 (not (looking-back "->\\|\\." (- (point) 2))))
144 (company-semantic-completions-raw arg)
145 (company-semantic-completions arg)))
146 (meta (funcall company-semantic-metadata-function
147 (assoc arg company-semantic--current-tags)))
148 (annotation (company-semantic-annotation arg
149 company-semantic--current-tags))
150 (doc-buffer (company-semantic-doc-buffer
151 (assoc arg company-semantic--current-tags)))
152 ;; Because "" is an empty context and doesn't return local variables.
153 (no-cache (equal arg ""))
154 (duplicates t)
155 (location (let ((tag (assoc arg company-semantic--current-tags)))
156 (when (buffer-live-p (semantic-tag-buffer tag))
157 (cons (semantic-tag-buffer tag)
158 (semantic-tag-start tag)))))
159 (post-completion (let ((anno (company-semantic-annotation
160 arg company-semantic--current-tags)))
161 (when (and company-semantic-insert-arguments anno)
162 (insert anno)
163 (company-template-c-like-templatify (concat arg anno)))
164 ))))
165
166 (provide 'company-semantic)
167 ;;; company-semantic.el ends here