1
0
forked from VimPlug/jedi

Get the first rename test passing

This commit is contained in:
Dave Halter
2020-02-10 17:42:23 +01:00
parent 6166e7961e
commit 13b393a5e3
6 changed files with 126 additions and 39 deletions

View File

@@ -523,6 +523,37 @@ class Script(object):
]
return sorted(filter(def_ref_filter, defs), key=lambda x: (x.line, x.column))
def rename(self, line, column, new_name):
"""
Returns an object that you can use to rename the variable under the
cursor and its references to a different name.
"""
definitions = self.get_references(line, column, include_builtins=False)
file_renames = []
file_tree_name_map = {}
for d in definitions:
if d.type == 'module':
file_renames.append((d.module_path, new))
else:
# This private access is ok in a way. It's not public to
# protect Jedi users from seeing it.
tree_name = d._name.tree_name
if tree_name is not None:
fmap = file_tree_name_map.setdefault(d.module_path, {})
fmap[tree_name] = tree_name.prefix + new_name
from jedi.api.refactoring import Refactoring
return Refactoring(self._grammar, file_tree_name_map, file_renames)
def extract_variable(self):
"""
"""
raise NotImplementedError
def extract_method(self):
"""
"""
raise NotImplementedError
class Interpreter(Script):
"""

View File

@@ -8,3 +8,7 @@ class InternalError(_JediError):
class WrongVersion(_JediError):
pass
class RefactoringError(_JediError):
pass

64
jedi/api/refactoring.py Normal file
View File

@@ -0,0 +1,64 @@
import difflib
from parso import split_lines
class ChangedFile(object):
def __init__(self, grammar, path, module_node, node_to_str_map):
self._grammar = grammar
self._path = path
self._module_node = module_node
self._node_to_str_map = node_to_str_map
def get_diff(self):
old_lines = split_lines(self._module_node.get_code(), keepends=True)
new_lines = split_lines(self.get_code(), keepends=True)
diff = difflib.unified_diff(
old_lines, new_lines,
fromfile=self._path,
tofile=self._path
)
# Apparently there's a space at the end of the diff - for whatever
# reason.
return ''.join(diff).rstrip(' ')
def get_code(self):
return self._grammar.refactor(self._module_node, self._node_to_str_map)
def apply(self):
with open(self._path, 'w') as f:
f.write(self.get_code())
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self._path)
class Refactoring(object):
def __init__(self, grammar, file_to_node_changes, renames=()):
self._grammar = grammar
self._renames = renames
self._file_to_node_changes = file_to_node_changes
def get_changed_files(self):
return [
ChangedFile(self._grammar, path, next(iter(map_)).get_root_node(), map_)
for path, map_ in self._file_to_node_changes.items()
]
def get_renames(self):
"""
Files can be renamed in a refactoring.
Returns ``Iterable[Tuple[str, str]]``.
"""
return self._renames
def get_diff(self):
return ''.join(f.get_diff() for f in self.get_changed_files())
def apply(self):
for old, new in self._renames:
rename(old, new)
for f in self.get_changed_files():
f.apply()