def get_completions(self): index = self.model.index("insert") script = Script(self.model.get_text(), line=index.line, column=index.col, path=self.model.document.get_path()) create_completion = lambda j, t="": JediCompletion(j,t, line=index.line, column=index.col) try: call_signatures = script.call_signatures() except SyntaxError: return ("", []) if not call_signatures: char = self.model.get("insert - 1c") if not char or not char in set(session.config.get('wchars')+"."): return ("", []) completions = sorted(script.completions(), key=comparator_key) return ("", (create_completion(completion) for completion in completions)) else: call_signature = "%(name)s(%(args)s)"% { 'name' : call_signatures[0].name, 'args' : ",".join(p.name for p in call_signatures[0].params) } if call_signatures[0].index is None: return (call_signature, []) char = self.model.get("insert - 1c") if not char or not char in set(session.config.get('wchars')+"."): return (call_signature, []) param_def = call_signatures[0].params[call_signatures[0].index] completions = sorted(script.completions(), key=comparator_key) return (call_signature, (create_completion(completion) for completion in completions))
def test_nested_namedtuples(Script): """ From issue #730. """ s = Script(dedent(''' import collections Dataset = collections.namedtuple('Dataset', ['data']) Datasets = collections.namedtuple('Datasets', ['train']) train_x = Datasets(train=Dataset('data_value')) train_x.train.''')) assert 'data' in [c.name for c in s.complete()]
def test_nested_namedtuples(Script): """ From issue #730. """ s = Script(dedent(''' import collections Dataset = collections.namedtuple('Dataset', ['data']) Datasets = collections.namedtuple('Datasets', ['train']) train_x = Datasets(train=Dataset('data_value')) train_x.train.''' )) assert 'data' in [c.name for c in s.completions()]
def test_namedtuple_goto_definitions(Script): source = dedent(""" from collections import namedtuple Foo = namedtuple('Foo', 'id timestamp gps_timestamp attributes') Foo""") from jedi.api import Script d1, = Script(source).goto_definitions() assert d1.get_line_code() == "class Foo(tuple):\n" assert d1.module_path is None
def test_namedtuple_infer(Script): source = dedent(""" from collections import namedtuple Foo = namedtuple('Foo', 'id timestamp gps_timestamp attributes') Foo""") from jedi.api import Script d1, = Script(source).infer() assert d1.get_line_code() == "class Foo(tuple):\n" assert d1.module_path is None assert d1.docstring() == 'Foo(id, timestamp, gps_timestamp, attributes)'
def syntax_error(buff): start = buff.get_start_iter() end = buff.get_end_iter() text = buff.get_text(start, end, True) if buff.get_tag_table().lookup("err_t") is None: buff.create_tag("err_t", underline="error") buff.remove_tag_by_name("err_t", start, end) errors = Script(text).get_syntax_errors() for e in errors: start = buff.get_start_iter() end = buff.get_end_iter() lin = e.line - 1 col = e.column - 1 lin2 = e.until_line - 1 col2 = e.until_column - 1 if lin < 1: lin = 0 if col < 1: col = 0 if lin2 < 1: lin2 = 0 if col2 < 1: col2 = 0 start.set_line(lin) end.set_line(lin2) start.set_line_offset(col) end.set_line_offset(col2) buff.apply_tag_by_name("err_t", start, end)
def construct_parsed_custom_class(calculator: MROCalculator, script: Script, expected: Dict): loc = expected['location'] # get the Jedi Name, line index starting from 1 class_name = script.infer(loc[0] + 1, loc[1])[0] # construct the ParsedCustomClass and return return ParsedCustomClass(class_name, calculator)
def get_script(document): doc_text = document.get_text(document.get_start_iter(), document.get_end_iter(), False) iter_cursor = document.get_iter_at_mark(document.get_insert()) linenum = iter_cursor.get_line() + 1 charnum = iter_cursor.get_line_index() return Script(doc_text, linenum, charnum, 'py')
def test_namedtuple_str(letter, expected, Script): source = dedent("""\ import collections Person = collections.namedtuple('Person', 'name smart') dave = Person('Dave', False) dave.%s""") % letter result = Script(source).completions() completions = set(r.name for r in result) assert completions == set(expected)
def test_namedtuple_list(Script): source = dedent("""\ import collections Cat = collections.namedtuple('Person', ['legs', u'length', 'large']) garfield = Cat(4, '85cm', True) garfield.l""") result = Script(source).completions() completions = set(r.name for r in result) assert completions == {'legs', 'length', 'large'}
def complete(msg, transport): handle = msg[0] info = msg[1] col = info['col'] line = info['line'] text = info['text'] path = info['path'] root = info.get('root') if root and root not in sys.path: sys.path.append(root) cur_line = text[line - 1][:col-1] match = re.search(r'\w+$', cur_line) if match: word = match.group(0) else: word = '' start_col = col - 1 - len(word) line_text = cur_line[:start_col] resp = [start_col+1] if not (path == _cache.get('path') and line == _cache.get('line') and len(text) == len(_cache.get('text')) and line_text == _cache.get('line_text')): script = Script('\n'.join(text), line=line, column=start_col) completions = tuple(script.completions()) _cache.update(path=path, line=line, text=text, line_text=line_text, completions=completions) else: completions = _cache.get('completions') if word: result = yield from fuzzy_match(completions, word) else: result = normal_match(completions) if result: resp.append(result) resp.append(path) if not transport._closing: transport.write(json.dumps([handle, resp]).encode('utf-8')) del result
def fill_store(buff): global store text = str( buff.get_text(buff.get_start_iter(), buff.get_iter_at_mark(buff.get_insert()), False)) completions = None for _ in range(5): try: completions = Script(text).complete() break except: return if completions is not None and len(completions) > 0: store = [x.name for x in completions if x] return store
def completionInvoked(self, view, range_, invocation_type): """For the lack of a better event, we create a script here and remember its completions""" # TODO Check if cursor positioned in a comment or string and # DO NOT EVEN TRY TO COMPLETE SOMETHING IN THAT CASE! # Unfortunately, due a (still opened) BUG https://bugs.kde.org/show_bug.cgi?id=247896#c5 # it is impossible to get that info nowadays :( # Fortunately, pate plugin can be hacked to provide required method... doc = view.document() if not doc.mimeType() in self.MIMETYPES: # Reset the model (and loose all completions!) if current document # is not suitable!!! (so data() will return nothing) self.resultList.clear() return cursor = view.cursorPosition() self.script = Script(doc.text(), cursor.line() + 1, cursor.column(), doc.url().toLocalFile()) self.resultList = self.script.complete()
def tab_suggest(buff: Gtk.TextBuffer, listore=None): global store text = str( buff.get_text(buff.get_start_iter(), buff.get_iter_at_mark(buff.get_insert()), False)) completions = None for _ in range(5): try: completions = Script(text).complete() break except: return if len(completions) == 1: com = str(completions[0].name) text = buff.get_insert() start_it = buff.get_start_iter() text = str(buff.get_text(start_it, buff.get_iter_at_mark(text), False)) sp1 = text.split(' ')[-1].split('.') it1 = buff.get_iter_at_mark(buff.get_insert()) it2 = buff.get_iter_at_mark(buff.get_insert()) lin = it1.get_line() while 1: _k = it1.backward_char() if not _k: break #if sp1[-1] == it1.get_char(): # break if it1.get_line() < lin: it1.forward_char() break if it1.get_char().isspace(): it1.forward_char() break if '\n' in list(it1.get_char()): it1.forward_char() break if it1.get_char() != "_" and it1.get_char().isalnum() is False: it1.forward_char() break buff.delete(it1, it2) buff.insert_at_cursor(com) store = 0 elif completions is not None and len(completions) > 0: store = [x.name for x in completions if x]
def get_completions(code, line, column, path=None): with _jedi_lock: script = Script(code, line, column, path=path) return script.completions()
def run(code): defs = Script(code).goto_definitions() return {d.name for d in defs}
def d(source): x, = Script(source).goto_definitions() return x.name
class JediCompletionModel(CodeCompletionBase): TITLE_AUTOCOMPLETION = i18nc('@label:listbox', 'Python Jedi autocomplete') MIMETYPES = ['text/x-python'] """Code Completion model using Jedi. I chose not to use libkatepate’s AbstractCodeCompletionModel due to it being optimized for several traits that we don’t need, especially line and word extraction """ def __init__(self, parent): """Script and completion list, the only properties we need""" super(JediCompletionModel, self).__init__(parent) self.script = None def completionInvoked(self, view, range_, invocation_type): """For the lack of a better event, we create a script here and remember its completions""" # TODO Check if cursor positioned in a comment or string and # DO NOT EVEN TRY TO COMPLETE SOMETHING IN THAT CASE! # Unfortunately, due a (still opened) BUG https://bugs.kde.org/show_bug.cgi?id=247896#c5 # it is impossible to get that info nowadays :( # Fortunately, pate plugin can be hacked to provide required method... doc = view.document() if not doc.mimeType() in self.MIMETYPES: # Reset the model (and loose all completions!) if current document # is not suitable!!! (so data() will return nothing) self.resultList.clear() return cursor = view.cursorPosition() self.script = Script(doc.text(), cursor.line() + 1, cursor.column(), doc.url().toLocalFile()) self.resultList = self.script.complete() def data(self, index, role): """Basically a 2D-lookup-table for all the things a code completion model can do""" if not index.parent().isValid(): return self.TITLE_AUTOCOMPLETION item = self.resultList[index.row()] col = index.column() if role == Qt.DecorationRole and col == CCM.Icon: return KIcon(TYPE2ICON[item.type.lower()]).pixmap(16, 16) if role == Qt.DisplayRole: call_def = self.script.get_in_function_call() if call_def: before, after = _func_param_strings(call_def) if col == CCM.Prefix: return before if call_def else None elif col == CCM.Name: return item.word elif col == CCM.Postfix: return after if call_def else item.description #elif col == CCM.Arguments: # TODO: what could we use it for? elif col == CCM.Name: return self.roles.get(role)
def _get_doc(word: str, buff, line_col: tuple): full_text = str( buff.get_text(buff.get_start_iter(), buff.get_end_iter(), True)) sc = Script(full_text).infer(line_col[0] + 1, line_col[1]) return sc
def run(code): defs = Script(code).infer() return {d.name for d in defs}
def d(source): x, = Script(source).infer() return x.name