diff --git a/autopep8.py b/autopep8.py index e30b4eb3..f2d4c4f7 100755 --- a/autopep8.py +++ b/autopep8.py @@ -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)) @@ -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', @@ -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 diff --git a/test/test_autopep8.py b/test/test_autopep8.py index cfb80d48..11cbe1b4 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -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): @@ -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)