mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Don't use a random grammar for extract
This commit is contained in:
@@ -54,11 +54,11 @@ sys.setrecursionlimit(3000)
|
|||||||
def no_py2_support(func):
|
def no_py2_support(func):
|
||||||
# TODO remove when removing Python 2/3.5
|
# TODO remove when removing Python 2/3.5
|
||||||
def wrapper(self, *args, **kwargs):
|
def wrapper(self, *args, **kwargs):
|
||||||
if self._grammar.version_info.major == 2:
|
if self._inference_state.grammar.version_info.major == 2:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"Python 2 is deprecated and won't support this feature anymore"
|
"Python 2 is deprecated and won't support this feature anymore"
|
||||||
)
|
)
|
||||||
if self._grammar.version_info[:2] == (3, 5):
|
if self._inference_state.grammar.version_info[:2] == (3, 5):
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"No support for refactorings on Python 3.5"
|
"No support for refactorings on Python 3.5"
|
||||||
)
|
)
|
||||||
@@ -115,9 +115,6 @@ class Script(object):
|
|||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
source = f.read()
|
source = f.read()
|
||||||
|
|
||||||
# Load the Python grammar of the current interpreter.
|
|
||||||
self._grammar = parso.load_grammar()
|
|
||||||
|
|
||||||
if sys_path is not None and not is_py3:
|
if sys_path is not None and not is_py3:
|
||||||
sys_path = list(map(force_unicode, sys_path))
|
sys_path = list(map(force_unicode, sys_path))
|
||||||
|
|
||||||
@@ -360,7 +357,7 @@ class Script(object):
|
|||||||
return definitions
|
return definitions
|
||||||
leaf = self._module_node.get_leaf_for_position((line, column))
|
leaf = self._module_node.get_leaf_for_position((line, column))
|
||||||
if leaf.type in ('keyword', 'operator', 'error_leaf'):
|
if leaf.type in ('keyword', 'operator', 'error_leaf'):
|
||||||
reserved = self._grammar._pgen_grammar.reserved_syntax_strings.keys()
|
reserved = self._inference_state.grammar._pgen_grammar.reserved_syntax_strings.keys()
|
||||||
if leaf.value in reserved:
|
if leaf.value in reserved:
|
||||||
name = KeywordName(self._inference_state, leaf.value)
|
name = KeywordName(self._inference_state, leaf.value)
|
||||||
return [classes.Definition(self._inference_state, name)]
|
return [classes.Definition(self._inference_state, name)]
|
||||||
@@ -523,7 +520,7 @@ class Script(object):
|
|||||||
return self._names(**kwargs) # Python 2...
|
return self._names(**kwargs) # Python 2...
|
||||||
|
|
||||||
def get_syntax_errors(self):
|
def get_syntax_errors(self):
|
||||||
return parso_to_jedi_errors(self._grammar, self._module_node)
|
return parso_to_jedi_errors(self._inference_state.grammar, self._module_node)
|
||||||
|
|
||||||
def _names(self, all_scopes=False, definitions=True, references=False):
|
def _names(self, all_scopes=False, definitions=True, references=False):
|
||||||
def def_ref_filter(_def):
|
def def_ref_filter(_def):
|
||||||
@@ -554,7 +551,7 @@ class Script(object):
|
|||||||
|
|
||||||
def _rename(self, line, column, new_name): # Python 2...
|
def _rename(self, line, column, new_name): # Python 2...
|
||||||
definitions = self.get_references(line, column, include_builtins=False)
|
definitions = self.get_references(line, column, include_builtins=False)
|
||||||
return refactoring.rename(self._grammar, definitions, new_name)
|
return refactoring.rename(self._inference_state.grammar, definitions, new_name)
|
||||||
|
|
||||||
@no_py2_support
|
@no_py2_support
|
||||||
@validate_line_column
|
@validate_line_column
|
||||||
@@ -576,7 +573,9 @@ class Script(object):
|
|||||||
until_column = len(self._code_lines[until_line - 1])
|
until_column = len(self._code_lines[until_line - 1])
|
||||||
until_pos = until_line, until_column
|
until_pos = until_line, until_column
|
||||||
return extract_variable(
|
return extract_variable(
|
||||||
self._grammar, self.path, self._module_node, new_name, (line, column), until_pos)
|
self._inference_state.grammar, self.path, self._module_node,
|
||||||
|
new_name, (line, column), until_pos
|
||||||
|
)
|
||||||
|
|
||||||
@no_py2_support
|
@no_py2_support
|
||||||
def extract_function(self, line, column, **kwargs):
|
def extract_function(self, line, column, **kwargs):
|
||||||
@@ -607,7 +606,7 @@ class Script(object):
|
|||||||
Inlines a variable under the cursor.
|
Inlines a variable under the cursor.
|
||||||
"""
|
"""
|
||||||
names = [d._name for d in self.get_references(line, column, include_builtins=True)]
|
names = [d._name for d in self.get_references(line, column, include_builtins=True)]
|
||||||
return refactoring.inline(self._grammar, names)
|
return refactoring.inline(self._inference_state.grammar, names)
|
||||||
|
|
||||||
|
|
||||||
class Interpreter(Script):
|
class Interpreter(Script):
|
||||||
|
|||||||
@@ -31,9 +31,9 @@ class RefactoringCase(object):
|
|||||||
f_name = os.path.basename(self._path)
|
f_name = os.path.basename(self._path)
|
||||||
return f_name.replace('.py', '')
|
return f_name.replace('.py', '')
|
||||||
|
|
||||||
def refactor(self):
|
def refactor(self, environment):
|
||||||
project = jedi.Project(os.path.join(test_dir, 'refactor'))
|
project = jedi.Project(os.path.join(test_dir, 'refactor'))
|
||||||
script = jedi.Script(self._code, path=self._path, project=project)
|
script = jedi.Script(self._code, path=self._path, project=project, environment=environment)
|
||||||
refactor_func = getattr(script, self.refactor_type)
|
refactor_func = getattr(script, self.refactor_type)
|
||||||
return refactor_func(self._line_nr, self._index, **self._kwargs)
|
return refactor_func(self._line_nr, self._index, **self._kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ def test_static_analysis(static_analysis_case, environment):
|
|||||||
static_analysis_case.run(assert_static_analysis, environment)
|
static_analysis_case.run(assert_static_analysis, environment)
|
||||||
|
|
||||||
|
|
||||||
def test_refactor(refactor_case, skip_pre_python36):
|
def test_refactor(refactor_case, skip_pre_python36, environment):
|
||||||
"""
|
"""
|
||||||
Run refactoring test case.
|
Run refactoring test case.
|
||||||
|
|
||||||
@@ -64,13 +64,13 @@ def test_refactor(refactor_case, skip_pre_python36):
|
|||||||
"""
|
"""
|
||||||
if refactor_case.type == 'error':
|
if refactor_case.type == 'error':
|
||||||
with pytest.raises(RefactoringError) as e:
|
with pytest.raises(RefactoringError) as e:
|
||||||
refactor_case.refactor()
|
refactor_case.refactor(environment)
|
||||||
assert e.value.args[0] == refactor_case.desired_result.strip()
|
assert e.value.args[0] == refactor_case.desired_result.strip()
|
||||||
elif refactor_case.type == 'text':
|
elif refactor_case.type == 'text':
|
||||||
refactoring = refactor_case.refactor()
|
refactoring = refactor_case.refactor(environment)
|
||||||
assert not refactoring.get_renames()
|
assert not refactoring.get_renames()
|
||||||
text = ''.join(f.get_new_code() for f in refactoring.get_changed_files().values())
|
text = ''.join(f.get_new_code() for f in refactoring.get_changed_files().values())
|
||||||
assert_case_equal(refactor_case, text, refactor_case.desired_result)
|
assert_case_equal(refactor_case, text, refactor_case.desired_result)
|
||||||
else:
|
else:
|
||||||
diff = refactor_case.refactor().get_diff()
|
diff = refactor_case.refactor(environment).get_diff()
|
||||||
assert_case_equal(refactor_case, diff, refactor_case.desired_result)
|
assert_case_equal(refactor_case, diff, refactor_case.desired_result)
|
||||||
|
|||||||
Reference in New Issue
Block a user