Skip to content

Commit

Permalink
Fix UnicodeDecodeError on non-UTF-8 files (#151)
Browse files Browse the repository at this point in the history
Co-authored-by: Rafal Chlodnicki <[email protected]>
  • Loading branch information
q3xq3r and rchl authored Jun 28, 2021
1 parent 80697d7 commit 7397387
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions trailing_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,7 @@ def modified_lines_as_numbers(old, new):
#
# Returns the list of regions matching dirty lines.
def get_modified_lines(view):
try:
on_disk
on_buffer = view.substr(sublime.Region(0, view.size())).splitlines()
except UnicodeDecodeError:
sublime.status_message("File format incompatible with this feature (UTF-8 files only)")
return

on_buffer = view.substr(sublime.Region(0, view.size())).splitlines()
lines = []
line_numbers = modified_lines_as_numbers(on_disk, on_buffer)
if line_numbers:
Expand Down Expand Up @@ -469,7 +463,19 @@ def freeze_last_version(self, view):
# For some reasons, the on_activated hook gets fired on a ghost document
# from time to time.
if file_name and not view.is_scratch() and isfile(file_name):
with codecs.open(file_name, "r", "utf-8") as f:
encoding = view.encoding()

if encoding == "Undefined":
encoding = view.settings().get("default_encoding", "UTF-8")

if encoding == "Hexadecimal": # not supported?
on_disk = None
return

match = re.match(r'.+\(([^)]+)\)$', encoding)
encoding = match.group(1) if match else encoding

with codecs.open(file_name, "r", encoding) as f:
on_disk = f.read().splitlines()

def is_view_visible(self, view):
Expand Down

0 comments on commit 7397387

Please sign in to comment.