Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ace5b8f

Browse files
committed
* 'master' of https://github.com/DonJayamanne/pythonVSCode: Add code to make definition lookup work with latest version of Jedi (DonJayamanne#1085) Add PEP 526 AutoCompletion (DonJayamanne#1102)
2 parents 60804a5 + 4c0e740 commit ace5b8f

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

pythonFiles/completion.py

+58-14
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,7 @@ def _top_definition(self, definition):
313313
return d
314314
return definition
315315

316-
def _extract_range(self, definition):
317-
"""Provides the definition range of a given definition
318-
319-
For regular symbols it returns the start and end location of the
320-
characters making up the symbol.
321-
322-
For scoped containers it will return the entire definition of the
323-
scope.
324-
325-
The scope that jedi provides ends with the first character of the next
326-
scope so it's not ideal. For vscode we need the scope to end with the
327-
last character of actual code. That's why we extract the lines that
328-
make up our scope and trim the trailing whitespace.
329-
"""
316+
def _extract_range_jedi_0_9_0(self, definition):
330317
from jedi import common
331318
from jedi.parser.utils import load_parser
332319
# get the scope range
@@ -366,6 +353,63 @@ def _extract_range(self, definition):
366353
'end_line': definition.line - 1,
367354
'end_column': definition.column
368355
}
356+
357+
def _extract_range_jedi_0_10_1(self, definition):
358+
from jedi import common
359+
from jedi.parser.python import parse
360+
# get the scope range
361+
try:
362+
if definition.type in ['class', 'function']:
363+
tree_name = definition._name.tree_name
364+
scope = tree_name.get_definition()
365+
start_line = scope.start_pos[0] - 1
366+
start_column = scope.start_pos[1]
367+
# get the lines
368+
code = scope.get_code(include_prefix=False)
369+
lines = common.splitlines(code)
370+
# trim the lines
371+
lines = '\n'.join(lines).rstrip().split('\n')
372+
end_line = start_line + len(lines) - 1
373+
end_column = len(lines[-1]) - 1
374+
else:
375+
symbol = definition._name.tree_name
376+
start_line = symbol.start_pos[0] - 1
377+
start_column = symbol.start_pos[1]
378+
end_line = symbol.end_pos[0] - 1
379+
end_column = symbol.end_pos[1]
380+
return {
381+
'start_line': start_line,
382+
'start_column': start_column,
383+
'end_line': end_line,
384+
'end_column': end_column
385+
}
386+
except Exception as e:
387+
return {
388+
'start_line': definition.line - 1,
389+
'start_column': definition.column,
390+
'end_line': definition.line - 1,
391+
'end_column': definition.column
392+
}
393+
394+
def _extract_range(self, definition):
395+
"""Provides the definition range of a given definition
396+
397+
For regular symbols it returns the start and end location of the
398+
characters making up the symbol.
399+
400+
For scoped containers it will return the entire definition of the
401+
scope.
402+
403+
The scope that jedi provides ends with the first character of the next
404+
scope so it's not ideal. For vscode we need the scope to end with the
405+
last character of actual code. That's why we extract the lines that
406+
make up our scope and trim the trailing whitespace.
407+
"""
408+
if jedi.__version__ in ('0.9.0', '0.10.0'):
409+
return self._extract_range_jedi_0_9_0(definition)
410+
else:
411+
return self._extract_range_jedi_0_10_1(definition)
412+
369413
def _get_definitionsx(self, definitions, identifier=None, ignoreNoModulePath=False):
370414
"""Serialize response to be read from VSCode.
371415

pythonFiles/preview/jedi/evaluate/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def eval_statement(self, stmt, seek_name=None):
162162
types = finder.check_tuple_assignments(self, types, seek_name)
163163

164164
first_operation = stmt.first_operation()
165-
if first_operation not in ('=', None) and not isinstance(stmt, er.InstanceElement): # TODO don't check for this.
165+
if first_operation not in ('=', None) and not isinstance(stmt, er.InstanceElement) and first_operation.type == 'operator': # TODO don't check for this.
166166
# `=` is always the last character in aug assignments -> -1
167167
operator = copy.copy(first_operation)
168168
operator.value = operator.value[:-1]
@@ -327,6 +327,8 @@ def _eval_element_not_cached(self, element):
327327
types = types
328328
elif element.type == 'eval_input':
329329
types = self._eval_element_not_cached(element.children[0])
330+
elif element.type == 'annassign':
331+
types = self.eval_element(element.children[1])
330332
else:
331333
types = precedence.calculate_children(self, element.children)
332334
debug.dbg('eval_element result %s', types)

pythonFiles/preview/jedi/parser/tree.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1522,9 +1522,14 @@ class ExprStmt(BaseNode, DocstringMixin):
15221522
__slots__ = ()
15231523

15241524
def get_defined_names(self):
1525-
return list(chain.from_iterable(_defined_names(self.children[i])
1526-
for i in range(0, len(self.children) - 2, 2)
1527-
if '=' in self.children[i + 1].value))
1525+
names = []
1526+
if self.children[1].type == 'annassign':
1527+
names = _defined_names(self.children[0])
1528+
return list(chain.from_iterable(
1529+
_defined_names(self.children[i])
1530+
for i in range(0, len(self.children) - 2, 2)
1531+
if '=' in self.children[i + 1].value)
1532+
) + names
15281533

15291534
def get_rhs(self):
15301535
"""Returns the right-hand-side of the equals."""

0 commit comments

Comments
 (0)