]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/analyze/complete.el
680a0ae65bd0e9be113487e4a2c6a61309273726
[gnu-emacs] / lisp / cedet / semantic / analyze / complete.el
1 ;;; semantic/analyze/complete.el --- Smart Completions
2
3 ;; Copyright (C) 2007-2015 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-longprefix' - ignore long multi-symbol prefixes.
117 `no-unique' - do not apply unique by name filtering."
118 (let* ((a context)
119 (desired-type (semantic-analyze-type-constraint a))
120 (desired-class (oref a prefixclass))
121 (prefix (oref a prefix))
122 (prefixtypes (oref a prefixtypes))
123 (completetext nil)
124 (completetexttype nil)
125 (scope (oref a scope))
126 (localvar (when scope (oref scope localvar)))
127 (origc nil)
128 (c nil)
129 (any nil)
130 (do-typeconstraint (not (memq 'no-tc flags)))
131 (do-longprefix (not (memq 'no-longprefix flags)))
132 (do-unique (not (memq 'no-unique flags)))
133 )
134
135 (when (not do-longprefix)
136 ;; If we are not doing the long prefix, shorten all the key
137 ;; elements.
138 (setq prefix (list (car (reverse prefix)))
139 prefixtypes nil))
140
141 ;; Calculate what our prefix string is so that we can
142 ;; find all our matching text.
143 (setq completetext (car (reverse prefix)))
144 (if (semantic-tag-p completetext)
145 (setq completetext (semantic-tag-name completetext)))
146
147 (if (and (not completetext) (not desired-type))
148 (error "Nothing to complete"))
149
150 (if (not completetext) (setq completetext ""))
151
152 ;; This better be a reasonable type, or we should fry it.
153 ;; The prefixtypes should always be at least 1 less than
154 ;; the prefix since the type is never looked up for the last
155 ;; item when calculating a sequence.
156 (setq completetexttype (car (reverse prefixtypes)))
157 (when (or (not completetexttype)
158 (not (and (semantic-tag-p completetexttype)
159 (eq (semantic-tag-class completetexttype) 'type))))
160 ;; What should I do here? I think this is an error condition.
161 (setq completetexttype nil)
162 ;; If we had something that was a completetexttype but it wasn't
163 ;; valid, then express our dismay!
164 (when (> (length prefix) 1)
165 (let* ((errprefix (car (cdr (reverse prefix)))))
166 (error "Cannot find types for `%s'"
167 (cond ((semantic-tag-p errprefix)
168 (semantic-format-tag-prototype errprefix))
169 (t
170 (format "%S" errprefix)))))
171 ))
172
173 ;; There are many places to get our completion stream for.
174 ;; Here we go.
175 (if completetexttype
176
177 (setq c (semantic-find-tags-for-completion
178 completetext
179 (semantic-analyze-scoped-type-parts completetexttype scope)
180 ))
181
182 ;; No type based on the completetext. This is a free-range
183 ;; var or function. We need to expand our search beyond this
184 ;; scope into semanticdb, etc.
185 (setq c (nconc
186 ;; Argument list and local variables
187 (semantic-find-tags-for-completion completetext localvar)
188 ;; The current scope
189 (semantic-find-tags-for-completion completetext (when scope (oref scope fullscope)))
190 ;; The world
191 (semantic-analyze-find-tags-by-prefix completetext))
192 )
193 )
194
195 (let ((loopc c)
196 (dtname (semantic-tag-name desired-type)))
197
198 ;; Save off our first batch of completions
199 (setq origc c)
200
201 ;; Reset c.
202 (setq c nil)
203
204 ;; Loop over all the found matches, and categorize them
205 ;; as being possible features.
206 (while (and loopc do-typeconstraint)
207
208 (cond
209 ;; Strip operators
210 ((semantic-tag-get-attribute (car loopc) :operator-flag)
211 nil
212 )
213
214 ;; If we are completing from within some prefix,
215 ;; then we want to exclude constructors and destructors
216 ((and completetexttype
217 (or (semantic-tag-get-attribute (car loopc) :constructor-flag)
218 (semantic-tag-get-attribute (car loopc) :destructor-flag)))
219 nil
220 )
221
222 ;; If there is a desired type, we need a pair of restrictions
223 (desired-type
224
225 (cond
226 ;; Ok, we now have a completion list based on the text we found
227 ;; we want to complete on. Now filter that stream against the
228 ;; type we want to search for.
229 ((string= dtname (semantic-analyze-type-to-name (semantic-tag-type (car loopc))))
230 (setq c (cons (car loopc) c))
231 )
232
233 ;; Now anything that is a compound type which could contain
234 ;; additional things which are of the desired type
235 ((semantic-tag-type (car loopc))
236 (let ((att (semantic-analyze-tag-type (car loopc) scope))
237 )
238 (if (and att (semantic-tag-type-members att))
239 (setq c (cons (car loopc) c))))
240 )
241
242 ) ; cond
243 ); desired type
244
245 ;; No desired type, no other restrictions. Just add.
246 (t
247 (setq c (cons (car loopc) c)))
248
249 ); cond
250
251 (setq loopc (cdr loopc)))
252
253 (when desired-type
254 ;; Some types, like the enum in C, have special constant values that
255 ;; we could complete with. Thus, if the target is an enum, we can
256 ;; find possible symbol values to fill in that value.
257 (let ((constants
258 (semantic-analyze-type-constants desired-type)))
259 (if constants
260 (progn
261 ;; Filter
262 (setq constants
263 (semantic-find-tags-for-completion
264 completetext constants))
265 ;; Add to the list
266 (setq c (nconc c constants)))
267 )))
268 )
269
270 (when desired-class
271 (setq c (semantic-analyze-tags-of-class-list c desired-class)))
272
273 (if do-unique
274 (if c
275 ;; Pull out trash.
276 ;; NOTE TO SELF: Is this too slow?
277 (setq c (semantic-unique-tag-table-by-name c))
278 (setq c (semantic-unique-tag-table-by-name origc)))
279 (when (not c)
280 (setq c origc)))
281
282 ;; All done!
283 c))
284
285 (provide 'semantic/analyze/complete)
286
287 ;; Local variables:
288 ;; generated-autoload-file: "../loaddefs.el"
289 ;; generated-autoload-load-name: "semantic/analyze/complete"
290 ;; End:
291
292 ;;; semantic/analyze/complete.el ends here