Skip to content

Commit a1ba877

Browse files
committed
Added feedback
1 parent 5f29c7c commit a1ba877

File tree

3 files changed

+10
-35
lines changed

3 files changed

+10
-35
lines changed

src/black/ranges.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def sanitized_lines(
5858
if not src_contents:
5959
return []
6060
good_lines = []
61-
src_line_count = len(src_contents.splitlines())
61+
src_line_count = src_contents.count("\n")
62+
if not src_contents.endswith("\n"):
63+
src_line_count += 1
6264
for start, end in lines:
6365
if start > src_line_count:
6466
continue

tests/data/cases/line_ranges_outside_source.py

-34
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,3 @@ def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parame
55
def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
66
def foo3(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
77
def foo4(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
8-
9-
# Adding some unformated code covering a wide range of syntaxes.
10-
11-
if True:
12-
# Incorrectly indented prefix comments.
13-
pass
14-
15-
import typing
16-
from typing import (
17-
Any ,
18-
)
19-
class MyClass( object): # Trailing comment with extra leading space.
20-
#NOTE: The following indentation is incorrect:
21-
@decor( 1 * 3 )
22-
def my_func( arg):
23-
pass
24-
25-
try: # Trailing comment with extra leading space.
26-
for i in range(10): # Trailing comment with extra leading space.
27-
while condition:
28-
if something:
29-
then_something( )
30-
elif something_else:
31-
then_something_else( )
32-
except ValueError as e:
33-
unformatted( )
34-
finally:
35-
unformatted( )
36-
37-
async def test_async_unformatted( ): # Trailing comment with extra leading space.
38-
async for i in some_iter( unformatted ): # Trailing comment with extra leading space.
39-
await asyncio.sleep( 1 )
40-
async with some_context( unformatted ):
41-
print( "unformatted" )

tests/test_ranges.py

+7
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,10 @@ def test_sanitize(
240240
4. pass
241241
"""
242242
assert sanitized == sanitized_lines(lines, source)
243+
244+
source_no_trailing_nl = """\
245+
1. import re
246+
2. def func(arg1,
247+
3. arg2, arg3):
248+
4. pass"""
249+
assert sanitized == sanitized_lines(lines, source_no_trailing_nl)

0 commit comments

Comments
 (0)