Browse files

Apply the charset on pre_save instead of load

  • Loading branch information...
sindresorhus committed Aug 26, 2012
1 parent ce8aa62 commit f03d2c94205f5dd07772cbd0d80c17119c6ad050
Showing with 15 additions and 4 deletions.
  1. +15 −4
@@ -18,6 +18,12 @@
class EditorConfig(sublime_plugin.EventListener):
def on_load(self, view):
+ self.init(view, False)
+ def on_pre_save(self, view):
+ self.init(view, True)
+ def init(self, view, pre_save):
path = view.file_name()
if not path:
@@ -27,14 +33,21 @@ def on_load(self, view):
print 'Error occurred while getting EditorConfig properties'
if config:
- self.apply_config(view, config)
+ if pre_save:
+ self.apply_charset(view, config)
+ else:
+ self.apply_config(view, config)
+ def apply_charset(self, view, config):
+ charset = config.get('charset')
+ if charset in CHARSETS:
+ view.set_encoding(CHARSETS[charset])
def apply_config(self, view, config):
settings = view.settings()
indent_style = config.get('indent_style')
indent_size = config.get('indent_size')
end_of_line = config.get('end_of_line')
- charset = config.get('charset')
trim_trailing_whitespace = config.get('trim_trailing_whitespace')
insert_final_newline = config.get('insert_final_newline')
if indent_style == 'space':
@@ -48,8 +61,6 @@ def apply_config(self, view, config):
if end_of_line in LINE_ENDINGS:
- if charset in CHARSETS and view.encoding() not in ['Undefined', CHARSETS[charset]]:
- view.run_command('save', {'encoding': CHARSETS[charset]})
if trim_trailing_whitespace == 'true':
settings.set('trim_trailing_white_space_on_save', True)
elif trim_trailing_whitespace == 'false':

3 comments on commit f03d2c9


sindresorhus replied Aug 26, 2012

@treyhunner I now apply the encoding on pre_save instead of load, this makes it also work with new files. I have tested it and it works fine for me. Could you give it a quick look and let me know if you agree or if there are something that can be improved/simplified?


treyhunner replied Aug 26, 2012

@sindresorhus That method (pre-save charset update) definitely sounds correct.

I want to write generic tests for the v0.10.0 properties for the plugin tests I've written. At that point I will test try to test this property in Sublime. This may take me a while though.


sindresorhus replied Aug 26, 2012

Ok, thanks for looking :)

Please sign in to comment.