Compute matches when text is a simple name. Return a list of all keywords, built-in functions and names currently defined in self.namespace or self.global_namespace that match.
(self, text: str, context: Optional[CompletionContext] = None)
| 1126 | return None |
| 1127 | |
| 1128 | def global_matches(self, text: str, context: Optional[CompletionContext] = None): |
| 1129 | """Compute matches when text is a simple name. |
| 1130 | |
| 1131 | Return a list of all keywords, built-in functions and names currently |
| 1132 | defined in self.namespace or self.global_namespace that match. |
| 1133 | |
| 1134 | """ |
| 1135 | matches = [] |
| 1136 | match_append = matches.append |
| 1137 | n = len(text) |
| 1138 | |
| 1139 | search_lists = [ |
| 1140 | keyword.kwlist, |
| 1141 | builtin_mod.__dict__.keys(), |
| 1142 | list(self.namespace.keys()), |
| 1143 | list(self.global_namespace.keys()), |
| 1144 | ] |
| 1145 | if context and context.full_text.count("\n") > 1: |
| 1146 | # try to evaluate on full buffer |
| 1147 | previous_lines = "\n".join( |
| 1148 | context.full_text.split("\n")[: context.cursor_line] |
| 1149 | ) |
| 1150 | if previous_lines: |
| 1151 | all_code_lines_before_cursor = ( |
| 1152 | self._extract_code(previous_lines) + "\n" + text |
| 1153 | ) |
| 1154 | context = EvaluationContext( |
| 1155 | globals=self.global_namespace, |
| 1156 | locals=self.namespace, |
| 1157 | evaluation=self.evaluation, |
| 1158 | auto_import=self._auto_import, |
| 1159 | policy_overrides=self.policy_overrides, |
| 1160 | ) |
| 1161 | try: |
| 1162 | obj = guarded_eval( |
| 1163 | all_code_lines_before_cursor, |
| 1164 | context, |
| 1165 | ) |
| 1166 | except Exception as e: |
| 1167 | if self.debug: |
| 1168 | warnings.warn(f"Evaluation exception {e}") |
| 1169 | |
| 1170 | search_lists.append(list(context.transient_locals.keys())) |
| 1171 | |
| 1172 | for lst in search_lists: |
| 1173 | for word in lst: |
| 1174 | if word[:n] == text and word != "__builtins__": |
| 1175 | match_append(word) |
| 1176 | |
| 1177 | snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z") |
| 1178 | for lst in [list(self.namespace.keys()), list(self.global_namespace.keys())]: |
| 1179 | shortened = { |
| 1180 | "_".join([sub[0] for sub in word.split("_")]): word |
| 1181 | for word in lst |
| 1182 | if snake_case_re.match(word) |
| 1183 | } |
| 1184 | for word in shortened.keys(): |
| 1185 | if word[:n] == text and word != "__builtins__": |
no test coverage detected