Completer for Python code.
| 41 | |
| 42 | |
| 43 | class PythonCompleter(Completer): |
| 44 | """ |
| 45 | Completer for Python code. |
| 46 | """ |
| 47 | |
| 48 | def __init__( |
| 49 | self, |
| 50 | get_globals: Callable[[], dict[str, Any]], |
| 51 | get_locals: Callable[[], dict[str, Any]], |
| 52 | enable_dictionary_completion: Callable[[], bool], |
| 53 | ) -> None: |
| 54 | super().__init__() |
| 55 | |
| 56 | self.get_globals = get_globals |
| 57 | self.get_locals = get_locals |
| 58 | self.enable_dictionary_completion = enable_dictionary_completion |
| 59 | |
| 60 | self._system_completer = SystemCompleter() |
| 61 | self._jedi_completer = JediCompleter(get_globals, get_locals) |
| 62 | self._dictionary_completer = DictionaryCompleter(get_globals, get_locals) |
| 63 | |
| 64 | self._path_completer_cache: GrammarCompleter | None = None |
| 65 | self._path_completer_grammar_cache: _CompiledGrammar | None = None |
| 66 | |
| 67 | @property |
| 68 | def _path_completer(self) -> GrammarCompleter: |
| 69 | if self._path_completer_cache is None: |
| 70 | self._path_completer_cache = GrammarCompleter( |
| 71 | self._path_completer_grammar, |
| 72 | { |
| 73 | "var1": PathCompleter(expanduser=True), |
| 74 | "var2": PathCompleter(expanduser=True), |
| 75 | }, |
| 76 | ) |
| 77 | return self._path_completer_cache |
| 78 | |
| 79 | @property |
| 80 | def _path_completer_grammar(self) -> _CompiledGrammar: |
| 81 | """ |
| 82 | Return the grammar for matching paths inside strings inside Python |
| 83 | code. |
| 84 | """ |
| 85 | # We make this lazy, because it delays startup time a little bit. |
| 86 | # This way, the grammar is build during the first completion. |
| 87 | if self._path_completer_grammar_cache is None: |
| 88 | self._path_completer_grammar_cache = self._create_path_completer_grammar() |
| 89 | return self._path_completer_grammar_cache |
| 90 | |
| 91 | def _create_path_completer_grammar(self) -> _CompiledGrammar: |
| 92 | def unwrapper(text: str) -> str: |
| 93 | return re.sub(r"\\(.)", r"\1", text) |
| 94 | |
| 95 | def single_quoted_wrapper(text: str) -> str: |
| 96 | return text.replace("\\", "\\\\").replace("'", "\\'") |
| 97 | |
| 98 | def double_quoted_wrapper(text: str) -> str: |
| 99 | return text.replace("\\", "\\\\").replace('"', '\\"') |
| 100 |
no outgoing calls
no test coverage detected