wistful / SublimeAutoPEP8

Automatically formats Python code to conform to the PEP 8 style guide using autopep8 and pep8 modules
144 stars 23 forks source link

Add setting to only format entire file on save, regardless of text selection #28

Closed kevincon closed 10 years ago

kevincon commented 10 years ago

TLDR: Could you add a setting for the plugin (or change the behavior) so that I can disable formatting text selection when format_on_save is enabled?

I love, love, love this Sublime Text plugin, especially the ability for the plugin to automatically format a file on save, but something that's really annoying is that if I, for example:

  1. Highlight a block of text
  2. Comment it out using Sublime's shortcut (cmd + /)
  3. Save using Sublime's shortcut (cmd + s)

Then the plugin tries to only format the selected text, resulting in some really strange text manipulation.

As an example, (with format_on_save enabled), try pasting the following code snippet in a file, highlighting the line 'month': obj.month,, commenting it out using cmd+/, and then saving using cmd+s:

if isinstance(obj, datetime):
            return {
                '__type__': 'datetime',
                'year': obj.year,
                'month': obj.month,
                'day': obj.day,
                'hour': obj.hour,
                'minute': obj.minute,
                'second': obj.second,
                'microsecond': obj.microsecond,
            }

For me, this turns into:

if isinstance(obj, datetime):
            return {
                '__type__': 'datetime',
                'year': obj.year,
                # 'month':
    obj.month,
nth,

                'day': obj.day,
                'hour': obj.hour,
                'minute': obj.minute,
                'second': obj.second,
                'microsecond': obj.microsecond,
            }

Of course, if I just don't have the text selected when I save, then there's no problem. But for those times that I forget, this behavior is super annoying, because I have to undo several times. Sometimes the plugin even fights back, so I have to continuously hit undo and save to beat the plugin back from screwing up my file.

So could you add a setting for the plugin (or change the behavior) so that I can disable formatting text selection when format_on_save is enabled?

Or can you help me configure the plugin as it is right now somehow to achieve this behavior?

Thanks again for your plugin!

wistful commented 10 years ago

Hi Kevin. Thanks for detailed explanation of issue. It's never happened with me, but I believe autosave should ignore selected blocks. Issue was fixed in the version 1.1.2.

kevincon commented 10 years ago

It works great now! Thanks so much for the fast fix!