Match string keys in a dictionary, after e.g. ``foo[``. .. deprecated:: 8.6 You can use :meth:`dict_key_matcher` instead.
(self, text: str)
| 3019 | ) |
| 3020 | |
| 3021 | def dict_key_matches(self, text: str) -> list[str]: |
| 3022 | """Match string keys in a dictionary, after e.g. ``foo[``. |
| 3023 | |
| 3024 | .. deprecated:: 8.6 |
| 3025 | You can use :meth:`dict_key_matcher` instead. |
| 3026 | """ |
| 3027 | |
| 3028 | # Short-circuit on closed dictionary (regular expression would |
| 3029 | # not match anyway, but would take quite a while). |
| 3030 | if self.text_until_cursor.strip().endswith("]"): |
| 3031 | return [] |
| 3032 | |
| 3033 | match = DICT_MATCHER_REGEX.search(self.text_until_cursor) |
| 3034 | |
| 3035 | if match is None: |
| 3036 | return [] |
| 3037 | |
| 3038 | expr, prior_tuple_keys, key_prefix = match.groups() |
| 3039 | |
| 3040 | obj = self._evaluate_expr(expr) |
| 3041 | |
| 3042 | if obj is not_found: |
| 3043 | return [] |
| 3044 | |
| 3045 | keys = self._get_keys(obj) |
| 3046 | if not keys: |
| 3047 | return keys |
| 3048 | |
| 3049 | tuple_prefix = guarded_eval( |
| 3050 | prior_tuple_keys, |
| 3051 | EvaluationContext( |
| 3052 | globals=self.global_namespace, |
| 3053 | locals=self.namespace, |
| 3054 | evaluation=self.evaluation, # type: ignore |
| 3055 | in_subscript=True, |
| 3056 | auto_import=self._auto_import, |
| 3057 | policy_overrides=self.policy_overrides, |
| 3058 | ), |
| 3059 | ) |
| 3060 | |
| 3061 | closing_quote, token_offset, matches = match_dict_keys( |
| 3062 | keys, key_prefix, self.splitter.delims, extra_prefix=tuple_prefix |
| 3063 | ) |
| 3064 | if not matches: |
| 3065 | return [] |
| 3066 | |
| 3067 | # get the cursor position of |
| 3068 | # - the text being completed |
| 3069 | # - the start of the key text |
| 3070 | # - the start of the completion |
| 3071 | text_start = len(self.text_until_cursor) - len(text) |
| 3072 | if key_prefix: |
| 3073 | key_start = match.start(3) |
| 3074 | completion_start = key_start + token_offset |
| 3075 | else: |
| 3076 | key_start = completion_start = match.end() |
| 3077 | |
| 3078 | # grab the leading prefix, to make sure all completions start with `text` |
no test coverage detected