2 from textwrap
import dedent
3 from inspect
import Parameter
5 from parso
.python
.token
import PythonTokenTypes
6 from parso
.python
import tree
7 from parso
.tree
import search_ancestor
, Leaf
8 from parso
import split_lines
10 from jedi
import debug
11 from jedi
import settings
12 from jedi
.api
import classes
13 from jedi
.api
import helpers
14 from jedi
.api
import keywords
15 from jedi
.api
.strings
import complete_dict
16 from jedi
.api
.file_name
import complete_file_name
17 from jedi
.inference
import imports
18 from jedi
.inference
.base_value
import ValueSet
19 from jedi
.inference
.helpers
import infer_call_of_leaf
, parse_dotted_names
20 from jedi
.inference
.context
import get_global_filters
21 from jedi
.inference
.value
import TreeInstance
22 from jedi
.inference
.docstring_utils
import DocstringModule
23 from jedi
.inference
.names
import ParamNameWrapper
, SubModuleName
24 from jedi
.inference
.gradual
.conversion
import convert_values
, convert_names
25 from jedi
.parser_utils
import cut_value_at_position
26 from jedi
.plugins
import plugin_manager
29 class ParamNameWithEquals(ParamNameWrapper
):
30 def get_public_name(self
):
31 return self
.string_name
+ '='
34 def _get_signature_param_names(signatures
, positional_count
, used_kwargs
):
36 for call_sig
in signatures
:
37 for i
, p
in enumerate(call_sig
.params
):
39 if i
< positional_count
and kind
== Parameter
.POSITIONAL_OR_KEYWORD
:
41 if kind
in (Parameter
.POSITIONAL_OR_KEYWORD
, Parameter
.KEYWORD_ONLY
) \
42 and p
.name
not in used_kwargs
:
43 yield ParamNameWithEquals(p
._name
)
46 def _must_be_kwarg(signatures
, positional_count
, used_kwargs
):
51 for signature
in signatures
:
52 for i
, p
in enumerate(signature
.params
):
54 if kind
is Parameter
.VAR_POSITIONAL
:
55 # In case there were not already kwargs, the next param can
56 # always be a normal argument.
59 if i
>= positional_count
and kind
in (Parameter
.POSITIONAL_OR_KEYWORD
,
60 Parameter
.POSITIONAL_ONLY
):
68 def filter_names(inference_state
, completion_names
, stack
, like_name
, fuzzy
, cached_name
):
70 if settings
.case_insensitive_completion
:
71 like_name
= like_name
.lower()
72 for name
in completion_names
:
73 string
= name
.string_name
74 if settings
.case_insensitive_completion
:
75 string
= string
.lower()
76 if helpers
.match(string
, like_name
, fuzzy
=fuzzy
):
77 new
= classes
.Completion(
83 cached_name
=cached_name
,
85 k
= (new
.name
, new
.complete
) # key
88 tree_name
= name
.tree_name
89 if tree_name
is not None:
90 definition
= tree_name
.get_definition()
91 if definition
is not None and definition
.type == 'del_stmt':
96 def _remove_duplicates(completions
, other_completions
):
97 names
= {d
.name
for d
in other_completions
}
98 return [c
for c
in completions
if c
.name
not in names
]
101 def get_user_context(module_context
, position
):
103 Returns the scope in which the user resides. This includes flows.
105 leaf
= module_context
.tree_node
.get_leaf_for_position(position
, include_prefixes
=True)
106 return module_context
.create_context(leaf
)
109 def get_flow_scope_node(module_node
, position
):
110 node
= module_node
.get_leaf_for_position(position
, include_prefixes
=True)
111 while not isinstance(node
, (tree
.Scope
, tree
.Flow
)):
117 @plugin_manager.decorate()
118 def complete_param_names(context
, function_name
, decorator_nodes
):
119 # Basically there's no way to do param completion. The plugins are
120 # responsible for this.
125 def __init__(self
, inference_state
, module_context
, code_lines
, position
,
126 signatures_callback
, fuzzy
=False):
127 self
._inference
_state
= inference_state
128 self
._module
_context
= module_context
129 self
._module
_node
= module_context
.tree_node
130 self
._code
_lines
= code_lines
132 # The first step of completions is to get the name
133 self
._like
_name
= helpers
.get_on_completion_name(self
._module
_node
, code_lines
, position
)
134 # The actual cursor position is not what we need to calculate
135 # everything. We want the start of the name we're on.
136 self
._original
_position
= position
137 self
._signatures
_callback
= signatures_callback
142 leaf
= self
._module
_node
.get_leaf_for_position(
143 self
._original
_position
,
144 include_prefixes
=True
146 string
, start_leaf
, quote
= _extract_string_while_in_string(leaf
, self
._original
_position
)
148 prefixed_completions
= complete_dict(
149 self
._module
_context
,
152 self
._original
_position
,
153 None if string
is None else quote
+ string
,
157 if string
is not None and not prefixed_completions
:
158 prefixed_completions
= list(complete_file_name(
159 self
._inference
_state
, self
._module
_context
, start_leaf
, quote
, string
,
160 self
._like
_name
, self
._signatures
_callback
,
161 self
._code
_lines
, self
._original
_position
,
164 if string
is not None:
165 if not prefixed_completions
and '\n' in string
:
166 # Complete only multi line strings
167 prefixed_completions
= self
._complete
_in
_string
(start_leaf
, string
)
168 return prefixed_completions
170 cached_name
, completion_names
= self
._complete
_python
(leaf
)
172 completions
= list(filter_names(self
._inference
_state
, completion_names
,
173 self
.stack
, self
._like
_name
,
174 self
._fuzzy
, cached_name
=cached_name
))
177 # Removing duplicates mostly to remove False/True/None duplicates.
178 _remove_duplicates(prefixed_completions
, completions
)
179 + sorted(completions
, key
=lambda x
: (x
.name
.startswith('__'),
180 x
.name
.startswith('_'),
184 def _complete_python(self
, leaf
):
186 Analyzes the current context of a completion and decides what to
189 Technically this works by generating a parser stack and analysing the
190 current stack for possible grammar nodes.
192 Possible enhancements:
193 - global/nonlocal search global
194 - yield from / raise from <- could be only exceptions/generators
195 - In args: */**: no completion
196 - In params (also lambda): no completion before =
198 grammar
= self
._inference
_state
.grammar
199 self
.stack
= stack
= None
201 self
._original
_position
[0],
202 self
._original
_position
[1] - len(self
._like
_name
)
207 self
.stack
= stack
= helpers
.get_stack_at_position(
208 grammar
, self
._code
_lines
, leaf
, self
._position
210 except helpers
.OnErrorLeaf
as e
:
211 value
= e
.error_leaf
.value
213 # After ErrorLeaf's that are dots, we will not do any
214 # completions since this probably just confuses the user.
215 return cached_name
, []
217 # If we don't have a value, just use global completion.
218 return cached_name
, self
._complete
_global
_scope
()
220 allowed_transitions
= \
221 list(stack
._allowed
_transition
_names
_and
_token
_types
())
223 if 'if' in allowed_transitions
:
224 leaf
= self
._module
_node
.get_leaf_for_position(self
._position
, include_prefixes
=True)
225 previous_leaf
= leaf
.get_previous_leaf()
227 indent
= self
._position
[1]
228 if not (leaf
.start_pos
<= self
._position
<= leaf
.end_pos
):
229 indent
= leaf
.start_pos
[1]
231 if previous_leaf
is not None:
234 stmt
= search_ancestor(
235 stmt
, 'if_stmt', 'for_stmt', 'while_stmt', 'try_stmt',
242 if type_
== 'error_node':
243 first
= stmt
.children
[0]
244 if isinstance(first
, Leaf
):
245 type_
= first
.value
+ '_stmt'
247 if stmt
.start_pos
[1] == indent
:
248 if type_
== 'if_stmt':
249 allowed_transitions
+= ['elif', 'else']
250 elif type_
== 'try_stmt':
251 allowed_transitions
+= ['except', 'finally', 'else']
252 elif type_
== 'for_stmt':
253 allowed_transitions
.append('else')
255 completion_names
= []
258 if any(t
in allowed_transitions
for t
in (PythonTokenTypes
.NAME
,
259 PythonTokenTypes
.INDENT
)):
260 # This means that we actually have to do type inference.
262 nonterminals
= [stack_node
.nonterminal
for stack_node
in stack
]
264 nodes
= _gather_nodes(stack
)
265 if nodes
and nodes
[-1] in ('as', 'def', 'class'):
266 # No completions for ``with x as foo`` and ``import x as foo``.
267 # Also true for defining names as a class or function.
268 return cached_name
, list(self
._complete
_inherited
(is_function
=True))
269 elif "import_stmt" in nonterminals
:
270 level
, names
= parse_dotted_names(nodes
, "import_from" in nonterminals
)
272 only_modules
= not ("import_from" in nonterminals
and 'import' in nodes
)
273 completion_names
+= self
._get
_importer
_names
(
276 only_modules
=only_modules
,
278 elif nonterminals
[-1] in ('trailer', 'dotted_name') and nodes
[-1] == '.':
279 dot
= self
._module
_node
.get_leaf_for_position(self
._position
)
280 if dot
.type == "endmarker":
281 # This is a bit of a weird edge case, maybe we can somehow
283 dot
= leaf
.get_previous_leaf()
284 cached_name
, n
= self
._complete
_trailer
(dot
.get_previous_leaf())
285 completion_names
+= n
286 elif self
._is
_parameter
_completion
():
287 completion_names
+= self
._complete
_params
(leaf
)
289 # Apparently this looks like it's good enough to filter most cases
290 # so that signature completions don't randomly appear.
291 # To understand why this works, three things are important:
292 # 1. trailer with a `,` in it is either a subscript or an arglist.
293 # 2. If there's no `,`, it's at the start and only signatures start
294 # with `(`. Other trailers could start with `.` or `[`.
295 # 3. Decorators are very primitive and have an optional `(` with
296 # optional arglist in them.
297 if nodes
[-1] in ['(', ','] \
298 and nonterminals
[-1] in ('trailer', 'arglist', 'decorator'):
299 signatures
= self
._signatures
_callback
(*self
._position
)
301 call_details
= signatures
[0]._call
_details
302 used_kwargs
= list(call_details
.iter_used_keyword_arguments())
303 positional_count
= call_details
.count_positional_arguments()
305 completion_names
+= _get_signature_param_names(
311 kwargs_only
= _must_be_kwarg(signatures
, positional_count
, used_kwargs
)
314 completion_names
+= self
._complete
_global
_scope
()
315 completion_names
+= self
._complete
_inherited
(is_function
=False)
318 current_line
= self
._code
_lines
[self
._position
[0] - 1][:self
._position
[1]]
319 completion_names
+= self
._complete
_keywords
(
321 only_values
=not (not current_line
or current_line
[-1] in ' \t.;'
322 and current_line
[-3:] != '...')
325 return cached_name
, completion_names
327 def _is_parameter_completion(self
):
329 if tos
.nonterminal
== 'lambdef' and len(tos
.nodes
) == 1:
330 # We are at the position `lambda `, where basically the next node
333 if tos
.nonterminal
in 'parameters':
334 # Basically we are at the position `foo(`, there's nothing there
335 # yet, so we have no `typedargslist`.
337 # var args is for lambdas and typed args for normal functions
338 return tos
.nonterminal
in ('typedargslist', 'varargslist') and tos
.nodes
[-1] == ','
340 def _complete_params(self
, leaf
):
341 stack_node
= self
.stack
[-2]
342 if stack_node
.nonterminal
== 'parameters':
343 stack_node
= self
.stack
[-3]
344 if stack_node
.nonterminal
== 'funcdef':
345 context
= get_user_context(self
._module
_context
, self
._position
)
346 node
= search_ancestor(leaf
, 'error_node', 'funcdef')
348 if node
.type == 'error_node':
350 if n
.type == 'decorators':
351 decorators
= n
.children
352 elif n
.type == 'decorator':
357 decorators
= node
.get_decorators()
358 function_name
= stack_node
.nodes
[1]
360 return complete_param_names(context
, function_name
.value
, decorators
)
363 def _complete_keywords(self
, allowed_transitions
, only_values
):
364 for k
in allowed_transitions
:
365 if isinstance(k
, str) and k
.isalpha():
366 if not only_values
or k
in ('True', 'False', 'None'):
367 yield keywords
.KeywordName(self
._inference
_state
, k
)
369 def _complete_global_scope(self
):
370 context
= get_user_context(self
._module
_context
, self
._position
)
371 debug
.dbg('global completion scope: %s', context
)
372 flow_scope_node
= get_flow_scope_node(self
._module
_node
, self
._position
)
373 filters
= get_global_filters(
378 completion_names
= []
379 for filter in filters
:
380 completion_names
+= filter.values()
381 return completion_names
383 def _complete_trailer(self
, previous_leaf
):
384 inferred_context
= self
._module
_context
.create_context(previous_leaf
)
385 values
= infer_call_of_leaf(inferred_context
, previous_leaf
)
386 debug
.dbg('trailer completion values: %s', values
, color
='MAGENTA')
388 # The cached name simply exists to make speed optimizations for certain
394 if len(v
.string_names
) == 1:
395 module_name
= v
.string_names
[0]
396 if module_name
in ('numpy', 'tensorflow', 'matplotlib', 'pandas'):
397 cached_name
= module_name
399 return cached_name
, self
._complete
_trailer
_for
_values
(values
)
401 def _complete_trailer_for_values(self
, values
):
402 user_context
= get_user_context(self
._module
_context
, self
._position
)
404 return complete_trailer(user_context
, values
)
406 def _get_importer_names(self
, names
, level
=0, only_modules
=True):
407 names
= [n
.value
for n
in names
]
408 i
= imports
.Importer(self
._inference
_state
, names
, self
._module
_context
, level
)
409 return i
.completion_names(self
._inference
_state
, only_modules
=only_modules
)
411 def _complete_inherited(self
, is_function
=True):
413 Autocomplete inherited methods when overriding in child class.
415 leaf
= self
._module
_node
.get_leaf_for_position(self
._position
, include_prefixes
=True)
416 cls
= tree
.search_ancestor(leaf
, 'classdef')
420 # Complete the methods that are defined in the super classes.
421 class_value
= self
._module
_context
.create_value(cls
)
423 if cls
.start_pos
[1] >= leaf
.start_pos
[1]:
426 filters
= class_value
.get_filters(is_instance
=True)
427 # The first dict is the dictionary of class itself.
429 for filter in filters
:
430 for name
in filter.values():
431 # TODO we should probably check here for properties
432 if (name
.api_type
== 'function') == is_function
:
435 def _complete_in_string(self
, start_leaf
, string
):
437 To make it possible for people to have completions in doctests or
438 generally in "Python" code in docstrings, we use the following
441 - Having an indented block of code
442 - Having some doctest code that starts with `>>>`
443 - Having backticks that doesn't have whitespace inside it
445 def iter_relevant_lines(lines
):
446 include_next_line
= False
448 if include_next_line
or l
.startswith('>>>') or l
.startswith(' '):
449 yield re
.sub(r
'^( *>>> ?| +)', '', l
)
453 include_next_line
= bool(re
.match(' *>>>', l
))
455 string
= dedent(string
)
456 code_lines
= split_lines(string
, keepends
=True)
457 relevant_code_lines
= list(iter_relevant_lines(code_lines
))
458 if relevant_code_lines
[-1] is not None:
459 # Some code lines might be None, therefore get rid of that.
460 relevant_code_lines
= ['\n' if c
is None else c
for c
in relevant_code_lines
]
461 return self
._complete
_code
_lines
(relevant_code_lines
)
462 match
= re
.search(r
'`([^`\s]+)', code_lines
[-1])
464 return self
._complete
_code
_lines
([match
.group(1)])
467 def _complete_code_lines(self
, code_lines
):
468 module_node
= self
._inference
_state
.grammar
.parse(''.join(code_lines
))
469 module_value
= DocstringModule(
470 in_module_context
=self
._module
_context
,
471 inference_state
=self
._inference
_state
,
472 module_node
=module_node
,
473 code_lines
=code_lines
,
476 self
._inference
_state
,
477 module_value
.as_context(),
478 code_lines
=code_lines
,
479 position
=module_node
.end_pos
,
480 signatures_callback
=lambda *args
, **kwargs
: [],
485 def _gather_nodes(stack
):
487 for stack_node
in stack
:
488 if stack_node
.dfa
.from_rule
== 'small_stmt':
491 nodes
+= stack_node
.nodes
495 _string_start
= re
.compile(r
'^\w*(\'{3}|
"{3}|\'|")')
498 def _extract_string_while_in_string(leaf, position):
499 def return_part_of_leaf(leaf):
501 if leaf.line == position[0]:
502 kwargs['endpos
'] = position[1] - leaf.column
503 match = _string_start.match(leaf.value, **kwargs)
505 return None, None, None
506 start = match.group(0)
507 if leaf.line == position[0] and position[1] < leaf.column + match.end():
508 return None, None, None
509 return cut_value_at_position(leaf, position)[match.end():], leaf, start
511 if position < leaf.start_pos:
512 return None, None, None
514 if leaf.type == 'string
':
515 return return_part_of_leaf(leaf)
518 while leaf is not None:
519 if leaf.type == 'error_leaf
' and ('"' in leaf.value or "'" in leaf.value):
520 if len(leaf.value) > 1:
521 return return_part_of_leaf(leaf)
524 prefix_leaf = leaf.get_previous_leaf()
525 if prefix_leaf is None or prefix_leaf.type != 'name
' \
526 or not all(c in 'rubf
' for c in prefix_leaf.value.lower()):
530 ''.join(cut_value_at_position(l, position) for l in leaves),
532 ('' if prefix_leaf is None else prefix_leaf.value)
533 + cut_value_at_position(leaf, position),
535 if leaf.line != position[0]:
536 # Multi line strings are always simple error leaves and contain the
537 # whole string, single line error leaves are atherefore important
538 # now and since the line is different, it's
not really a single
539 # line string anymore.
541 leaves
.insert(0, leaf
)
542 leaf
= leaf
.get_previous_leaf()
543 return None, None, None
546 def complete_trailer(user_context
, values
):
547 completion_names
= []
549 for filter in value
.get_filters(origin_scope
=user_context
.tree_node
):
550 completion_names
+= filter.values()
552 if not value
.is_stub() and isinstance(value
, TreeInstance
):
553 completion_names
+= _complete_getattr(user_context
, value
)
555 python_values
= convert_values(values
)
556 for c
in python_values
:
558 for filter in c
.get_filters(origin_scope
=user_context
.tree_node
):
559 completion_names
+= filter.values()
560 return completion_names
563 def _complete_getattr(user_context
, instance
):
565 A heuristic to make completion for proxy objects work. This is not
566 intended to work in all cases. It works exactly in this case:
568 def __getattr__(self, name):
570 return getattr(any_object, name)
572 It is important that the return contains getattr directly, otherwise it
573 won't work anymore. It's really just a stupid heuristic. It will not
574 work if you write e.g. `return (getatr(o, name))`, because of the
575 additional parentheses. It will also not work if you move the getattr
576 to some other place that is not the return statement itself.
578 It is intentional that it doesn't work in all cases. Generally it's
579 really hard to do even this case (as you can see below). Most people
580 will write it like this anyway and the other ones, well they are just
581 out of luck I guess :) ~dave.
583 names
= (instance
.get_function_slot_names('__getattr__')
584 or instance
.get_function_slot_names('__getattribute__'))
585 functions
= ValueSet
.from_sets(
589 for func
in functions
:
590 tree_node
= func
.tree_node
591 if tree_node
is None or tree_node
.type != 'funcdef':
594 for return_stmt
in tree_node
.iter_return_stmts():
595 # Basically until the next comment we just try to find out if a
596 # return statement looks exactly like `return getattr(x, name)`.
597 if return_stmt
.type != 'return_stmt':
599 atom_expr
= return_stmt
.children
[1]
600 if atom_expr
.type != 'atom_expr':
602 atom
= atom_expr
.children
[0]
603 trailer
= atom_expr
.children
[1]
604 if len(atom_expr
.children
) != 2 or atom
.type != 'name' \
605 or atom
.value
!= 'getattr':
607 arglist
= trailer
.children
[1]
608 if arglist
.type != 'arglist' or len(arglist
.children
) < 3:
610 context
= func
.as_context()
611 object_node
= arglist
.children
[0]
613 # Make sure it's a param: foo in __getattr__(self, foo)
614 name_node
= arglist
.children
[2]
615 name_list
= context
.goto(name_node
, name_node
.start_pos
)
616 if not any(n
.api_type
== 'param' for n
in name_list
):
619 # Now that we know that these are most probably completion
620 # objects, we just infer the object and return them as
622 objects
= context
.infer_node(object_node
)
623 return complete_trailer(user_context
, objects
)
627 def search_in_module(inference_state
, module_context
, names
, wanted_names
,
628 wanted_type
, complete
=False, fuzzy
=False,
629 ignore_imports
=False, convert
=False):
630 for s
in wanted_names
[:-1]:
633 if s
== n
.string_name
:
634 if n
.tree_name
is not None and n
.api_type
in ('module', 'namespace') \
637 new_names
+= complete_trailer(
641 debug
.dbg('dot lookup on search %s from %s', new_names
, names
[:10])
644 last_name
= wanted_names
[-1].lower()
646 string
= n
.string_name
.lower()
647 if complete
and helpers
.match(string
, last_name
, fuzzy
=fuzzy
) \
648 or not complete
and string
== last_name
:
649 if isinstance(n
, SubModuleName
):
650 names
= [v
.name
for v
in n
.infer()]
654 names
= convert_names(names
)
657 def_
= classes
.Completion(
660 like_name_length
=len(last_name
),
664 def_
= classes
.Name(inference_state
, n2
)
665 if not wanted_type
or wanted_type
== def_
.type: