Skip to content

Commit

Permalink
Add 'disable current file linting' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Rudnyh committed Feb 2, 2016
1 parent 3cf39e0 commit 711651b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ "keys": ["ctrl+alt+8"], "command": "flake8_lint" },
{ "keys": ["ctrl+alt+9"], "command": "flake8_next_error" }
{ "keys": ["ctrl+alt+9"], "command": "flake8_next_error" },
{ "keys": ["ctrl+alt+0"], "command": "flake8_disable" }
]
3 changes: 2 additions & 1 deletion Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ "keys": ["ctrl+super+8"], "command": "flake8_lint" },
{ "keys": ["ctrl+super+9"], "command": "flake8_next_error" }
{ "keys": ["ctrl+super+9"], "command": "flake8_next_error" },
{ "keys": ["ctrl+super+0"], "command": "flake8_disable" }
]
3 changes: 2 additions & 1 deletion Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ "keys": ["ctrl+alt+shift+8"], "command": "flake8_lint" },
{ "keys": ["ctrl+alt+shift+9"], "command": "flake8_next_error" }
{ "keys": ["ctrl+alt+shift+9"], "command": "flake8_next_error" },
{ "keys": ["ctrl+alt+shift+0"], "command": "flake8_disable" }
]
3 changes: 2 additions & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ "caption": "User: Python Flake8 Lint", "command": "flake8_lint" },
{ "caption": "User: Jump to next Flake8 Lint error", "command": "flake8_next_error" }
{ "caption": "User: Jump to next Flake8 Lint error", "command": "flake8_next_error" },
{ "caption": "User: Disable Python Flake8 Lint for this file", "command": "flake8_disable"}
]
30 changes: 30 additions & 0 deletions Flake8Lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'ignore', 'select', 'ignore_files', 'pep8_max_line_length'
)

DISABLED_VIEWS = set()
ERRORS_IN_VIEWS = {}
PLUGIN_DIR = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -1064,6 +1065,9 @@ def on_file_load(view=None, retry=False):
@staticmethod
def cleanup(view):
"""Clear regions and statusbar."""
# cleanup errors in cache
ERRORS_IN_VIEWS.pop(view.id(), None)

# we need to always clear regions. three situations here:
# - we need to clear regions with fixed previous errors
# - is user will turn off 'highlight' in settings and then run lint
Expand All @@ -1079,6 +1083,11 @@ def do_lint(view, quiet=False):
"""Do view lint."""
log("run flake8 lint")

if view.id() in DISABLED_VIEWS:
log("skip lint because view linting is disabled")
Flake8Lint.cleanup(view)
return

# check if view is scratch
if view.is_scratch():
log("skip lint because view is scratch")
Expand Down Expand Up @@ -1206,13 +1215,28 @@ def async_lint(view, view_settings, quiet=False):
LintReport(view, errors_list, view_settings, quiet=quiet)


class Flake8DisableCommand(sublime_plugin.TextCommand):
"""Disable current view linting."""

def run(self, edit):
"""Disable current view linting."""
DISABLED_VIEWS.add(self.view.id())
Flake8Lint.cleanup(self.view)


class Flake8NextErrorCommand(sublime_plugin.TextCommand):
"""Jump to next lint error command."""

def run(self, edit):
"""Jump to next lint error."""
log("jump to next lint error")

if self.view.id() in DISABLED_VIEWS:
log("view lint is disabled")
if not sublime.ok_cancel_dialog("Enable lint for this view?"):
return
DISABLED_VIEWS.remove(self.view.id())

view_errors = ERRORS_IN_VIEWS.get(self.view.id())
if not view_errors:
log("no view errors found")
Expand Down Expand Up @@ -1248,6 +1272,12 @@ class Flake8LintCommand(sublime_plugin.TextCommand):

def run(self, edit):
"""Run flake8 lint."""
if self.view.id() in DISABLED_VIEWS:
log("view lint is disabled")
if not sublime.ok_cancel_dialog("Enable lint for this view?"):
return
DISABLED_VIEWS.remove(self.view.id())

Flake8Lint.do_lint(self.view)


Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,11 @@ Use these keys (by default) to jump to next lint error:
* OS X: <kbd>Ctrl+Super+9</kbd>
* Linux: <kbd>Ctrl+Alt+9</kbd>
* Windows: <kbd>Ctrl+Alt+Shift+9</kbd>

Use these keys (by default) to disable current file linting:

* OS X: <kbd>Ctrl+Super+0</kbd>
* Linux: <kbd>Ctrl+Alt+0</kbd>
* Windows: <kbd>Ctrl+Alt+Shift+0</kbd>

[![Commands list](https://habrastorage.org/files/1b3/fe1/32f/1b3fe132f6f24516a59474486b56a224.png)](https://habrastorage.org/files/1b3/fe1/32f/1b3fe132f6f24516a59474486b56a224.png)

0 comments on commit 711651b

Please sign in to comment.