]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/jedi/api/strings.py
2 This module is here for string completions. This means mostly stuff where
3 strings are returned, like `foo = dict(bar=3); foo["ba` would complete to
6 It however does the same for numbers. The difference between string completions
7 and other completions is mostly that this module doesn't return defined
8 names in a module, but pretty much an arbitrary string.
12 from jedi
.inference
.names
import AbstractArbitraryName
13 from jedi
.inference
.helpers
import infer_call_of_leaf
14 from jedi
.api
.classes
import Completion
15 from jedi
.parser_utils
import cut_value_at_position
20 class StringName(AbstractArbitraryName
):
25 def complete_dict(module_context
, code_lines
, leaf
, position
, string
, fuzzy
):
27 if bracket_leaf
!= '[':
28 bracket_leaf
= leaf
.get_previous_leaf()
32 cut_end_quote
= get_quote_ending(string
, code_lines
, position
, invert_result
=True)
34 if bracket_leaf
== '[':
35 if string
is None and leaf
is not bracket_leaf
:
36 string
= cut_value_at_position(leaf
, position
)
38 context
= module_context
.create_context(bracket_leaf
)
40 before_node
= before_bracket_leaf
= bracket_leaf
.get_previous_leaf()
41 if before_node
in (')', ']', '}'):
42 before_node
= before_node
.parent
43 if before_node
.type in ('atom', 'trailer', 'name'):
44 values
= infer_call_of_leaf(context
, before_bracket_leaf
)
45 return list(_completions_for_dicts(
46 module_context
.inference_state
,
48 '' if string
is None else string
,
55 def _completions_for_dicts(inference_state
, dicts
, literal_string
, cut_end_quote
, fuzzy
):
56 for dict_key
in sorted(_get_python_keys(dicts
), key
=lambda x
: repr(x
)):
57 dict_key_str
= _create_repr_string(literal_string
, dict_key
)
58 if dict_key_str
.startswith(literal_string
):
59 name
= StringName(inference_state
, dict_key_str
[:-len(cut_end_quote
) or None])
64 like_name_length
=len(literal_string
),
69 def _create_repr_string(literal_string
, dict_key
):
70 if not isinstance(dict_key
, (str, bytes
)) or not literal_string
:
74 prefix
, quote
= _get_string_prefix_and_quote(literal_string
)
79 return prefix
+ quote
+ r
[1:-1] + quote
82 def _get_python_keys(dicts
):
84 if dct
.array_type
== 'dict':
85 for key
in dct
.get_key_values():
86 dict_key
= key
.get_safe_value(default
=_sentinel
)
87 if dict_key
is not _sentinel
:
91 def _get_string_prefix_and_quote(string
):
92 match
= re
.match(r
'(\w*)("""|\'{3}|
"|\')', string)
95 return match.group(1), match.group(2)
98 def _matches_quote_at_position(code_lines, quote, position):
99 string = code_lines[position[0] - 1][position[1]:position[1] + len(quote)]
100 return string == quote
103 def get_quote_ending(string, code_lines, position, invert_result=False):
104 _, quote = _get_string_prefix_and_quote(string)
108 # Add a quote only if it's not already there.
109 if _matches_quote_at_position(code_lines, quote, position) != invert_result: