Skip to content

Commit

Permalink
feat: check line is commented or not to determine if enforcement woul…
Browse files Browse the repository at this point in the history
…d toggle line or keep it as-is
  • Loading branch information
acrois committed Mar 31, 2024
1 parent 095def4 commit 84c4467
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions shell/toggle_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ def is_line_commented(line, n):

def toggle_comment_on_line(line, n, is_commented, enforce_state=None):
"""Toggles comment status of a line, with an option to enforce a specific state."""
# Determine whether to add or remove comments based on the enforce_state and current state
if enforce_state is not None:
should_comment = enforce_state
should_comment = enforce_state and not is_commented
should_uncomment = not enforce_state and is_commented
else:
# Toggle the comment if no enforce_state is provided
should_comment = not is_commented
should_uncomment = is_commented

if should_comment:
# Add n-1 leading comment characters if the line should be commented
indentation = re.match(r"^(\s*)", line).group(1)
new_line = f"{indentation}{'#' * (n-1)} {line.lstrip()}"
else:
elif should_uncomment:
# Remove leading comment characters if the line should be uncommented
new_line = re.sub(r"^[^\s\w]{" + str(n-1) + r"}\s?", "", line, 1)
else:
# Leave the line unchanged if it already meets the desired state
new_line = line

return new_line

Expand Down

0 comments on commit 84c4467

Please sign in to comment.