Skip to content

Commit 7397387

Browse files
q3xq3rrchl
andauthored
Fix UnicodeDecodeError on non-UTF-8 files (#151)
Co-authored-by: Rafal Chlodnicki <rchl2k@gmail.com>
1 parent 80697d7 commit 7397387

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

trailing_spaces.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,7 @@ def modified_lines_as_numbers(old, new):
283283
#
284284
# Returns the list of regions matching dirty lines.
285285
def get_modified_lines(view):
286-
try:
287-
on_disk
288-
on_buffer = view.substr(sublime.Region(0, view.size())).splitlines()
289-
except UnicodeDecodeError:
290-
sublime.status_message("File format incompatible with this feature (UTF-8 files only)")
291-
return
292-
286+
on_buffer = view.substr(sublime.Region(0, view.size())).splitlines()
293287
lines = []
294288
line_numbers = modified_lines_as_numbers(on_disk, on_buffer)
295289
if line_numbers:
@@ -469,7 +463,19 @@ def freeze_last_version(self, view):
469463
# For some reasons, the on_activated hook gets fired on a ghost document
470464
# from time to time.
471465
if file_name and not view.is_scratch() and isfile(file_name):
472-
with codecs.open(file_name, "r", "utf-8") as f:
466+
encoding = view.encoding()
467+
468+
if encoding == "Undefined":
469+
encoding = view.settings().get("default_encoding", "UTF-8")
470+
471+
if encoding == "Hexadecimal": # not supported?
472+
on_disk = None
473+
return
474+
475+
match = re.match(r'.+\(([^)]+)\)$', encoding)
476+
encoding = match.group(1) if match else encoding
477+
478+
with codecs.open(file_name, "r", encoding) as f:
473479
on_disk = f.read().splitlines()
474480

475481
def is_view_visible(self, view):

0 commit comments

Comments
 (0)