Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jedi-0.18.2-py3.8.egg/jedi/api/refactoring/__init__.py: 15%
149 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:56 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:56 +0000
1import difflib
2from pathlib import Path
3from typing import Dict, Iterable, Tuple
5from parso import split_lines
7from jedi.api.exceptions import RefactoringError
9EXPRESSION_PARTS = (
10 'or_test and_test not_test comparison '
11 'expr xor_expr and_expr shift_expr arith_expr term factor power atom_expr'
12).split()
15class ChangedFile:
16 def __init__(self, inference_state, from_path, to_path,
17 module_node, node_to_str_map):
18 self._inference_state = inference_state
19 self._from_path = from_path
20 self._to_path = to_path
21 self._module_node = module_node
22 self._node_to_str_map = node_to_str_map
24 def get_diff(self):
25 old_lines = split_lines(self._module_node.get_code(), keepends=True)
26 new_lines = split_lines(self.get_new_code(), keepends=True)
28 # Add a newline at the end if it's missing. Otherwise the diff will be
29 # very weird. A `diff -u file1 file2` would show the string:
30 #
31 # \ No newline at end of file
32 #
33 # This is not necessary IMO, because Jedi does not really play with
34 # newlines and the ending newline does not really matter in Python
35 # files. ~dave
36 if old_lines[-1] != '':
37 old_lines[-1] += '\n'
38 if new_lines[-1] != '':
39 new_lines[-1] += '\n'
41 project_path = self._inference_state.project.path
42 if self._from_path is None:
43 from_p = ''
44 else:
45 try:
46 from_p = self._from_path.relative_to(project_path)
47 except ValueError: # Happens it the path is not on th project_path
48 from_p = self._from_path
49 if self._to_path is None:
50 to_p = ''
51 else:
52 try:
53 to_p = self._to_path.relative_to(project_path)
54 except ValueError:
55 to_p = self._to_path
56 diff = difflib.unified_diff(
57 old_lines, new_lines,
58 fromfile=str(from_p),
59 tofile=str(to_p),
60 )
61 # Apparently there's a space at the end of the diff - for whatever
62 # reason.
63 return ''.join(diff).rstrip(' ')
65 def get_new_code(self):
66 return self._inference_state.grammar.refactor(self._module_node, self._node_to_str_map)
68 def apply(self):
69 if self._from_path is None:
70 raise RefactoringError(
71 'Cannot apply a refactoring on a Script with path=None'
72 )
74 with open(self._from_path, 'w', newline='') as f:
75 f.write(self.get_new_code())
77 def __repr__(self):
78 return '<%s: %s>' % (self.__class__.__name__, self._from_path)
81class Refactoring:
82 def __init__(self, inference_state, file_to_node_changes, renames=()):
83 self._inference_state = inference_state
84 self._renames = renames
85 self._file_to_node_changes = file_to_node_changes
87 def get_changed_files(self) -> Dict[Path, ChangedFile]:
88 def calculate_to_path(p):
89 if p is None:
90 return p
91 p = str(p)
92 for from_, to in renames:
93 if p.startswith(str(from_)):
94 p = str(to) + p[len(str(from_)):]
95 return Path(p)
97 renames = self.get_renames()
98 return {
99 path: ChangedFile(
100 self._inference_state,
101 from_path=path,
102 to_path=calculate_to_path(path),
103 module_node=next(iter(map_)).get_root_node(),
104 node_to_str_map=map_
105 ) for path, map_ in sorted(self._file_to_node_changes.items())
106 }
108 def get_renames(self) -> Iterable[Tuple[Path, Path]]:
109 """
110 Files can be renamed in a refactoring.
111 """
112 return sorted(self._renames)
114 def get_diff(self):
115 text = ''
116 project_path = self._inference_state.project.path
117 for from_, to in self.get_renames():
118 text += 'rename from %s\nrename to %s\n' \
119 % (from_.relative_to(project_path), to.relative_to(project_path))
121 return text + ''.join(f.get_diff() for f in self.get_changed_files().values())
123 def apply(self):
124 """
125 Applies the whole refactoring to the files, which includes renames.
126 """
127 for f in self.get_changed_files().values():
128 f.apply()
130 for old, new in self.get_renames():
131 old.rename(new)
134def _calculate_rename(path, new_name):
135 dir_ = path.parent
136 if path.name in ('__init__.py', '__init__.pyi'):
137 return dir_, dir_.parent.joinpath(new_name)
138 return path, dir_.joinpath(new_name + path.suffix)
141def rename(inference_state, definitions, new_name):
142 file_renames = set()
143 file_tree_name_map = {}
145 if not definitions:
146 raise RefactoringError("There is no name under the cursor")
148 for d in definitions:
149 tree_name = d._name.tree_name
150 if d.type == 'module' and tree_name is None:
151 p = None if d.module_path is None else Path(d.module_path)
152 file_renames.add(_calculate_rename(p, new_name))
153 else:
154 # This private access is ok in a way. It's not public to
155 # protect Jedi users from seeing it.
156 if tree_name is not None:
157 fmap = file_tree_name_map.setdefault(d.module_path, {})
158 fmap[tree_name] = tree_name.prefix + new_name
159 return Refactoring(inference_state, file_tree_name_map, file_renames)
162def inline(inference_state, names):
163 if not names:
164 raise RefactoringError("There is no name under the cursor")
165 if any(n.api_type in ('module', 'namespace') for n in names):
166 raise RefactoringError("Cannot inline imports, modules or namespaces")
167 if any(n.tree_name is None for n in names):
168 raise RefactoringError("Cannot inline builtins/extensions")
170 definitions = [n for n in names if n.tree_name.is_definition()]
171 if len(definitions) == 0:
172 raise RefactoringError("No definition found to inline")
173 if len(definitions) > 1:
174 raise RefactoringError("Cannot inline a name with multiple definitions")
175 if len(names) == 1:
176 raise RefactoringError("There are no references to this name")
178 tree_name = definitions[0].tree_name
180 expr_stmt = tree_name.get_definition()
181 if expr_stmt.type != 'expr_stmt':
182 type_ = dict(
183 funcdef='function',
184 classdef='class',
185 ).get(expr_stmt.type, expr_stmt.type)
186 raise RefactoringError("Cannot inline a %s" % type_)
188 if len(expr_stmt.get_defined_names(include_setitem=True)) > 1:
189 raise RefactoringError("Cannot inline a statement with multiple definitions")
190 first_child = expr_stmt.children[1]
191 if first_child.type == 'annassign' and len(first_child.children) == 4:
192 first_child = first_child.children[2]
193 if first_child != '=':
194 if first_child.type == 'annassign':
195 raise RefactoringError(
196 'Cannot inline a statement that is defined by an annotation'
197 )
198 else:
199 raise RefactoringError(
200 'Cannot inline a statement with "%s"'
201 % first_child.get_code(include_prefix=False)
202 )
204 rhs = expr_stmt.get_rhs()
205 replace_code = rhs.get_code(include_prefix=False)
207 references = [n for n in names if not n.tree_name.is_definition()]
208 file_to_node_changes = {}
209 for name in references:
210 tree_name = name.tree_name
211 path = name.get_root_context().py__file__()
212 s = replace_code
213 if rhs.type == 'testlist_star_expr' \
214 or tree_name.parent.type in EXPRESSION_PARTS \
215 or tree_name.parent.type == 'trailer' \
216 and tree_name.parent.get_next_sibling() is not None:
217 s = '(' + replace_code + ')'
219 of_path = file_to_node_changes.setdefault(path, {})
221 n = tree_name
222 prefix = n.prefix
223 par = n.parent
224 if par.type == 'trailer' and par.children[0] == '.':
225 prefix = par.parent.children[0].prefix
226 n = par
227 for some_node in par.parent.children[:par.parent.children.index(par)]:
228 of_path[some_node] = ''
229 of_path[n] = prefix + s
231 path = definitions[0].get_root_context().py__file__()
232 changes = file_to_node_changes.setdefault(path, {})
233 changes[expr_stmt] = _remove_indent_of_prefix(expr_stmt.get_first_leaf().prefix)
234 next_leaf = expr_stmt.get_next_leaf()
236 # Most of the time we have to remove the newline at the end of the
237 # statement, but if there's a comment we might not need to.
238 if next_leaf.prefix.strip(' \t') == '' \
239 and (next_leaf.type == 'newline' or next_leaf == ';'):
240 changes[next_leaf] = ''
241 return Refactoring(inference_state, file_to_node_changes)
244def _remove_indent_of_prefix(prefix):
245 r"""
246 Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
247 """
248 return ''.join(split_lines(prefix, keepends=True)[:-1])