Skip to content

Commit e9e63a8

Browse files
committed
Add "Reload with Encoding" menu items and command
When ConvertToUTF8 failed to detect the encoding correctly, either because of insufficient bytes from file or improper settings, we need to manually specify the source file encoding. However, ConvertToUTF8 doesn't have this kind of settings/utility. A simple test case is a GBK encoded text file with only two Chinese characters "回家" (it may contain other ASCII characters). EUC-JP will be detected as the source encoding and the Chinese characters are rendered as "指社". As far as I have tested, "File" -> "Set File Encoding to" won't help in this case. Now, you have two choices to manually specify the source encoding when it goes wrong: 1) "File" -> "Reload with Encoding" -> Choose the correct file encoding 2) Ctrl+Shift+P to activate the "Command Palette..." and search for "ConvertToUTF8: Reload with Encoding", select it and then choose the correct file encoding.
1 parent 8b488c4 commit e9e63a8

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

ConvertToUTF8.py

+23
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,31 @@ def on_done(self, selected):
222222
def show_selection(view):
223223
EncodingSelection(view).start()
224224

225+
class ReloadWithEncoding(threading.Thread):
226+
def __init__(self, view, encoding):
227+
threading.Thread.__init__(self)
228+
self.view = view
229+
self.encoding = encoding
230+
231+
def run(self):
232+
sublime.set_timeout(self.reload, 0)
233+
234+
def reload(self):
235+
init_encoding_vars(self.view, self.encoding)
236+
237+
def reload_encoding(view, encoding):
238+
ReloadWithEncoding(view, encoding).start()
239+
225240
stamps = {}
226241

242+
class ShowEncodingSelectionCommand(sublime_plugin.TextCommand):
243+
def run(self, edit):
244+
show_selection(self.view)
245+
246+
class ReloadWithEncodingCommand(sublime_plugin.TextCommand):
247+
def run(self, edit, encoding):
248+
reload_encoding(self.view, encoding)
249+
227250
class PyInstructionCommand(sublime_plugin.TextCommand):
228251
def run(self, edit, encoding):
229252
self.view.set_name('ConvertToUTF8 Instructions')

Default.sublime-commands

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"caption": "ConvertToUTF8: Reload with Encoding",
4+
"command": "show_encoding_selection"
5+
}
6+
]

Main.sublime-menu

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@
1616
{ "caption": "-" },
1717
{ "caption": "UTF-8", "command": "convert_to_utf8", "args": {"encoding": "UTF-8", "stamp": "0" } }
1818
]
19-
}
19+
},
20+
{
21+
"caption": "Reload with Encoding",
22+
"children":
23+
[
24+
{ "caption": "Chinese Simplified (GBK)", "command": "reload_with_encoding", "args": {"encoding": "GBK"} },
25+
{ "caption": "Chinese Traditional (BIG5)", "command": "reload_with_encoding", "args": {"encoding": "BIG5-HKSCS"} },
26+
{ "caption": "Korean (EUC-KR)", "command": "reload_with_encoding", "args": {"encoding": "EUC-KR"} },
27+
{ "caption": "Japanese (CP932)", "command": "reload_with_encoding", "args": {"encoding": "CP932"} },
28+
{ "caption": "Japanese (Shift_JIS)", "command": "reload_with_encoding", "args": {"encoding": "Shift_JIS"} },
29+
{ "caption": "Japanese (EUC-JP)", "command": "reload_with_encoding", "args": {"encoding": "EUC-JP"} },
30+
{ "caption": "-" },
31+
{ "caption": "UTF-8", "command": "reload_with_encoding", "args": {"encoding": "UTF-8"} }
32+
]
33+
}
2034
]
2135
}
2236
]

0 commit comments

Comments
 (0)