|
| 1 | +# only python 3.8 needs this, but it must be the first expression in the file, so we can't predicate it |
| 2 | +from __future__ import annotations |
| 3 | +from enum import Enum |
| 4 | +from typing import Any |
| 5 | +from jedi import Interpreter, Script |
| 6 | + |
| 7 | + |
| 8 | +class CompleterMode(Enum): |
| 9 | + off = 'off' |
| 10 | + safe = 'safe' |
| 11 | + strong = 'strong' |
| 12 | + |
| 13 | + def __str__(self) -> str: |
| 14 | + return self.value |
| 15 | + |
| 16 | + |
| 17 | +class Completer(object): |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + self._docs = {} |
| 21 | + self._versions = {} |
| 22 | + # we will replace this w/ top-level globals() when we open the document |
| 23 | + self.__scope = globals() |
| 24 | + # might want to make this a {uri: []} instead of [] |
| 25 | + self.pending = [] |
| 26 | + try: |
| 27 | + import jedi |
| 28 | + self.__can_jedi = True |
| 29 | + self.mode = CompleterMode.strong |
| 30 | + except ImportError: |
| 31 | + self.__can_jedi = False |
| 32 | + self.mode = CompleterMode.off |
| 33 | + |
| 34 | + @property |
| 35 | + def mode(self) -> CompleterMode: |
| 36 | + return self.__mode |
| 37 | + |
| 38 | + @mode.setter |
| 39 | + def mode(self, mode) -> None: |
| 40 | + if type(mode) == 'str': |
| 41 | + mode = CompleterMode[mode] |
| 42 | + self.__mode = mode |
| 43 | + |
| 44 | + def open_doc(self, text: str, uri: str, version: int) -> None: |
| 45 | + self._docs[uri] = text |
| 46 | + self._versions[uri] = version |
| 47 | + |
| 48 | + def get_doc(self, uri: str) -> str: |
| 49 | + return self._docs[uri] |
| 50 | + |
| 51 | + def update_doc(self, text: str, uri: str, version: int) -> None: |
| 52 | + self._docs[uri] = text |
| 53 | + self._versions[uri] = version |
| 54 | + # any pending completions should stop running now. We use a list of Event to signal any running threads to stop |
| 55 | + for pending in self.pending: |
| 56 | + pending.set() |
| 57 | + |
| 58 | + def close_doc(self, uri: str) -> None: |
| 59 | + del self._docs[uri] |
| 60 | + del self._versions[uri] |
| 61 | + for pending in self.pending: |
| 62 | + pending.set() |
| 63 | + |
| 64 | + def is_enabled(self) -> bool: |
| 65 | + return self.__mode != CompleterMode.off |
| 66 | + |
| 67 | + def can_jedi(self) -> bool: |
| 68 | + return self.__can_jedi |
| 69 | + |
| 70 | + def set_scope(self, scope: dict) -> None: |
| 71 | + self.__scope = scope |
| 72 | + |
| 73 | + def do_completion(self, uri: str, version: int, line: int, col: int) -> list[list[Any]]: |
| 74 | + if not self._versions[uri] == version: |
| 75 | + # if you aren't the newest completion, you get nothing, quickly |
| 76 | + return [] |
| 77 | + |
| 78 | + # run jedi |
| 79 | + txt = self.get_doc(uri) |
| 80 | + # The Script completer is static analysis only, so we should actually be feeding it a whole document at once. |
| 81 | + |
| 82 | + completer = Script if self.__mode == CompleterMode.safe else Interpreter |
| 83 | + |
| 84 | + completions = completer(txt, [self.__scope]).complete(line, col) |
| 85 | + # for now, a simple sorting based on number of preceding _ |
| 86 | + # we may want to apply additional sorting to each list before combining |
| 87 | + results: list = [] |
| 88 | + results_: list = [] |
| 89 | + results__: list = [] |
| 90 | + for complete in completions: |
| 91 | + # keep checking the latest version as we run, so updated doc can cancel us |
| 92 | + if not self._versions[uri] == version: |
| 93 | + return [] |
| 94 | + result: list = self.to_result(complete, col) |
| 95 | + if result[0].startswith('__'): |
| 96 | + results__.append(result) |
| 97 | + elif result[0].startswith('_'): |
| 98 | + results_.append(result) |
| 99 | + else: |
| 100 | + results.append(result) |
| 101 | + |
| 102 | + # put the results together in a better-than-nothing sorting |
| 103 | + return results + results_ + results__ |
| 104 | + |
| 105 | + @staticmethod |
| 106 | + def to_result(complete: Any, col: int) -> list[Any]: |
| 107 | + name: str = complete.name |
| 108 | + prefix_length: int = complete.get_completion_prefix_length() |
| 109 | + start: int = col - prefix_length |
| 110 | + # all java needs to build a grpc response is completion text (name) and where the completion should start |
| 111 | + return [name, start] |
0 commit comments