Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/DonJayamanne/pythonVSCode:
  Add code to make definition lookup work with latest version of Jedi (#1085)
  Add PEP 526 AutoCompletion (#1102)
  • Loading branch information
DonJayamanne committed Jul 20, 2017
2 parents 60804a5 + 4c0e740 commit ace5b8f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
72 changes: 58 additions & 14 deletions pythonFiles/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,7 @@ def _top_definition(self, definition):
return d
return definition

def _extract_range(self, definition):
"""Provides the definition range of a given definition
For regular symbols it returns the start and end location of the
characters making up the symbol.
For scoped containers it will return the entire definition of the
scope.
The scope that jedi provides ends with the first character of the next
scope so it's not ideal. For vscode we need the scope to end with the
last character of actual code. That's why we extract the lines that
make up our scope and trim the trailing whitespace.
"""
def _extract_range_jedi_0_9_0(self, definition):
from jedi import common
from jedi.parser.utils import load_parser
# get the scope range
Expand Down Expand Up @@ -366,6 +353,63 @@ def _extract_range(self, definition):
'end_line': definition.line - 1,
'end_column': definition.column
}

def _extract_range_jedi_0_10_1(self, definition):
from jedi import common
from jedi.parser.python import parse
# get the scope range
try:
if definition.type in ['class', 'function']:
tree_name = definition._name.tree_name
scope = tree_name.get_definition()
start_line = scope.start_pos[0] - 1
start_column = scope.start_pos[1]
# get the lines
code = scope.get_code(include_prefix=False)
lines = common.splitlines(code)
# trim the lines
lines = '\n'.join(lines).rstrip().split('\n')
end_line = start_line + len(lines) - 1
end_column = len(lines[-1]) - 1
else:
symbol = definition._name.tree_name
start_line = symbol.start_pos[0] - 1
start_column = symbol.start_pos[1]
end_line = symbol.end_pos[0] - 1
end_column = symbol.end_pos[1]
return {
'start_line': start_line,
'start_column': start_column,
'end_line': end_line,
'end_column': end_column
}
except Exception as e:
return {
'start_line': definition.line - 1,
'start_column': definition.column,
'end_line': definition.line - 1,
'end_column': definition.column
}

def _extract_range(self, definition):
"""Provides the definition range of a given definition
For regular symbols it returns the start and end location of the
characters making up the symbol.
For scoped containers it will return the entire definition of the
scope.
The scope that jedi provides ends with the first character of the next
scope so it's not ideal. For vscode we need the scope to end with the
last character of actual code. That's why we extract the lines that
make up our scope and trim the trailing whitespace.
"""
if jedi.__version__ in ('0.9.0', '0.10.0'):
return self._extract_range_jedi_0_9_0(definition)
else:
return self._extract_range_jedi_0_10_1(definition)

def _get_definitionsx(self, definitions, identifier=None, ignoreNoModulePath=False):
"""Serialize response to be read from VSCode.
Expand Down
4 changes: 3 additions & 1 deletion pythonFiles/preview/jedi/evaluate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def eval_statement(self, stmt, seek_name=None):
types = finder.check_tuple_assignments(self, types, seek_name)

first_operation = stmt.first_operation()
if first_operation not in ('=', None) and not isinstance(stmt, er.InstanceElement): # TODO don't check for this.
if first_operation not in ('=', None) and not isinstance(stmt, er.InstanceElement) and first_operation.type == 'operator': # TODO don't check for this.
# `=` is always the last character in aug assignments -> -1
operator = copy.copy(first_operation)
operator.value = operator.value[:-1]
Expand Down Expand Up @@ -327,6 +327,8 @@ def _eval_element_not_cached(self, element):
types = types
elif element.type == 'eval_input':
types = self._eval_element_not_cached(element.children[0])
elif element.type == 'annassign':
types = self.eval_element(element.children[1])
else:
types = precedence.calculate_children(self, element.children)
debug.dbg('eval_element result %s', types)
Expand Down
11 changes: 8 additions & 3 deletions pythonFiles/preview/jedi/parser/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,9 +1522,14 @@ class ExprStmt(BaseNode, DocstringMixin):
__slots__ = ()

def get_defined_names(self):
return list(chain.from_iterable(_defined_names(self.children[i])
for i in range(0, len(self.children) - 2, 2)
if '=' in self.children[i + 1].value))
names = []
if self.children[1].type == 'annassign':
names = _defined_names(self.children[0])
return list(chain.from_iterable(
_defined_names(self.children[i])
for i in range(0, len(self.children) - 2, 2)
if '=' in self.children[i + 1].value)
) + names

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

0 comments on commit ace5b8f

Please sign in to comment.