Home Download Buy Blog Forum Support

Auto-format settings file

Auto-format settings file

Postby schlamar on Wed Jul 17, 2013 7:08 am

ST sometimes auto-formats my Preferences.sublime-settings file (which means sorting by keys and indenting nested entries).

This auto-formatting is per se actually not bad but it happens unpredictable and I can't see a way to trigger it manually. This is especially bad if you have the settings file tracked with a VCS and sometimes just everything has changed due to auto-format (example commit: https://github.com/schlamar/ST3User/com ... 7447510fc6). So:

- Is there a trigger for this auto-format?
- If not, can we have a command and/or settings to trigger this manually?

Thanks
schlamar
 
Posts: 102
Joined: Tue Feb 19, 2013 2:11 pm

Re: Auto-format settings file

Postby iamntz on Wed Jul 17, 2013 8:01 am

AFAIK the autoformat is triggered every time you do a settings without changing the file manually. E.g.: changing theme, changing font-size, etc.

Not sure how configurable is this though...
iamntz
 
Posts: 814
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

Re: Auto-format settings file

Postby schlamar on Wed Jul 17, 2013 10:23 am

Oh, thanks for pointing me in the right direction. In fact auto-format is triggered by sublime.save_settings.

So always triggering auto-format when a settings file is saved is pretty easy:

Code: Select all
import os

import sublime
import sublime_plugin

class AutoFormatSettings(sublime_plugin.EventListener):

    def on_post_save(self, view):
        name = view.file_name()
        if not name:
            return

        base = os.path.basename(name)
        _, ext = os.path.splitext(base)
        if ext == '.sublime-settings':
            sublime.save_settings(base)
schlamar
 
Posts: 102
Joined: Tue Feb 19, 2013 2:11 pm

Re: Auto-format settings file

Postby schlamar on Wed Jul 17, 2013 11:10 am

Oh this implementation is actually bad because it overrides the settings before Sublime was able to reload them :)

Fixed version is at: https://github.com/schlamar/ST3User/blo ... _format.py
schlamar
 
Posts: 102
Joined: Tue Feb 19, 2013 2:11 pm

Re: Auto-format settings file

Postby schlamar on Wed Jul 17, 2013 1:11 pm

Feel free to use it :)
schlamar
 
Posts: 102
Joined: Tue Feb 19, 2013 2:11 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 20 guests