Skip to content

Commit

Permalink
Add a --use-tabs argument to use tabs instead of spaces
Browse files Browse the repository at this point in the history
Closes #47
  • Loading branch information
jleclanche committed Jan 19, 2022
1 parent 8305f10 commit e23c038
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ def validate_regex(
" Currently disabled because it leads to some crashes."
),
)
@click.option(
"--use-tabs",
is_flag=True,
help=(
"Use tabs instead of spaces for indentation. "
"Tabs are always equal to 4 spaces."
),
)
@click.option(
"--check",
is_flag=True,
Expand Down Expand Up @@ -399,6 +407,7 @@ def main(
skip_string_normalization: bool,
skip_magic_trailing_comma: bool,
experimental_string_processing: bool,
use_tabs: bool,
quiet: bool,
verbose: bool,
required_version: Optional[str],
Expand Down Expand Up @@ -469,6 +478,7 @@ def main(
string_normalization=not skip_string_normalization,
magic_trailing_comma=not skip_magic_trailing_comma,
experimental_string_processing=experimental_string_processing,
use_tabs=use_tabs,
)

if code is not None:
Expand Down
3 changes: 2 additions & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def transform_line(
yield line
return

line_str = line_to_string(line)
# Force spaces to ensure len(line) is correct
line_str = line.render(force_spaces=True).strip("\n")

ll = mode.line_length
sn = mode.string_normalization
Expand Down
6 changes: 5 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,15 @@ def clone(self) -> "Line":
)

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

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

indent = " " * self.depth
indent_style = " " if force_spaces or not self.use_tabs else "\t"
indent = indent_style * self.depth
leaves = iter(self.leaves)
first = next(leaves)
res = f"{first.prefix}{indent}{first.value}"
Expand Down
2 changes: 2 additions & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class Mode:
is_ipynb: bool = False
magic_trailing_comma: bool = True
experimental_string_processing: bool = False
use_tabs: bool = False

def get_cache_key(self) -> str:
if self.target_versions:
Expand All @@ -147,5 +148,6 @@ def get_cache_key(self) -> str:
str(int(self.is_ipynb)),
str(int(self.magic_trailing_comma)),
str(int(self.experimental_string_processing)),
str(int(self.use_tabs)),
]
return ".".join(parts)

0 comments on commit e23c038

Please sign in to comment.