1 ;;; semantic/analyze/complete.el --- Smart Completions
3 ;; Copyright (C) 2007-2018 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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 <https://www.gnu.org/licenses/>.
24 ;; Calculate smart completions.
26 ;; Uses the analyzer context routine to determine the best possible
27 ;; list of completions.
31 ;; Code was moved here from semantic/analyze.el
33 (require 'semantic
/analyze
)
35 ;; For semantic-find-* macros:
36 (eval-when-compile (require 'semantic
/find
))
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.")
48 (defun semantic-analyze-type-constants-default (type)
49 "Do nothing with TYPE."
52 (defun semantic-analyze-tags-of-class-list (tags classlist
)
53 "Return the tags in TAGS that are of classes in CLASSLIST."
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
))
63 ;;; MAIN completion calculator
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.
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:
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
87 ;; In theory, we don't need the below since the context will
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
)
94 (semantic-analyze-current-context context
)))
95 (ans (if (not context
)
96 (error "Nothing to complete")
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*")))
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."))))
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."
119 (desired-type (semantic-analyze-type-constraint a
))
120 (desired-class (oref a prefixclass
))
121 (prefix (oref a prefix
))
122 (prefixtypes (oref a prefixtypes
))
124 (completetexttype nil
)
125 (scope (oref a scope
))
126 (localvar (when scope
(oref scope localvar
)))
130 (do-typeconstraint (not (memq 'no-tc flags
)))
131 (do-longprefix (not (memq 'no-longprefix flags
)))
132 (do-unique (not (memq 'no-unique flags
)))
135 (when (not do-longprefix
)
136 ;; If we are not doing the long prefix, shorten all the key
138 (setq prefix
(list (car (reverse prefix
)))
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
)))
147 (if (and (not completetext
) (not desired-type
))
148 (error "Nothing to complete"))
150 (if (not completetext
) (setq completetext
""))
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
))
170 (format "%S" errprefix
)))))
173 ;; There are many places to get our completion stream for.
177 (setq c
(semantic-find-tags-for-completion
179 (semantic-analyze-scoped-type-parts completetexttype scope
)
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.
186 ;; Argument list and local variables
187 (semantic-find-tags-for-completion completetext localvar
)
189 (semantic-find-tags-for-completion completetext
(when scope
(oref scope fullscope
)))
191 (semantic-analyze-find-tags-by-prefix completetext
))
196 (dtname (semantic-tag-name desired-type
)))
198 ;; Save off our first batch of completions
204 ;; Loop over all the found matches, and categorize them
205 ;; as being possible features.
206 (while (and loopc do-typeconstraint
)
210 ((semantic-tag-get-attribute (car loopc
) :operator-flag
)
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
)))
222 ;; If there is a desired type, we need a pair of restrictions
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
))
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
))
238 (if (and att
(semantic-tag-type-members att
))
239 (setq c
(cons (car loopc
) c
))))
245 ;; No desired type, no other restrictions. Just add.
247 (setq c
(cons (car loopc
) c
)))
251 (setq loopc
(cdr loopc
)))
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.
258 (semantic-analyze-type-constants desired-type
)))
263 (semantic-find-tags-for-completion
264 completetext constants
))
266 (setq c
(nconc c constants
)))
271 (setq c
(semantic-analyze-tags-of-class-list c desired-class
)))
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
)))
285 (provide 'semantic
/analyze
/complete
)
288 ;; generated-autoload-file: "../loaddefs.el"
289 ;; generated-autoload-load-name: "semantic/analyze/complete"
292 ;;; semantic/analyze/complete.el ends here