mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Make the diff nicer if there is no ending newline, fixes #1581
This commit is contained in:
@@ -25,11 +25,33 @@ class ChangedFile(object):
|
||||
def get_diff(self):
|
||||
old_lines = split_lines(self._module_node.get_code(), keepends=True)
|
||||
new_lines = split_lines(self.get_new_code(), keepends=True)
|
||||
|
||||
# Add a newline at the end if it's missing. Otherwise the diff will be
|
||||
# very weird. A `diff -u file1 file2` would show the string:
|
||||
#
|
||||
# \ No newline at end of file
|
||||
#
|
||||
# This is not necessary IMO, because Jedi does not really play with
|
||||
# newlines and the ending newline does not really matter in Python
|
||||
# files. ~dave
|
||||
if old_lines[-1] != '':
|
||||
old_lines[-1] += '\n'
|
||||
if new_lines[-1] != '':
|
||||
new_lines[-1] += '\n'
|
||||
|
||||
project_path = self._inference_state.project._path
|
||||
if self._from_path is None:
|
||||
from_p = ''
|
||||
else:
|
||||
from_p = relpath(self._from_path, project_path)
|
||||
if self._to_path is None:
|
||||
to_p = ''
|
||||
else:
|
||||
to_p = relpath(self._to_path, project_path)
|
||||
diff = difflib.unified_diff(
|
||||
old_lines, new_lines,
|
||||
fromfile=relpath(self._from_path, project_path),
|
||||
tofile=relpath(self._to_path, project_path),
|
||||
fromfile=from_p,
|
||||
tofile=to_p,
|
||||
)
|
||||
# Apparently there's a space at the end of the diff - for whatever
|
||||
# reason.
|
||||
|
||||
@@ -62,3 +62,17 @@ def test_rename_none_path(Script):
|
||||
with pytest.raises(jedi.RefactoringError, match='on a Script with path=None'):
|
||||
refactoring.apply()
|
||||
assert refactoring
|
||||
|
||||
|
||||
def test_diff_without_ending_newline(Script):
|
||||
refactoring = Script('a = 1\nb\na').rename(1, 0, new_name='c')
|
||||
assert refactoring.get_diff() == dedent('''\
|
||||
---
|
||||
+++
|
||||
@@ -1,3 +1,3 @@
|
||||
-a = 1
|
||||
+c = 1
|
||||
b
|
||||
-a
|
||||
+c
|
||||
''')
|
||||
|
||||
Reference in New Issue
Block a user