mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-11 23:34:17 +00:00
Fix --show-source when indented with tabs
This commit is contained in:
parent
d43d498d41
commit
e8c79dcd33
2 changed files with 24 additions and 11 deletions
|
|
@ -171,10 +171,13 @@ class BaseFormatter(object):
|
||||||
|
|
||||||
# Because column numbers are 1-indexed, we need to remove one to get
|
# Because column numbers are 1-indexed, we need to remove one to get
|
||||||
# the proper number of space characters.
|
# the proper number of space characters.
|
||||||
pointer = (" " * (error.column_number - 1)) + "^"
|
indent = "".join(
|
||||||
|
c if c.isspace() else " "
|
||||||
|
for c in error.physical_line[: error.column_number - 1]
|
||||||
|
)
|
||||||
# Physical lines have a newline at the end, no need to add an extra
|
# Physical lines have a newline at the end, no need to add an extra
|
||||||
# one
|
# one
|
||||||
return error.physical_line + pointer
|
return "{}{}^".format(error.physical_line, indent)
|
||||||
|
|
||||||
def _write(self, output): # type: (str) -> None
|
def _write(self, output): # type: (str) -> None
|
||||||
"""Handle logic of whether to use an output file or print()."""
|
"""Handle logic of whether to use an output file or print()."""
|
||||||
|
|
|
||||||
|
|
@ -65,19 +65,29 @@ def test_show_source_returns_nothing_when_there_is_source():
|
||||||
) == ''
|
) == ''
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('line, column', [
|
@pytest.mark.parametrize(('line1', 'line2', 'column'), [
|
||||||
('x=1\n', 2),
|
(
|
||||||
(' x=(1\n +2)\n', 5),
|
'x=1\n',
|
||||||
# TODO(sigmavirus24): Add more examples
|
' ^',
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
' x=(1\n +2)\n',
|
||||||
|
' ^',
|
||||||
|
5,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'\tx\t=\ty\n',
|
||||||
|
'\t \t \t^',
|
||||||
|
6,
|
||||||
|
),
|
||||||
])
|
])
|
||||||
def test_show_source_updates_physical_line_appropriately(line, column):
|
def test_show_source_updates_physical_line_appropriately(line1, line2, column):
|
||||||
"""Ensure the error column is appropriately indicated."""
|
"""Ensure the error column is appropriately indicated."""
|
||||||
formatter = base.BaseFormatter(options(show_source=True))
|
formatter = base.BaseFormatter(options(show_source=True))
|
||||||
error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line)
|
error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line1)
|
||||||
output = formatter.show_source(error)
|
output = formatter.show_source(error)
|
||||||
assert output
|
assert output == line1 + line2
|
||||||
_, pointer = output.rsplit('\n', 1)
|
|
||||||
assert pointer.count(' ') == (column - 1)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('tee', [False, True])
|
@pytest.mark.parametrize('tee', [False, True])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue