Skip to content

Commit d61d4a2

Browse files
authored
Merge pull request #421 from iconmaster5326/master
trailing-whitespace: add option for custom chars to strip
2 parents 0f7b5e0 + 0114962 commit d61d4a2

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Add this to your `.pre-commit-config.yaml`
111111
use `args: [--markdown-linebreak-ext=md]` (or other extensions used
112112
by your markdownfiles). If for some reason you want to treat all files
113113
as markdown, use `--markdown-linebreak-ext=*`.
114+
- By default, this hook trims all whitespace from the ends of lines.
115+
To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`.
114116

115117
### Deprecated / replaced hooks
116118

pre_commit_hooks/trailing_whitespace_fixer.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from typing import Sequence
88

99

10-
def _fix_file(filename, is_markdown): # type: (str, bool) -> bool
10+
def _fix_file(filename, is_markdown, chars):
11+
# type: (str, bool, Optional[bytes]) -> bool
1112
with open(filename, mode='rb') as file_processed:
1213
lines = file_processed.readlines()
13-
newlines = [_process_line(line, is_markdown) for line in lines]
14+
newlines = [_process_line(line, is_markdown, chars) for line in lines]
1415
if newlines != lines:
1516
with open(filename, mode='wb') as file_processed:
1617
for line in newlines:
@@ -20,17 +21,20 @@ def _fix_file(filename, is_markdown): # type: (str, bool) -> bool
2021
return False
2122

2223

23-
def _process_line(line, is_markdown): # type: (bytes, bool) -> bytes
24+
def _process_line(line, is_markdown, chars):
25+
# type: (bytes, bool, Optional[bytes]) -> bytes
2426
if line[-2:] == b'\r\n':
2527
eol = b'\r\n'
28+
line = line[:-2]
2629
elif line[-1:] == b'\n':
2730
eol = b'\n'
31+
line = line[:-1]
2832
else:
2933
eol = b''
3034
# preserve trailing two-space for non-blank lines in markdown files
31-
if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol):
32-
return line.rstrip() + b' ' + eol
33-
return line.rstrip() + eol
35+
if is_markdown and (not line.isspace()) and line.endswith(b' '):
36+
return line[:-2].rstrip(chars) + b' ' + eol
37+
return line.rstrip(chars) + eol
3438

3539

3640
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
@@ -50,6 +54,13 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
5054
'default: %(default)s'
5155
),
5256
)
57+
parser.add_argument(
58+
'--chars',
59+
help=(
60+
'The set of characters to strip from the end of lines. '
61+
'Defaults to all whitespace characters.'
62+
),
63+
)
5364
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
5465
args = parser.parse_args(argv)
5566

@@ -73,12 +84,12 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
7384
" (probably filename; use '--markdown-linebreak-ext=EXT')"
7485
.format(ext),
7586
)
76-
87+
chars = None if args.chars is None else args.chars.encode('utf-8')
7788
return_code = 0
7889
for filename in args.filenames:
7990
_, extension = os.path.splitext(filename.lower())
8091
md = all_markdown or extension in md_exts
81-
if _fix_file(filename, md):
92+
if _fix_file(filename, md, chars):
8293
print('Fixing {}'.format(filename))
8394
return_code = 1
8495
return return_code

tests/trailing_whitespace_fixer_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,27 @@ def test_preserve_non_utf8_file(tmpdir):
7878
ret = main([path.strpath])
7979
assert ret == 1
8080
assert path.size() == (len(non_utf8_bytes_content) - 1)
81+
82+
83+
def test_custom_charset_change(tmpdir):
84+
# strip spaces only, no tabs
85+
path = tmpdir.join('file.txt')
86+
path.write('\ta \t \n')
87+
ret = main([path.strpath, '--chars', ' '])
88+
assert ret == 1
89+
assert path.read() == '\ta \t\n'
90+
91+
92+
def test_custom_charset_no_change(tmpdir):
93+
path = tmpdir.join('file.txt')
94+
path.write('\ta \t\n')
95+
ret = main([path.strpath, '--chars', ' '])
96+
assert ret == 0
97+
98+
99+
def test_markdown_with_custom_charset(tmpdir):
100+
path = tmpdir.join('file.md')
101+
path.write('\ta \t \n')
102+
ret = main([path.strpath, '--chars', ' ', '--markdown-linebreak-ext', '*'])
103+
assert ret == 1
104+
assert path.read() == '\ta \t \n'

0 commit comments

Comments
 (0)