Skip to content

Commit

Permalink
Fix: Tabs are not counted as 4 spaces (psf#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienCochuyt committed Nov 12, 2019
1 parent 9b821df commit dd8820e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions black.py
Expand Up @@ -1570,12 +1570,12 @@ def is_complex_subscript(self, leaf: Leaf) -> bool:
n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
)

def render(self, force_spaces: bool = False) -> str:
def __str__(self) -> str:
"""Render the line."""
if not self:
return "\n"

indent_style = " " if force_spaces or not self.use_tabs else "\t"
indent_style = " " if not self.use_tabs else "\t"
indent = indent_style * self.depth
leaves = iter(self.leaves)
first = next(leaves)
Expand All @@ -1586,9 +1586,6 @@ def render(self, force_spaces: bool = False) -> str:
res += str(comment)
return res + "\n"

def __str__(self) -> str:
return self.render()

def __bool__(self) -> bool:
"""Return True if the line has leaves or comments."""
return bool(self.leaves or self.comments)
Expand Down Expand Up @@ -3971,10 +3968,15 @@ def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") ->
Uses the provided `line_str` rendering, if any, otherwise computes a new one.
"""
if not line_str:
# Force spaces to ensure len(line) is correct
line_str = line.render(force_spaces=True).strip("\n")
line_str = str(line).strip("\n")
candidate_length = len(line_str)
for c in line_str:
if c == "\t":
candidate_length += 3 # 1 tab = 4 spaces
else:
break
return (
len(line_str) <= line_length
candidate_length <= line_length
and "\n" not in line_str # multiline strings
and not line.contains_standalone_comments()
)
Expand Down

0 comments on commit dd8820e

Please sign in to comment.