Update copyright year to 2014 by running admin/update-copyright.
[bpt/emacs.git] / lisp / cedet / semantic / analyze / complete.el
1 ;;; semantic/analyze/complete.el --- Smart Completions
2
3 ;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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 ;;; Commentary:
23 ;;
24 ;; Calculate smart completions.
25 ;;
26 ;; Uses the analyzer context routine to determine the best possible
27 ;; list of completions.
28 ;;
29 ;;; History:
30 ;;
31 ;; Code was moved here from semantic/analyze.el
32
33 (require 'semantic/analyze)
34
35 ;; For semantic-find-* macros:
36 (eval-when-compile (require 'semantic/find))
37
38 ;;; Code:
39
40 ;;; Helper Fcns
41 ;;
42 ;;
43 ;;;###autoload
44 (define-overloadable-function semantic-analyze-type-constants (type)
45 "For the tag TYPE, return any constant symbols of TYPE.
46 Used as options when completing.")
47
48 (defun semantic-analyze-type-constants-default (type)
49 "Do nothing with TYPE."
50 nil)
51
52 (defun semantic-analyze-tags-of-class-list (tags classlist)
53 "Return the tags in TAGS that are of classes in CLASSLIST."
54 (let ((origc tags))
55 ;; Accept only tags that are of the datatype specified by
56 ;; the desired classes.
57 (setq tags (apply 'nconc ;; All input lists are permutable.
58 (mapcar (lambda (class)
59 (semantic-find-tags-by-class class origc))
60 classlist)))
61 tags))
62
63 ;;; MAIN completion calculator
64 ;;
65 ;;;###autoload
66 (define-overloadable-function semantic-analyze-possible-completions (context &rest flags)
67 "Return a list of semantic tags which are possible completions.
68 CONTEXT is either a position (such as point), or a precalculated
69 context. Passing in a context is useful if the caller also needs
70 to access parts of the analysis.
71 The remaining FLAGS arguments are passed to the mode specific completion engine.
72 Bad flags should be ignored by modes that don't use them.
73 See `semantic-analyze-possible-completions-default' for details on the default FLAGS.
74
75 Completions run through the following filters:
76 * Elements currently in scope
77 * Constants currently in scope
78 * Elements match the :prefix in the CONTEXT.
79 * Type of the completion matches the type of the context.
80 Context type matching can identify the following:
81 * No specific type
82 * Assignment into a variable of some type.
83 * Argument to a function with type constraints.
84 When called interactively, displays the list of possible completions
85 in a buffer."
86 (interactive "d")
87 ;; In theory, we don't need the below since the context will
88 ;; do it for us.
89 ;;(semantic-refresh-tags-safe)
90 (if (semantic-active-p)
91 (with-syntax-table semantic-lex-syntax-table
92 (let* ((context (if (semantic-analyze-context-child-p context)
93 context
94 (semantic-analyze-current-context context)))
95 (ans (if (not context)
96 (error "Nothing to complete")
97 (:override))))
98 ;; If interactive, display them.
99 (when (called-interactively-p 'any)
100 (with-output-to-temp-buffer "*Possible Completions*"
101 (semantic-analyze-princ-sequence ans "" (current-buffer)))
102 (shrink-window-if-larger-than-buffer
103 (get-buffer-window "*Possible Completions*")))
104 ans))
105 ;; Buffer was not parsed by Semantic.
106 ;; Raise error if called interactively.
107 (when (called-interactively-p 'any)
108 (error "Buffer was not parsed by Semantic."))))
109
110 (defun semantic-analyze-possible-completions-default (context &optional flags)
111 "Default method for producing smart completions.
112 Argument CONTEXT is an object specifying the locally derived context.
113 The optional argument FLAGS changes which return options are returned.
114 FLAGS can be any number of:
115 'no-tc - do not apply data-type constraint.
116 'no-unique - do not apply unique by name filtering."
117 (let* ((a context)
118 (desired-type (semantic-analyze-type-constraint a))
119 (desired-class (oref a prefixclass))
120 (prefix (oref a prefix))
121 (prefixtypes (oref a prefixtypes))
122 (completetext nil)
123 (completetexttype nil)
124 (scope (oref a scope))
125 (localvar (when scope (oref scope localvar)))
126 (origc nil)
127 (c nil)
128 (any nil)
129 (do-typeconstraint (not (memq 'no-tc flags)))
130 (do-unique (not (memq 'no-unique flags)))
131 )
132
133 ;; Calculate what our prefix string is so that we can
134 ;; find all our matching text.
135 (setq completetext (car (reverse prefix)))
136 (if (semantic-tag-p completetext)
137 (setq completetext (semantic-tag-name completetext)))
138
139 (if (and (not completetext) (not desired-type))
140 (error "Nothing to complete"))
141
142 (if (not completetext) (setq completetext ""))
143
144 ;; This better be a reasonable type, or we should fry it.
145 ;; The prefixtypes should always be at least 1 less than
146 ;; the prefix since the type is never looked up for the last
147 ;; item when calculating a sequence.
148 (setq completetexttype (car (reverse prefixtypes)))
149 (when (or (not completetexttype)
150 (not (and (semantic-tag-p completetexttype)
151 (eq (semantic-tag-class completetexttype) 'type))))
152 ;; What should I do here? I think this is an error condition.
153 (setq completetexttype nil)
154 ;; If we had something that was a completetexttype but it wasn't
155 ;; valid, then express our dismay!
156 (when (> (length prefix) 1)
157 (let* ((errprefix (car (cdr (reverse prefix)))))
158 (error "Cannot find types for `%s'"
159 (cond ((semantic-tag-p errprefix)
160 (semantic-format-tag-prototype errprefix))
161 (t
162 (format "%S" errprefix)))))
163 ))
164
165 ;; There are many places to get our completion stream for.
166 ;; Here we go.
167 (if completetexttype
168
169 (setq c (semantic-find-tags-for-completion
170 completetext
171 (semantic-analyze-scoped-type-parts completetexttype scope)
172 ))
173
174 ;; No type based on the completetext. This is a free-range
175 ;; var or function. We need to expand our search beyond this
176 ;; scope into semanticdb, etc.
177 (setq c (nconc
178 ;; Argument list and local variables
179 (semantic-find-tags-for-completion completetext localvar)
180 ;; The current scope
181 (semantic-find-tags-for-completion completetext (when scope (oref scope fullscope)))
182 ;; The world
183 (semantic-analyze-find-tags-by-prefix completetext))
184 )
185 )
186
187 (let ((loopc c)
188 (dtname (semantic-tag-name desired-type)))
189
190 ;; Save off our first batch of completions
191 (setq origc c)
192
193 ;; Reset c.
194 (setq c nil)
195
196 ;; Loop over all the found matches, and categorize them
197 ;; as being possible features.
198 (while (and loopc do-typeconstraint)
199
200 (cond
201 ;; Strip operators
202 ((semantic-tag-get-attribute (car loopc) :operator-flag)
203 nil
204 )
205
206 ;; If we are completing from within some prefix,
207 ;; then we want to exclude constructors and destructors
208 ((and completetexttype
209 (or (semantic-tag-get-attribute (car loopc) :constructor-flag)
210 (semantic-tag-get-attribute (car loopc) :destructor-flag)))
211 nil
212 )
213
214 ;; If there is a desired type, we need a pair of restrictions
215 (desired-type
216
217 (cond
218 ;; Ok, we now have a completion list based on the text we found
219 ;; we want to complete on. Now filter that stream against the
220 ;; type we want to search for.
221 ((string= dtname (semantic-analyze-type-to-name (semantic-tag-type (car loopc))))
222 (setq c (cons (car loopc) c))
223 )
224
225 ;; Now anything that is a compound type which could contain
226 ;; additional things which are of the desired type
227 ((semantic-tag-type (car loopc))
228 (let ((att (semantic-analyze-tag-type (car loopc) scope))
229 )
230 (if (and att (semantic-tag-type-members att))
231 (setq c (cons (car loopc) c))))
232 )
233
234 ) ; cond
235 ); desired type
236
237 ;; No desired type, no other restrictions. Just add.
238 (t
239 (setq c (cons (car loopc) c)))
240
241 ); cond
242
243 (setq loopc (cdr loopc)))
244
245 (when desired-type
246 ;; Some types, like the enum in C, have special constant values that
247 ;; we could complete with. Thus, if the target is an enum, we can
248 ;; find possible symbol values to fill in that value.
249 (let ((constants
250 (semantic-analyze-type-constants desired-type)))
251 (if constants
252 (progn
253 ;; Filter
254 (setq constants
255 (semantic-find-tags-for-completion
256 completetext constants))
257 ;; Add to the list
258 (setq c (nconc c constants)))
259 )))
260 )
261
262 (when desired-class
263 (setq c (semantic-analyze-tags-of-class-list c desired-class)))
264
265 (if do-unique
266 (if c
267 ;; Pull out trash.
268 ;; NOTE TO SELF: Is this too slow?
269 (setq c (semantic-unique-tag-table-by-name c))
270 (setq c (semantic-unique-tag-table-by-name origc)))
271 (when (not c)
272 (setq c origc)))
273
274 ;; All done!
275 c))
276
277 (provide 'semantic/analyze/complete)
278
279 ;; Local variables:
280 ;; generated-autoload-file: "../loaddefs.el"
281 ;; generated-autoload-load-name: "semantic/analyze/complete"
282 ;; End:
283
284 ;;; semantic/analyze/complete.el ends here