Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors because of mix indentation #13274

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix errors because of mix indentation
I am hitting errors because of mixed indentation. This change always used indent_char as space and if tab, assume it to be equivalent to 4 space chars.
  • Loading branch information
345ishaan authored May 5, 2024
commit 6fc56d3379761683f125607ef0188b6a3306cb2d
Original file line number Diff line number Diff line change
Expand Up @@ -652,25 +652,28 @@ def _parse_nodes(
def _get_indentation(text: str) -> Tuple[str, int, int]:
indent_char = None
minimum_chain = None
first_indent_char = None

# Check that text is at least 1 line long
text_split = text.splitlines()
if len(text_split) == 0:
raise ValueError("Text should be at least one line long.")

for line in text_split:
for line_id, line in enumerate(text_split):
stripped_line = line.lstrip()

if stripped_line:
# Get whether it's tabs or spaces
spaces_count = line.count(" ", 0, len(line) - len(stripped_line))
tabs_count = line.count("\t", 0, len(line) - len(stripped_line))

if not indent_char:
if spaces_count:
indent_char = " "
if tabs_count:
indent_char = "\t"
if spaces_count:
indent_char = " "
if tabs_count:
indent_char = "\t"

if line_id == 0:
first_indent_char = indent_char


# Detect mixed indentation.
if spaces_count > 0 and tabs_count > 0:
Expand All @@ -685,6 +688,7 @@ def _get_indentation(text: str) -> Tuple[str, int, int]:
char_count = line.count(
indent_char, 0, len(line) - len(stripped_line)
)
char_count = char_count * 4 if indent_char == "\t" else char_count
if minimum_chain is not None:
if char_count > 0:
minimum_chain = min(char_count, minimum_chain)
Expand All @@ -693,23 +697,22 @@ def _get_indentation(text: str) -> Tuple[str, int, int]:
minimum_chain = char_count

# Handle edge case
if indent_char is None:
indent_char = " "
indent_char = " "
if minimum_chain is None:
minimum_chain = 4

# Get the first indent count
first_line = text_split[0]
first_indent_count = 0
for char in first_line:
if char == indent_char:
first_indent_count += 1
if char == first_indent_char:
first_indent_count += 1 if first_indent_char == " " else 4
else:
break

# Return the default indent level if only one indentation level was found.
return indent_char, minimum_chain, first_indent_count // minimum_chain

@staticmethod
def _get_comment_text(node: TextNode) -> str:
"""Gets just the natural language text for a skeletonize comment."""
Expand Down
Loading