Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autopep8.py: Add ability to specify a column range #452

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ def fix(self):
results = [r for r in results
if start <= r['line'] <= end]

if self.options.column_range:
start, end = self.options.column_range
results = [r for r in results
if start <= r['column'] <= end]

self._fix_source(filter_results(source=''.join(self.source),
results=results,
aggressive=self.options.aggressive))
Expand Down Expand Up @@ -3592,6 +3597,11 @@ def create_parser():
help='only fix errors found within this inclusive '
'range of line numbers (e.g. 1 99); '
'line numbers are indexed at 1')
parser.add_argument('--column-range', metavar='column',
default=None, type=int, nargs=2,
help='only fix errors found within this inclusive '
'range of column numbers (e.g. 1 99); '
'column numbers are indexed at 1')
parser.add_argument('--indent-size', default=DEFAULT_INDENT_SIZE,
type=int, help=argparse.SUPPRESS)
parser.add_argument('--hang-closing', action='store_true',
Expand Down Expand Up @@ -3677,10 +3687,17 @@ def parse_args(arguments, apply_config=False):

if args.line_range:
if args.line_range[0] <= 0:
parser.error('--range must be positive numbers')
parser.error('--line-range must be positive numbers')
if args.line_range[0] > args.line_range[1]:
parser.error('First value of --range should be less than or equal '
'to the second')
parser.error('First value of --line-range should be less than '
'or equal to the second')

if args.column_range:
if args.column_range[0] <= 0:
parser.error('--column-range must be positive numbers')
if args.column_range[0] > args.column_range[1]:
parser.error('First value of --column-range should be less than '
'or equal to the second')

return args

Expand Down
33 changes: 33 additions & 0 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -4821,6 +4821,36 @@ def test_range_with_broken_syntax(self):
with autopep8_context(line, options=['--line-range', '1', '1']) as result:
self.assertEqual(line, result)

def test_column_range_changes_one_column(self):
line = 'print( 1, a( 2))\n'
fixed = 'print(1, a( 2))\n'
with autopep8_context(line, options=['--column-range', '7', '7']) as result:
self.assertEqual(fixed, result)

def test_column_range_changes_small_range(self):
line = 'print( 1, a( 2))\n'
fixed = 'print(1, a(2))\n'
with autopep8_context(line, options=['--column-range', '7', '13']) as result:
self.assertEqual(fixed, result)

def test_column_range_longer_than_line(self):
line = 'print( 1, a( 2))\n'
fixed = 'print(1, a(2))\n'
with autopep8_context(line, options=['--column-range', '7', '100']) as result:
self.assertEqual(fixed, result)

def test_column_range_multiline_changes_one_column(self):
line = 'print( 1, a( 2))\nprint( 3, b( 4))\n'
fixed = 'print(1, a( 2))\nprint(3, b( 4))\n'
with autopep8_context(line, options=['--column-range', '7', '7']) as result:
self.assertEqual(fixed, result)

def test_column_range_multiline_changes_small_range(self):
line = 'print( 1, a( 2))\nprint(3 , b (4))\n'
fixed = 'print(1, a(2))\nprint(3, b(4))\n'
with autopep8_context(line, options=['--column-range', '7', '13']) as result:
self.assertEqual(fixed, result)


class UtilityFunctionTests(unittest.TestCase):

Expand Down Expand Up @@ -5117,6 +5147,9 @@ def test_invalid_option_combinations(self):
['--line-range', '0', '2', filename],
['--line-range', '2', '1', filename],
['--line-range', '-1', '-1', filename],
['--column-range', '0', '2', filename],
['--column-range', '2', '1', filename],
['--column-range', '-1', '-1', filename],
]:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + options,
stderr=PIPE)
Expand Down