Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support ST3; change options list

  • Loading branch information...
commit 8e141db41afbb1d104bd998622bdf0912a8e332e 1 parent f21c81d
@wistful authored
View
20 AutoPep8 (Windows).sublime-settings
@@ -1,11 +1,21 @@
{
+ "max-line-length": 79,
+
+ // list codes for fixes; used by --ignore and --select
+ "list-fixes": "",
+
// do not fix these errors / warnings(e.g. E4, W)
"ignore": "",
+
// select errors / warnings(e.g. E4, W)
"select": "",
- "format_on_save": false,
- // path to python binary which should use to execute autopep8.py module
- "python": "pythonw"
- // path to autopep8 lib
- // , "autopep8": "D:\\sublime\\autopep8.py"
+
+ // enable possibly unsafe changes (E711, E712)
+ "aggressive": 0,
+
+ // verbose level
+ "verbose": 0,
+
+ "format_on_save": false
}
+
View
17 AutoPep8.sublime-settings
@@ -1,10 +1,21 @@
{
+ "max-line-length": 79,
+
+ // list codes for fixes; used by --ignore and --select
+ "list-fixes": "",
+
// do not fix these errors / warnings(e.g. E4, W)
"ignore": "",
+
// select errors / warnings(e.g. E4, W)
"select": "",
+
+ // enable possibly unsafe changes (E711, E712)
+ "aggressive": 0,
+
+ // verbose level
+ "verbose": 0,
+
"format_on_save": false
- // path to python binary which should use to execute autopep8.py module
- // , "python": "/usr/bin/python3"
- // , "autopep8": "/home/sublime/autopep8.py"
}
+
View
104 sublautopep8.py
@@ -1,4 +1,4 @@
-## coding=utf-8
+# coding=utf-8
import os
from collections import namedtuple
import re
@@ -7,42 +7,45 @@
import sublime
import sublime_plugin
-if int(sublime.version()) > 3000:
- import sys
- from io import StringIO
- from imp import reload
-
- try:
- from AutoPEP8.sublimeautopep8lib import autopep8
- except ImportError:
- sys.path.append(os.path.join(sublime.packages_path(), 'AutoPep8', 'sublimeautopep8lib'))
- autopep8 = __import__('autopep8')
- reload(autopep8)
- del sys.path[-1]
-else:
+try:
from StringIO import StringIO
- import sublimeautopep8lib.autopep8 as autopep8
+except ImportError:
+ from io import StringIO
+try:
+ import sublimeautopep8lib.autopep8 as autopep8
+except ImportError:
+ import AutoPEP8.sublimeautopep8lib.autopep8 as autopep8
plugin_path = os.path.split(os.path.abspath(__file__))[0]
pycoding = re.compile("coding[:=]\s*([-\w.]+)")
-base_name = sublime.platform() == 'windows' and 'AutoPep8 (Windows).sublime-settings' or 'AutoPep8.sublime-settings'
+if sublime.platform() == 'windows':
+ base_name = 'AutoPep8 (Windows).sublime-settings'
+else:
+ base_name = 'AutoPep8.sublime-settings'
class AutoPep8(object):
+
"""AutoPep8 Formatter"""
def pep8_params(self):
- params = ['-d', '-vv'] # args for preview
+ params = ['-d'] # args for preview
# read settings
settings = sublime.load_settings(base_name)
- if settings.get("ignore"):
- params.append("--ignore=" + settings.get("ignore"))
- if settings.get("select"):
- params.append("--select=" + settings.get("select"))
+ for opt in ("ignore", "select", "max-line-length"):
+ params.append("--{0}={1}".format(opt, settings.get(opt, "")))
+
+ if settings.get("list-fixes", None):
+ params.append("--{0}={1}".format(opt, settings.get(opt)))
+
+ for opt in ("verbose", ):
+ opt_count = settings.get(opt, 0)
+ params.extend(["--"+opt]*opt_count)
- params.append('fake-arg') # autopep8.parse_args raises exception without it
+ params.append(
+ 'fake-arg') # autopep8.parse_args raises exception without it
return autopep8.parse_args(params)[0]
def _get_diff(self, old, new, filename):
@@ -66,10 +69,8 @@ def new_view(self, encoding, text):
view = sublime.active_window().new_file()
view.set_encoding(encoding)
view.set_syntax_file("Packages/Diff/Diff.tmLanguage")
- edit = view.begin_edit()
- view.insert(edit, 0, text)
- view.end_edit(edit)
- view.set_scratch(1)
+ view.run_command("auto_pep8_output", {"text": text})
+ view.set_scratch(True)
class AutoPep8Command(sublime_plugin.TextCommand, AutoPep8):
@@ -100,8 +101,8 @@ def restore_state(self):
sel.add(sublime.Region(cur_point, cur_point))
# restore viewport
- self.view.set_viewport_position(
- (0.0, 0.0)) # magic, next line doesn't work without it
+ # magic, next line doesn't work without it
+ self.view.set_viewport_position((0.0, 0.0))
self.view.set_viewport_position(self.vector)
def run(self, edit, preview=True):
@@ -134,6 +135,16 @@ def is_visible(self, *args):
return self.view.settings().get('syntax') == "Packages/Python/Python.tmLanguage"
+class AutoPep8OutputCommand(sublime_plugin.TextCommand, AutoPep8):
+
+ def run(self, edit, text):
+ self.view.insert(edit, 0, text)
+ self.view.end_edit(edit)
+
+ def is_visible(self, *args):
+ return False
+
+
class AutoPep8FileCommand(sublime_plugin.WindowCommand, AutoPep8):
file_names = None
@@ -151,7 +162,9 @@ def run(self, paths=None, preview=True):
sublime.status_message(
"autopep8: formatting {path}".format(path=path))
- if not out_data or out_data == in_data or (preview and len(out_data.split('\n')) < 3):
+ if not out_data \
+ or out_data == in_data \
+ or (preview and len(out_data.split('\n')) < 3):
continue
has_changes = True
@@ -191,32 +204,15 @@ def is_visible(self, *args, **kwd):
class AutoPep8Listener(sublime_plugin.EventListener, AutoPep8):
- def on_pre_save(self, view):
+ def on_pre_save_async(self, view):
if not view.settings().get('syntax') == "Packages/Python/Python.tmLanguage" \
or not sublime.load_settings(base_name).get('format_on_save', False):
return
- # save cursor position
- cur_row, cur_col = view.rowcol(view.sel()[0].begin())
- # save viewport
- vector = view.text_to_layout(view.visible_region().begin())
-
- region = sublime.Region(0, view.size())
- source = view.substr(region)
- out_data = self.format_text(source)
- if out_data != source:
- edit = view.begin_edit()
- view.replace(edit, region, out_data)
- view.end_edit(edit)
-
- # restore cursor position
- sel = view.sel()
- if len(sel) == 1 and sel[0].a == sel[0].b:
- cur_point = view.text_point(cur_row, cur_col)
- sel.subtract(sel[0])
- sel.add(sublime.Region(cur_point, cur_point))
-
- # restore viewport
- view.set_viewport_position(
- (0.0, 0.0)) # magic, next line doesn't work without it
- view.set_viewport_position(vector)
+ view.run_command("auto_pep8", {"preview": False})
+
+ def on_pre_save(self, view):
+ if sublime.version() < '3000':
+ self.on_pre_save_async(view)
+
+
View
5 sublimeautopep8lib/autopep8.py
@@ -53,7 +53,10 @@
import difflib
import tempfile
-import pep8
+try:
+ import pep8
+except ImportError:
+ from . import pep8
try:
Please sign in to comment.
Something went wrong with that request. Please try again.