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.completions()]
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 get_completions(code, line, column, path=None): with _jedi_lock: script = Script(code, line, column, path=path) return script.completions()