Skip to content

Commit

Permalink
autopep8.py: Add ability to specify a column range
Browse files Browse the repository at this point in the history
This adds the ability to specify a column range
on which to perform fixes. Although multiline
column range fixing is supported, it is not
recommended, since changing columns may result in
differing numbers of columns among lines - which
would make it impossible to automatically adjust
the column range after performing changes.

Closes hhatto#227
  • Loading branch information
CLiu13 committed Nov 4, 2018
1 parent 65359bc commit 003f3e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
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
24 changes: 24 additions & 0 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -4821,6 +4821,30 @@ 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_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

0 comments on commit 003f3e1

Please sign in to comment.