1
0
forked from VimPlug/jedi

Readability for completion parts.

This commit is contained in:
Dave Halter
2016-05-19 11:33:17 +02:00
parent 323581e253
commit 055ff8be23
3 changed files with 24 additions and 20 deletions

View File

@@ -156,11 +156,13 @@ class Script(object):
:return: Completion objects, sorted by name and __ comes last. :return: Completion objects, sorted by name and __ comes last.
:rtype: list of :class:`classes.Completion` :rtype: list of :class:`classes.Completion`
""" """
debug.speed('completions start')
path = self._user_context.get_path_until_cursor()
completion = Completion( completion = Completion(
self._evaluator, self._parser, self._user_context, self._evaluator, self._parser, self._user_context,
self._pos, self.call_signatures self._pos, self.call_signatures
) )
return completion.completions() return completion.completions(path)
def goto_definitions(self): def goto_definitions(self):
""" """

View File

@@ -21,7 +21,7 @@ class Completion:
self._pos = position self._pos = position
self._call_signatures_method = call_signatures_method self._call_signatures_method = call_signatures_method
def get_completions(self, user_stmt, path, dot, like): def get_completions(self, user_stmt, completion_parts):
# TODO this closure is ugly. it also doesn't work with # TODO this closure is ugly. it also doesn't work with
# simple_complete (used for Interpreter), somehow redo. # simple_complete (used for Interpreter), somehow redo.
module = self._evaluator.wrap(self._parser.module()) module = self._evaluator.wrap(self._parser.module())
@@ -50,7 +50,7 @@ class Completion:
return completion_names return completion_names
if names is None and not isinstance(user_stmt, tree.Import): if names is None and not isinstance(user_stmt, tree.Import):
if not path and not dot: if not completion_parts.path and not completion_parts.has_dot:
# add keywords # add keywords
completion_names += keywords.completion_names( completion_names += keywords.completion_names(
self._evaluator, self._evaluator,
@@ -59,23 +59,21 @@ class Completion:
module) module)
# TODO delete? We should search for valid parser # TODO delete? We should search for valid parser
# transformations. # transformations.
completion_names += self._simple_complete(path, dot, like) completion_names += self._simple_complete(completion_parts)
return completion_names return completion_names
def completions(self): def completions(self, path):
debug.speed('completions start')
path = self._user_context.get_path_until_cursor()
# Dots following an int are not the start of a completion but a float # Dots following an int are not the start of a completion but a float
# literal. # literal.
if re.search(r'^\d\.$', path): if re.search(r'^\d\.$', path):
return [] return []
path, dot, like = helpers.completion_parts(path) completion_parts = helpers.get_completion_parts(path)
user_stmt = self._parser.user_stmt_with_whitespace() user_stmt = self._parser.user_stmt_with_whitespace()
completion_names = self.get_completions(user_stmt, path, dot, like) completion_names = self.get_completions(user_stmt, completion_parts)
if not dot: if not completion_parts.has_dot:
# add named params # add named params
for call_sig in self._call_signatures_method(): for call_sig in self._call_signatures_method():
# Allow protected access, because it's a public API. # Allow protected access, because it's a public API.
@@ -89,20 +87,20 @@ class Completion:
if p._definition.stars == 0: # no *args/**kwargs if p._definition.stars == 0: # no *args/**kwargs
completion_names.append(p._name) completion_names.append(p._name)
needs_dot = not dot and path needs_dot = not completion_parts.has_dot and path
comps = [] comps = []
comp_dct = {} comp_dct = {}
for c in set(completion_names): for c in set(completion_names):
n = str(c) n = str(c)
if settings.case_insensitive_completion \ if settings.case_insensitive_completion \
and n.lower().startswith(like.lower()) \ and n.lower().startswith(completion_parts.name.lower()) \
or n.startswith(like): or n.startswith(completion_parts.name):
if isinstance(c.parent, (tree.Function, tree.Class)): if isinstance(c.parent, (tree.Function, tree.Class)):
# TODO I think this is a hack. It should be an # TODO I think this is a hack. It should be an
# er.Function/er.Class before that. # er.Function/er.Class before that.
c = self._evaluator.wrap(c.parent).name c = self._evaluator.wrap(c.parent).name
new = classes.Completion(self._evaluator, c, needs_dot, len(like)) new = classes.Completion(self._evaluator, c, needs_dot, len(completion_parts.name))
k = (new.name, new.complete) # key k = (new.name, new.complete) # key
if k in comp_dct and settings.no_completion_duplicates: if k in comp_dct and settings.no_completion_duplicates:
comp_dct[k]._same_name_completions.append(new) comp_dct[k]._same_name_completions.append(new)
@@ -116,8 +114,8 @@ class Completion:
x.name.startswith('_'), x.name.startswith('_'),
x.name.lower())) x.name.lower()))
def _simple_complete(self, path, dot, like): def _simple_complete(self, completion_parts):
if not path and not dot: if not completion_parts.path and not completion_parts.has_dot:
scope = self._parser.user_scope() scope = self._parser.user_scope()
if not scope.is_scope(): # Might be a flow (if/while/etc). if not scope.is_scope(): # Might be a flow (if/while/etc).
scope = scope.get_parent_scope() scope = scope.get_parent_scope()
@@ -135,12 +133,12 @@ class Completion:
names, self._parser.user_stmt(), pos names, self._parser.user_stmt(), pos
) )
elif inference.get_under_cursor_stmt(self._evaluator, self._parser, elif inference.get_under_cursor_stmt(self._evaluator, self._parser,
path, self._pos) is None: completion_parts.path, self._pos) is None:
return [] return []
else: else:
scopes = list(inference.type_inference( scopes = list(inference.type_inference(
self._evaluator, self._parser, self._user_context, self._evaluator, self._parser, self._user_context,
self._pos, path, is_completion=True self._pos, completion_parts.path, is_completion=True
)) ))
completion_names = [] completion_names = []
debug.dbg('possible completion scopes: %s', scopes) debug.dbg('possible completion scopes: %s', scopes)

View File

@@ -2,18 +2,22 @@
Helpers for the API Helpers for the API
""" """
import re import re
from collections import namedtuple
from jedi.parser import tree as pt from jedi.parser import tree as pt
from jedi.evaluate import imports from jedi.evaluate import imports
def completion_parts(path_until_cursor): CompletionParts = namedtuple('CompletionParts', ['path', 'has_dot', 'name'])
def get_completion_parts(path_until_cursor):
""" """
Returns the parts for the completion Returns the parts for the completion
:return: tuple - (path, dot, like) :return: tuple - (path, dot, like)
""" """
match = re.match(r'^(.*?)(\.|)(\w?[\w\d]*)$', path_until_cursor, flags=re.S) match = re.match(r'^(.*?)(\.|)(\w?[\w\d]*)$', path_until_cursor, flags=re.S)
return match.groups() path, dot, name = match.groups()
return CompletionParts(path, bool(dot), name)
def sorted_definitions(defs): def sorted_definitions(defs):