diff --git a/README.md b/README.md index f642cac..669e442 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,15 @@ 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 @@ -34,4 +26,11 @@ Please make sure that the path to `flake8` is available to SublimeLinter. Before - 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. + diff --git a/linter.py b/linter.py index 4937da3..00faffc 100644 --- a/linter.py +++ b/linter.py @@ -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'^\'(?:.*\.)?(.+)\'') @@ -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. @@ -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 diff --git a/messages.json b/messages.json index ae59b07..4240581 100644 --- a/messages.json +++ b/messages.json @@ -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" } diff --git a/messages/4.0.0-rc.1.txt b/messages/4.0.0-rc.1.txt deleted file mode 100644 index c332fac..0000000 --- a/messages/4.0.0-rc.1.txt +++ /dev/null @@ -1,6 +0,0 @@ -SublimeLinter-flake8 4.0.0-rc.1 -------------------------------- - -Thank you for using the beta release of SublimeLinter-flake8! -If you run into anything please report at: -https://github.com/SublimeLinter/SublimeLinter3/issues diff --git a/messages/4.2.0.txt b/messages/4.2.0.txt new file mode 100644 index 0000000..82bf146 --- /dev/null +++ b/messages/4.2.0.txt @@ -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 +