Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions file_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ def diff_content(self, view):
content = view.substr(sublime.Region(0, view.size()))
return content

def get_buffer_line_endings(self):
"""Detect buffer line ending."""
line_sep = self.view.line_endings()
if line_sep == 'CR':
return '\r'
elif line_sep == 'Unix':
return '\n'
elif line_sep == 'Windows':
return '\r\n'

return os.linesep

def set_line_endings(self, content, line_ending):
"""Normalize all line endings in content to specified line ending."""
if not line_ending:
return content
# First normalize to LF, then convert to target
content = content.replace('\r\n', '\n').replace('\r', '\n')
if line_ending != '\n':
content = content.replace('\n', line_ending)
return content

def prep_content(self, ab, file_name, default_name):
content = ab.splitlines(True)
if file_name is None:
Expand Down Expand Up @@ -132,7 +154,8 @@ def diff_with_external(self, external_command, a, b, from_file=None, to_file=Non
files_to_remove.append(tmp_file.name)

with codecs.open(from_file, encoding='utf-8', mode='w+') as tmp_file:
tmp_file.write(a)
a_tmp = self.set_line_endings(a, self.get_buffer_line_endings())
tmp_file.write(a_tmp)

if not to_file_on_disk:
tmp_file = tempfile.NamedTemporaryFile(dir = sublime.packages_path(), prefix = "file-diffs-", suffix = ".temp", delete=False)
Expand All @@ -141,7 +164,8 @@ def diff_with_external(self, external_command, a, b, from_file=None, to_file=Non
files_to_remove.append(tmp_file.name)

with codecs.open(to_file, encoding='utf-8', mode='w+') as tmp_file:
tmp_file.write(b)
b_tmp = self.set_line_endings(b, self.get_buffer_line_endings())
tmp_file.write(b_tmp)

trim_trailing_white_space_before_diff = get_setting('trim_trailing_white_space_before_diff', False)
if trim_trailing_white_space_before_diff:
Expand Down