Bug 1387555 - [mozlint] Make use of quickfix when using --edit with vim/nvim, r=dylan

MozReview-Commit-ID: BlJbWVv1CeO

--HG--
extra : rebase_source : 005f72d9f74ccd86ae73a298acde6b0b3fd6a550
This commit is contained in:
Andrew Halberstadt 2017-08-09 10:03:02 -04:00
Родитель 81abb80919
Коммит 411adfbc83
5 изменённых файлов: 87 добавлений и 7 удалений

Просмотреть файл

@ -5,7 +5,6 @@
from __future__ import print_function, unicode_literals
import os
import subprocess
import sys
from argparse import REMAINDER, ArgumentParser
@ -142,6 +141,7 @@ def find_linters(linters=None):
def run(paths, linters, fmt, outgoing, workdir, edit, list_linters=None, **lintargs):
from mozlint import LintRoller, formatters
from mozlint.editor import edit_results
if list_linters:
lint_paths = find_linters(linters)
@ -156,10 +156,8 @@ def run(paths, linters, fmt, outgoing, workdir, edit, list_linters=None, **linta
# run all linters
results = lint.roll(paths, outgoing=outgoing, workdir=workdir)
if edit:
editor = os.environ['EDITOR']
for path in results:
subprocess.call([editor, path])
if edit and results:
edit_results(results)
results = lint.roll(results.keys())
formatter = formatters.get(fmt)

Просмотреть файл

@ -0,0 +1,53 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import unicode_literals, print_function
import os
import subprocess
import tempfile
from mozlint import formatters
def get_editor():
return os.environ.get('EDITOR')
def edit_results(results):
if not results:
return
editor = get_editor()
if not editor:
print('warning: could not find a default editor')
return
name = os.path.basename(editor)
if name in ('vim', 'nvim', 'gvim'):
cmd = [
editor,
# need errorformat to match both Error and Warning, with or without a column
'--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %trror\\ -\\ %m',
'--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %tarning\\ -\\ %m',
'--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ %trror\\ -\\ %m',
'--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ %tarning\\ -\\ %m',
# start with quickfix window opened
'-c', 'copen',
# running with -q seems to open an empty buffer in addition to the
# first file, this removes that empty buffer
'-c', '1bd',
]
with tempfile.NamedTemporaryFile() as fh:
s = formatters.get('compact', summary=False)(results)
fh.write(s)
fh.flush()
cmd.extend(['-q', fh.name])
subprocess.call(cmd)
else:
for path, errors in results.iteritems():
subprocess.call([editor, path])

Просмотреть файл

@ -13,6 +13,7 @@ class CompactFormatter(object):
This formatter prints one error per line, mimicking the
eslint 'compact' formatter.
"""
# If modifying this format, please also update the vim errorformats in editor.py
fmt = "{path}: line {lineno}{column}, {level} - {message} ({rule})"
def __init__(self, summary=True):

Просмотреть файл

@ -47,8 +47,8 @@ def test_cli_run_with_edit(run, parser, capfd):
assert ret == 1
assert len(out) == 5
assert out[0].endswith('foobar.js') # from the `echo` editor
assert "files/foobar.js: line 1, col 1, Error" in out[1]
assert "files/foobar.js: line 2, col 1, Error" in out[2]
assert "foobar.js: line 1, col 1, Error" in out[1]
assert "foobar.js: line 2, col 1, Error" in out[2]
del os.environ['EDITOR']
with pytest.raises(SystemExit):

Просмотреть файл

@ -75,3 +75,31 @@ To enable a pre-commit git hook, run the following command:
.. parsed-literal::
$ ln -s /path/to/gecko/tools/lint/hooks.py .git/hooks/pre-commit
Fixing Lint Errors
==================
``Mozlint`` has a best-effort ability to fix lint errors:
.. parsed-literal::
$ ./mach lint --fix
Not all linters support fixing, and even the ones that do can not usually fix
all types of errors. Any errors that cannot be automatically fixed, will be
printed to stdout like normal. In that case, you can also fix errors manually:
.. parsed-literal::
$ ./mach lint --edit
This requires the $EDITOR environment variable be defined. For most editors,
this will simply open each file containing errors one at a time. For vim (or
neovim), this will populate the `quickfix list`_ with the errors.
The ``--fix`` and ``--edit`` arguments can be combined, in which case any
errors that can be fixed automatically will be, and the rest will be opened
with your $EDITOR.
.. _quickfix list: http://vimdoc.sourceforge.net/htmldoc/quickfix.html