1
0
forked from VimPlug/jedi

Make the diff nicer if there is no ending newline, fixes #1581

This commit is contained in:
Dave Halter
2020-05-14 00:18:16 +02:00
parent 3104443212
commit 381fbeda6a
2 changed files with 38 additions and 2 deletions

View File

@@ -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.