Match magics.
(self, context: CompletionContext)
| 2361 | |
| 2362 | @context_matcher() |
| 2363 | def magic_matcher(self, context: CompletionContext) -> SimpleMatcherResult: |
| 2364 | """Match magics.""" |
| 2365 | |
| 2366 | # Get all shell magics now rather than statically, so magics loaded at |
| 2367 | # runtime show up too. |
| 2368 | text = context.token |
| 2369 | lsm = self.shell.magics_manager.lsmagic() |
| 2370 | line_magics = lsm['line'] |
| 2371 | cell_magics = lsm['cell'] |
| 2372 | pre = self.magic_escape |
| 2373 | pre2 = pre + pre |
| 2374 | |
| 2375 | explicit_magic = text.startswith(pre) |
| 2376 | |
| 2377 | # Completion logic: |
| 2378 | # - user gives %%: only do cell magics |
| 2379 | # - user gives %: do both line and cell magics |
| 2380 | # - no prefix: do both |
| 2381 | # In other words, line magics are skipped if the user gives %% explicitly |
| 2382 | # |
| 2383 | # We also exclude magics that match any currently visible names: |
| 2384 | # https://github.com/ipython/ipython/issues/4877, unless the user has |
| 2385 | # typed a %: |
| 2386 | # https://github.com/ipython/ipython/issues/10754 |
| 2387 | bare_text = text.lstrip(pre) |
| 2388 | global_matches = self.global_matches(bare_text) |
| 2389 | if not explicit_magic: |
| 2390 | def matches(magic): |
| 2391 | """ |
| 2392 | Filter magics, in particular remove magics that match |
| 2393 | a name present in global namespace. |
| 2394 | """ |
| 2395 | return ( magic.startswith(bare_text) and |
| 2396 | magic not in global_matches ) |
| 2397 | else: |
| 2398 | def matches(magic): |
| 2399 | return magic.startswith(bare_text) |
| 2400 | |
| 2401 | completions = [pre2 + m for m in cell_magics if matches(m)] |
| 2402 | if not text.startswith(pre2): |
| 2403 | completions += [pre + m for m in line_magics if matches(m)] |
| 2404 | |
| 2405 | is_magic_prefix = len(text) > 0 and text[0] == "%" |
| 2406 | |
| 2407 | return { |
| 2408 | "completions": [ |
| 2409 | SimpleCompletion(text=comp, type="magic") for comp in completions |
| 2410 | ], |
| 2411 | "suppress": is_magic_prefix and len(completions) > 0, |
| 2412 | } |
| 2413 | |
| 2414 | @context_matcher() |
| 2415 | def magic_config_matcher(self, context: CompletionContext) -> SimpleMatcherResult: |
nothing calls this directly
no test coverage detected