1
0
forked from VimPlug/jedi

remove user_stmt and user_scope stuff - yes!

This commit is contained in:
Dave Halter
2014-01-17 02:33:53 +01:00
parent fc1899ecd4
commit b30a186f8f
5 changed files with 19 additions and 121 deletions

View File

@@ -214,7 +214,7 @@ def save_parser(path, name, parser, pickling=True):
class ParserPickling(object):
version = 8
version = 9
"""
Version number (integer) for file system cache.

View File

@@ -17,7 +17,7 @@ annotations.
import re
from jedi.evaluate.cache import memoize_default
from jedi.parser import Parser
from jedi.parser.user_context import UserContextParser
DOCSTRING_PARAM_PATTERNS = [
r'\s*:type\s+%s:\s*([^\n]+)', # Sphinx
@@ -49,10 +49,10 @@ def follow_param(evaluator, param):
param_str)
user_position = (2, 0)
p = Parser(param_str, None, user_position, no_docstr=True)
if p.user_stmt is None:
p = UserContextParser(param_str, None, user_position, no_docstr=True)
if p.user_stmt() is None:
return []
return evaluator.eval_statement(p.user_stmt)
return evaluator.eval_statement(p.user_stmt())
return []
@@ -115,8 +115,8 @@ def find_return_types(evaluator, func):
if not type_str:
return []
p = Parser(type_str, None, (1, 0), no_docstr=True)
if p.user_stmt is None:
p = UserContextParser(type_str, None, (1, 0), no_docstr=True)
if p.user_stmt() is None:
return []
p.user_stmt.parent = func
p.user_stmt().parent = func
return list(evaluator.eval_statement(p.user_stmt))

View File

@@ -43,9 +43,6 @@ class Parser(object):
def __init__(self, source, module_path=None, user_position=None,
no_docstr=False, offset=(0, 0), is_fast_parser=None,
top_module=None):
self.user_position = user_position
self.user_scope = None
self.user_stmt = None
self.no_docstr = no_docstr
self.start_pos = self.end_pos = 1 + offset[0], offset[1]
@@ -85,28 +82,6 @@ class Parser(object):
def __repr__(self):
return "<%s: %s>" % (type(self).__name__, self.module)
def _check_user_stmt(self, simple):
# this is not user checking, just update the used_names
for tok_name in self.module.temp_used_names:
try:
self.module.used_names[tok_name].add(simple)
except KeyError:
self.module.used_names[tok_name] = set([simple])
self.module.temp_used_names = []
if not self.user_position:
return
# the position is right
if simple.start_pos <= self.user_position <= simple.end_pos:
if self.user_stmt is not None:
# if there is already a user position (another import, because
# imports are splitted) the names are checked.
for n in simple.get_set_vars():
if n.start_pos < self.user_position <= n.end_pos:
self.user_stmt = simple
else:
self.user_stmt = simple
def _parse_dot_name(self, pre_used_token=None):
"""
The dot name parser parses a name, variable or function and returns
@@ -252,11 +227,7 @@ class Parser(object):
return None
# because of 2 line func param definitions
scope = pr.Function(self.module, fname, params, first_pos, annotation)
if self.user_scope and scope != self.user_scope \
and self.user_position > first_pos:
self.user_scope = scope
return scope
return pr.Function(self.module, fname, params, first_pos, annotation)
def _parse_class(self):
"""
@@ -286,12 +257,7 @@ class Parser(object):
debug.warning("class syntax: %s@%s", cname, self.start_pos[0])
return None
# because of 2 line class initializations
scope = pr.Class(self.module, cname, super, first_pos)
if self.user_scope and scope != self.user_scope \
and self.user_position > first_pos:
self.user_scope = scope
return scope
return pr.Class(self.module, cname, super, first_pos)
def _parse_statement(self, pre_used_token=None, added_breaks=None,
stmt_class=pr.Statement, names_are_set_vars=False):
@@ -405,7 +371,6 @@ class Parser(object):
names_are_set_vars=names_are_set_vars)
stmt.parent = self.top_module
self._check_user_stmt(stmt)
if tok in always_break + not_first_break:
self._gen.push_last_back()
@@ -438,15 +403,6 @@ class Parser(object):
s = s.parent
raise
if self.user_position and (
self.start_pos[0] == self.user_position[0]
or self.user_scope is None
and self.start_pos[0] >= self.user_position[0]
):
debug.dbg('user scope found [%s] = %s',
self.parserline.replace('\n', ''), self._scope)
self.user_scope = self._scope
self._current = typ, tok
return self._current
@@ -522,12 +478,10 @@ class Parser(object):
end_pos = self.end_pos if count + 1 == len(imports) else e
i = pr.Import(self.module, first_pos, end_pos, m,
alias, defunct=defunct)
self._check_user_stmt(i)
self._scope.add_import(i)
if not imports:
i = pr.Import(self.module, first_pos, self.end_pos, None,
defunct=True)
self._check_user_stmt(i)
self.freshscope = False
elif tok == 'from':
defunct = False
@@ -559,7 +513,6 @@ class Parser(object):
i = pr.Import(self.module, first_pos, end_pos, name,
alias, mod, star, relative_count,
defunct=defunct or defunct2)
self._check_user_stmt(i)
self._scope.add_import(i)
self.freshscope = False
# loops

View File

@@ -31,9 +31,6 @@ class Module(pr.Simple, pr.Module):
parsers. """
with common.ignored(AttributeError):
del self._used_names
for p in self.parsers:
p.user_scope = None
p.user_stmt = None
def __getattr__(self, name):
if name.startswith('__'):
@@ -107,7 +104,6 @@ class ParserNode(object):
for key, c in self._contents.items():
setattr(scope, key, list(c))
scope.is_generator = self._is_generator
self.parser.user_scope = self.parser.module
if self.parent is None:
# Global vars of the first one can be deleted, in the global scope
@@ -189,8 +185,6 @@ class FastParser(use_metaclass(CachedFastParser)):
def __init__(self, code, module_path=None, user_position=None):
# set values like `pr.Module`.
self.module_path = module_path
self.user_position = user_position
self._user_scope = None
self.current_node = None
self.parsers = []
@@ -204,30 +198,6 @@ class FastParser(use_metaclass(CachedFastParser)):
self.parsers[:] = []
raise
@property
@cache.underscore_memoization
def user_scope(self):
user_scope = None
for p in self.parsers:
if p.user_scope:
if isinstance(p.user_scope, pr.SubModule):
continue
user_scope = p.user_scope
if isinstance(user_scope, pr.SubModule) or user_scope is None:
user_scope = self.module
return user_scope
@property
@cache.underscore_memoization
def user_stmt(self):
user_stmt = None
for p in self.parsers:
if p.user_stmt:
user_stmt = p.user_stmt
break
return user_stmt
def update(self, code, user_position=None):
self.user_position = user_position
self.reset_caches()
@@ -239,14 +209,6 @@ class FastParser(use_metaclass(CachedFastParser)):
self.parsers[:] = []
raise
def _scan_user_scope(self, sub_module):
""" Scan with self.user_position. """
for scope in sub_module.statements + sub_module.subscopes:
if isinstance(scope, pr.Scope):
if scope.start_pos <= self.user_position <= scope.end_pos:
return self._scan_user_scope(scope) or scope
return None
def _split_parts(self, code):
"""
Split the code into different parts. This makes it possible to parse
@@ -377,12 +339,6 @@ class FastParser(use_metaclass(CachedFastParser)):
else:
self.current_node = self.current_node.add_node(node)
if self.current_node.parent and (isinstance(p.user_scope, pr.SubModule)
or p.user_scope is None) \
and self.user_position \
and p.start_pos <= self.user_position < p.end_pos:
p.user_scope = self.current_node.parent.content_scope
self.parsers.append(p)
is_first = False
@@ -411,8 +367,7 @@ class FastParser(use_metaclass(CachedFastParser)):
if nodes[index].code != code:
raise ValueError()
except ValueError:
p = Parser(parser_code, self.module_path,
self.user_position, offset=(line_offset, 0),
p = Parser(parser_code, self.module_path, offset=(line_offset, 0),
is_fast_parser=True, top_module=self.module,
no_docstr=no_docstr)
p.module.parent = self.module
@@ -424,24 +379,10 @@ class FastParser(use_metaclass(CachedFastParser)):
p = node.parser
m = p.module
m.line_offset += line_offset + 1 - m.start_pos[0]
if self.user_position is not None and \
m.start_pos[0] <= self.user_position[0] <= m.end_pos[0]:
# It's important to take care of the whole user
# positioning stuff, if no reparsing is being done.
p.user_stmt = m.get_statement_for_position(
self.user_position, include_imports=True)
if p.user_stmt:
p.user_scope = p.user_stmt.parent
else:
p.user_scope = self._scan_user_scope(m) or m
return p, node
def reset_caches(self):
with common.ignored(AttributeError):
del self._user_scope
with common.ignored(AttributeError):
del self._user_stmt
self.module.reset_caches()
if self.current_node is not None:
self.current_node.reset_contents()

View File

@@ -3,8 +3,8 @@ import os
import sys
from jedi import cache
from jedi.parser import tokenize
from jedi.parser import fast
from jedi.parser import tokenize, Parser
from jedi.parser.fast import FastParser
from jedi.parser import representation
from jedi import debug
@@ -181,16 +181,20 @@ class UserContext(object):
class UserContextParser(object):
def __init__(self, source, path, position, user_context):
def __init__(self, source, path, position, user_context=None, no_docstr=False):
self._source = source
self._path = path and os.path.abspath(path)
self._position = position
self._user_context = user_context
self._no_docstr = no_docstr
@cache.underscore_memoization
def _parser(self):
cache.invalidate_star_import_cache(self._path)
parser = fast.FastParser(self._source, self._path, self._position)
if self._no_docstr:
parser = Parser(self._source, self._path, self._position, no_docstr=self._no_docstr)
else:
parser = FastParser(self._source, self._path, self._position)
# Don't pickle that module, because the main module is changing quickly
cache.save_parser(self._path, None, parser, pickling=False)
return parser