Skip to content

Commit

Permalink
Merge pull request #95 from SublimeLinter/filter-codes
Browse files Browse the repository at this point in the history
Implement ignoring auto-fixable codes
  • Loading branch information
kaste authored Sep 24, 2018
2 parents e2df27a + 476da1f commit 6653a9e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 22 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ This linter plugin for [SublimeLinter](https://github.com/SublimeLinter/SublimeL

SublimeLinter must be installed in order to use this plugin.

Please install via [Package Control](https://packagecontrol.io).
Install via [Package Control](https://packagecontrol.io) or `git clone` as usual.

Before installing this plugin, ensure that `flake8` is installed on your system.
To install `flake8`, do the following:
Ensure that a `flake8` is actually installed somewhere on your system. Typically, `pip install flake8` on the command line will do that.

1. Install [Python](http://python.org) and [pip](http://www.pip-installer.org/en/latest/installing.html) (Python 3 requires pip3).
If you want to use a globally installed flake, make sure that it is available on the PATH. Before going any further, please read and follow the steps in ["Finding a linter executable"](http://sublimelinter.com/en/latest/troubleshooting.html#finding-a-linter-executable) through "Validating your PATH" in the documentation.

1. Install `flake8` (2.1 or later) by typing the following in a terminal:
```
# For python2
[sudo] pip install flake8
Otherwise configure ["executable"](http://www.sublimelinter.com/en/latest/linter_settings.html#executable) or the ["python"](http://www.sublimelinter.com/en/latest/linter_settings.html#python) setting.

# For python 3
[sudo] pip3 install flake8
```

Please make sure that the path to `flake8` is available to SublimeLinter. Before going any further, please read and follow the steps in ["Finding a linter executable"](http://sublimelinter.com/en/latest/troubleshooting.html#finding-a-linter-executable) through "Validating your PATH" in the documentation.
If you use pipenv, and you're working on a project with a Pipfile, everything should be automatic.


## Settings

- SublimeLinter settings: http://sublimelinter.com/en/latest/settings.html
- Linter settings: http://sublimelinter.com/en/latest/linter_settings.html

SublimeLinter-flake8 works with common flake8 [configuration files](http://flake8.pycqa.org/en/latest/user/configuration.html#configuration-locations) and inline overrides.
Additional setting:

- 'ignore_fixables' (Default: True): Filter warnings that Sublime can fix automatically (e.g. trailing white-space) on save.

SublimeLinter-flake8 works with common flake8 [configuration files](http://flake8.pycqa.org/en/latest/user/configuration.html#configuration-locations) and inline overrides. Note that, by default the [working dir](http://www.sublimelinter.com/en/latest/linter_settings.html#working-dir) is set to an open folder attached to the current window of Sublime. This is of importance if your config files are located in a subfolder for example.

Use ["args"](http://www.sublimelinter.com/en/latest/linter_settings.html#args) if you want to set some arguments just like on the command line.

61 changes: 60 additions & 1 deletion linter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import logging
from SublimeLinter.lint import PythonLinter
import re


logger = logging.getLogger('SublimeLinter.plugins.flake8')


CAPTURE_WS = re.compile(r'(\s+)')
CAPTURE_IMPORT_ID = re.compile(r'^\'(?:.*\.)?(.+)\'')

Expand All @@ -10,7 +14,10 @@ class Flake8(PythonLinter):

cmd = ('flake8', '--format', 'default', '${args}', '-')
defaults = {
'selector': 'source.python'
'selector': 'source.python',

# Ignore codes Sublime can auto-fix
'ignore_fixables': True
}

# The following regex marks these pyflakes and pep8 codes as errors.
Expand Down Expand Up @@ -40,6 +47,58 @@ class Flake8(PythonLinter):
)
multiline = True

def on_stderr(self, stderr):
# For python 3.7 we actually have the case that flake yields
# FutureWarnings. We just eat those as they're irrelevant here. Note
# that we try to eat the subsequent line as well which usually contains
# the culprit source line.
stderr = re.sub(r'^.+FutureWarning.+\n(.*\n?)?', '', stderr, re.M)
stderr = re.sub(r'^.+DeprecationWarning.+\n(.*\n?)?', '', stderr, re.M)

if stderr:
self.notify_failure()
logger.error(stderr)

def parse_output(self, proc, virtual_view):
settings = self.get_view_settings()
errors = super().parse_output(proc, virtual_view)

if not settings.get('ignore_fixables', True):
return errors

trims_ws = self.view.settings().get('trim_trailing_white_space_on_save')
ensures_newline = self.view.settings().get('ensure_newline_at_eof_on_save')

if not (trims_ws or ensures_newline):
return errors

filtered_errors = []
for error in errors:
code = error['code']

if ensures_newline and code == 'W292':
continue

if trims_ws and code in ('W291', 'W293'):
continue

if trims_ws and code == 'W391':
# Fixable if one WS line is at EOF, except the view only has
# one line.
lines = len(virtual_view._newlines) - 1
if (
virtual_view.select_line(lines - 1).strip() == ''
and (
lines < 2
or virtual_view.select_line(lines - 2).strip() != ''
)
):
continue

filtered_errors.append(error)

return filtered_errors

def reposition_match(self, line, col, m, virtual_view):
"""Reposition white-space errors."""
code = m.error or m.warning
Expand Down
2 changes: 1 addition & 1 deletion messages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"install": "messages/install.txt",
"4.0.0-rc.1": "messages/4.0.0-rc.1.txt"
"4.2.0": "messages/4.2.0.txt"
}
6 changes: 0 additions & 6 deletions messages/4.0.0-rc.1.txt

This file was deleted.

24 changes: 24 additions & 0 deletions messages/4.2.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SublimeLinter-flake8 4.2.0
--------------------------

Less noise, hide problems that can be fixed automatically!

Sublime has a basic code formatter built in. E.g. it will remove trailing white-space on save. (The related Sublime settings here are "trim_trailing_white_space_on_save" and "ensure_newline_at_eof_on_save".)

Starting with this release, we will filter (hide) these errors automatically. We introduce a new setting "ignore_fixables" (True by default) which controls this behavior.

Maybe you already know black, the new, zero-config code formatter for python. And in case you use it, you may have noticed that flake8 now produces a lot of noise which black can fix for you. There is an addon https://packagecontrol.io/packages/SublimeLinter-addon-black-for-flake which will automatically filter all warnings black can fix. Try it!


Notable enhancement: Mark unused imported identifiers correct. E.g. in the following code,

from x import (
x,
y,
z
)

we now correctly highlight x, y, and z.

Fix: Tighten compatibility with python 3.7

0 comments on commit 6653a9e

Please sign in to comment.