Skip to content

Commit acc14f8

Browse files
committed
[IMP] Makefile: make review optional early break
1 parent 8c1881e commit acc14f8

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ static: $(HTML_BUILD_DIR)/_static/style.css
8686

8787
test:
8888
@python tests/main.py $(SOURCE_DIR)/administration $(SOURCE_DIR)/applications $(SOURCE_DIR)/contributing $(SOURCE_DIR)/developer $(SOURCE_DIR)/services redirects
89+
90+
review:
91+
@read -p "Enter file or folder path: " path; read -p "Enter max line length: " line_length; \
92+
if [ -z "$$path" ]; then echo "Error: Path cannot be empty"; exit 1; fi; \
93+
python tests/main.py -e line-too-long -e early-line-breaks --max-line-length=$$line_length $$path

tests/checkers/rst_style.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
'^(' + '|'.join(rf'\{char}+' for char in FORBIDDEN_HEADING_CHARS) + ')\n$'
1717
)
1818
GIT_CONFLICT_MARKERS = ['<' * 7, '>' * 7]
19-
19+
ALLOWED_EARLY_BREAK_CHARS_RE = re.compile(
20+
r'^\s*(\.\. |:\S+:\s+)', re.IGNORECASE
21+
) # Matches lines containing directives ('.. directive::' or ':directive:'), allowed to break early.
2022

2123
@sphinxlint.checker('.rst')
2224
def check_heading_delimiters_characters(file, lines, options=None):
@@ -109,3 +111,45 @@ def check_git_conflict_markers(file, lines, options=None):
109111
for lno, line in enumerate(lines):
110112
if any(marker in line for marker in GIT_CONFLICT_MARKERS):
111113
yield lno + 1, "the git conflict should be resolved"
114+
115+
116+
@sphinxlint.checker('.rst', enabled=False, rst_only=True)
117+
def check_early_line_breaks(file, lines, options=None):
118+
""" Optional - Checks that all lines dont break early per options.max_line_length param """
119+
def is_valid_line(line, type):
120+
""" Allowed to break early - handle tables and bullets """
121+
if type == 1:
122+
return not ALLOWED_EARLY_BREAK_CHARS_RE.search(line) \
123+
and not HEADING_DELIMITER_RE.search(line) \
124+
and not line.startswith("\n") \
125+
and not line.lstrip().startswith(("+", "|", "- ", "* ", "#. ")) \
126+
and len(line) <= options.max_line_length
127+
if type == 0:
128+
return not ALLOWED_EARLY_BREAK_CHARS_RE.search(line) \
129+
and not HEADING_DELIMITER_RE.search(line) \
130+
and not line.startswith("\n") \
131+
and not line.lstrip().startswith(("+", "|")) \
132+
and len(line) <= options.max_line_length
133+
def get_next_line_first_word(next_line):
134+
""" Return the first word of the next line """
135+
if next_line.startswith(" "):
136+
next_line_dict = {
137+
"*": lambda x: x.split("* ", 1)[0],
138+
"-": lambda x: x.split("- ", 1)[0],
139+
"#.": lambda x: x.split("#. ", 1)[0],
140+
"default": lambda x: x.split(" ", 1)[0]
141+
}
142+
return next_line_dict.get(
143+
next_line.lstrip()[:2], next_line_dict["default"]
144+
)(next_line.lstrip())
145+
else:
146+
return next_line.split(" ", 1)[0]
147+
148+
for lno, current_line in enumerate(lines):
149+
if lno + 1 < len(lines):
150+
next_line = lines[lno + 1]
151+
if is_valid_line(current_line, 0) and is_valid_line(next_line, 1):
152+
current_line_remaining_space = options.max_line_length - len(current_line)
153+
next_line_first_word = get_next_line_first_word(next_line)
154+
if current_line_remaining_space > len(next_line_first_word):
155+
yield lno + 1, f"Consider moving \"{next_line_first_word}\" to line {lno + 1}."

tests/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def run_additional_checks(argv=None):
5252
5353
Optional checkers:
5454
- line-too-long: Check for line length; this checker is not run by default.
55+
- early-line-break: Check for early line breaks; this checker is not run by default.
5556
5657
Custom checkers:
5758
- all the checkers defined in checkers/* files

0 commit comments

Comments
 (0)