Skip to content

Commit 4ea65f6

Browse files
committed
fix: ai
1 parent 5da1e6c commit 4ea65f6

2 files changed

Lines changed: 53 additions & 36 deletions

File tree

snakefmt/blocken.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def __init__(self, name, tokens: Iterator[TokenInfo]):
5858
self.name = name
5959
self._live_tokens = tokens
6060
self._buffered_tokens: list[TokenInfo] = list()
61-
self.tokens = tokens
6261
self.lines = 0
6362
self.rulecount = 0
6463
self._overwrite_cmd: Optional[str] = None
@@ -94,10 +93,11 @@ def next_component(self):
9493

9594
def next_block(self):
9695
"""Returns a entire block, just consume until the end of the block.
97-
Donot care if there are nested blocks inside or snakemake keywords inside.
96+
Do not care if there are nested blocks inside or snakemake keywords inside.
9897
99-
it could be INDEDT -> [any content] -> DEDENT, or [any content] -> DEDENT
98+
it could be INDENT -> [any content] -> DEDENT, or [any content] -> DEDENT
10099
"""
100+
101101
line = self.next_new_line()
102102
if line.end.type == tokenize.ENDMARKER:
103103
self.denext(*reversed(list(line.iter)))
@@ -230,7 +230,7 @@ def is_keyword_line(self):
230230
and self.body[1].string == "="
231231
):
232232
return True
233-
if self.body[0].type == "**":
233+
if self.body[0].string == "**":
234234
return True
235235
return False
236236

@@ -326,7 +326,7 @@ def tokens2linestrs(tokens: Iterator[TokenInfo]):
326326

327327
class FormatState(NamedTuple):
328328
fmt_on: bool = True
329-
sort_direcives: bool | None = None
329+
sort_directives: bool | None = None
330330
skip_next: bool = False # one-time directive for the next snakemake block
331331

332332
@property
@@ -349,12 +349,11 @@ def update(self, comment: str):
349349
350350
If found `# fmt: on` and no `# fmt: off` before:
351351
if `fmt: off[sort]` is False:
352-
sort_direcives == True -> enabled
353-
sort_direcives == False -> disabled in this indent before
354-
sort_direcives == None -> haven't enabled originally
352+
sort_directives == True -> enabled
353+
sort_directives == False -> disabled in this indent before
354+
sort_directives == None -> haven't enabled originally
355355
turn it on
356356
"""
357-
match = _FMT_DIRECTIVE_RE.match(comment)
358357
if match := _FMT_DIRECTIVE_RE.match(comment):
359358
directive, options = match.groups()
360359
# Parse options: "sort,next" -> ["sort", "next"] -> "sort"
@@ -364,14 +363,14 @@ def update(self, comment: str):
364363
return self._replace(fmt_on=True)
365364
elif directive == "on":
366365
if option == "sort":
367-
return self._replace(sort_direcives=True)
368-
if self.sort_direcives is False:
366+
return self._replace(sort_directives=True)
367+
if self.sort_directives is False:
369368
# re-enable sorting if it was disabled by `# fmt: off[sort]` before,
370369
# but should effect if no `# fmt: off[sort]` in this indent before.
371-
return self._replace(sort_direcives=True)
370+
return self._replace(sort_directives=True)
372371
elif directive == "off":
373372
if option == "sort":
374-
return self._replace(sort_direcives=False)
373+
return self._replace(sort_directives=False)
375374
if option == "next":
376375
return self._replace(skip_next=True)
377376
return self._replace(fmt_on=False)
@@ -388,8 +387,8 @@ def found_skip(comment: str):
388387
return "# fmt: skip" in comment
389388

390389
def reset_sort(self):
391-
if self.sort_direcives is False:
392-
return self._replace(sort_direcives=None)
390+
if self.sort_directives is False:
391+
return self._replace(sort_directives=None)
393392
return self
394393

395394

@@ -1083,8 +1082,9 @@ def try_combine_format(
10831082
Search reversly, so it only give one of the possible results.
10841083
10851084
Since the non-comma param is the mistake of the user,
1086-
please do not blame if the olgorithm is slow :)
1085+
please do not blame if the algorithm is slow :)
10871086
"""
1087+
10881088
if len(arg_lines) <= 1:
10891089
return [arg_lines]
10901090
mode = mode or Mode()
@@ -1540,7 +1540,7 @@ def format_body(self, mode, state, post_colon):
15401540
directive = ""
15411541
for line in noncoding: # here noncoding is already formated
15421542
linelstrip = line.lstrip()
1543-
last_sort_off = state.sort_direcives
1543+
last_sort_off = state.sort_directives
15441544
if linelstrip:
15451545
# only non-empty lines are formattable
15461546
if state.found_skip(linelstrip):
@@ -1560,14 +1560,14 @@ def format_body(self, mode, state, post_colon):
15601560
block.indent_str, self.deindent_level + 1, [line]
15611561
)
15621562
)
1563-
elif not state.sort_direcives:
1563+
elif not state.sort_directives:
15641564
if directives:
15651565
formatted.extend(self.sort_directives(directives))
15661566
if directive:
15671567
formatted.append(directive)
15681568
directive = ""
15691569
elif not last_sort_off:
1570-
# state.sort_direcives switched on, this comment is
1570+
# state.sort_directives switched on, this comment is
15711571
# actually `# fmt: on[sort]` directive,
15721572
# so split from next directive
15731573
formatted.append(directive)
@@ -1587,7 +1587,7 @@ def format_body(self, mode, state, post_colon):
15871587
)
15881588
else:
15891589
directive += block.formatted(mode, state)
1590-
if state.sort_direcives:
1590+
if state.sort_directives:
15911591
directives[block.keyword] = directive
15921592
else:
15931593
assert not directives, "Already flushed once fmt: off[sort]"
@@ -1797,9 +1797,9 @@ class GlobalBlock(Block):
17971797
so tail_noncoding always updated to the last body_block
17981798
"""
17991799

1800-
__slots__ = ("mode", "sort_direcives")
1800+
__slots__ = ("mode", "sort_directives")
18011801
mode: Mode
1802-
sort_direcives: bool
1802+
sort_directives: bool
18031803

18041804
subautomata = {**python_subautomata, **global_snakemake_subautomata}
18051805

@@ -1817,8 +1817,8 @@ def get_formatted(
18171817
if mode is None:
18181818
raise ValueError("Mode should be provided for formatting")
18191819
if sort_directives is None:
1820-
sort_directives = getattr(self, "sort_direcives", None)
1821-
state = FormatState(sort_direcives=sort_directives or None)
1820+
sort_directives = getattr(self, "sort_directives", None)
1821+
state = FormatState(sort_directives=sort_directives or None)
18221822
# if set to None, it will not be enabled by `# fmt: on`
18231823
python_codes: list[str] = []
18241824
snakemake_codes: list[tuple[str, str]] = []
@@ -1830,19 +1830,19 @@ def get_formatted(
18301830
snakemake_codes.append((segment, indent_proxy))
18311831
else:
18321832
last_str += segment
1833-
place_hode_str = "o" * 50
1833+
placeholder = "o" * 50
18341834
raw_str = "".join(python_codes)
1835-
while place_hode_str in raw_str:
1836-
place_hode_str *= 2
1835+
while placeholder in raw_str:
1836+
placeholder *= 2
18371837
raw_str = "#\n"
18381838
for python_code, (snakemake_code, indent) in zip(python_codes, snakemake_codes):
18391839
if snakemake_code.count("\n") == 1: # must at the end of line
1840-
place_hode = f"{indent}def l{place_hode_str}1ng(): ...\n"
1840+
snakemake_proxy = f"{indent}def l{placeholder}1ng(): ...\n"
18411841
else:
1842-
place_hode = f"{indent}def l{place_hode_str}ng():\n{indent} return\n"
1843-
raw_str += python_code + place_hode
1842+
snakemake_proxy = f"{indent}def l{placeholder}ng():\n{indent} return\n"
1843+
raw_str += python_code + snakemake_proxy
18441844
raw_str += last_str
1845-
formatted, *formatted_split = format_black(raw_str, mode).split(place_hode_str)
1845+
formatted, *formatted_split = format_black(raw_str, mode).split(placeholder)
18461846
final_str = formatted
18471847
for formatted, (snakemake_code, _) in zip(formatted_split, snakemake_codes):
18481848
final_str = final_str.rsplit("\n", 1)[0] + "\n" + snakemake_code
@@ -1856,13 +1856,13 @@ def compilation(self):
18561856
raise NotImplementedError
18571857

18581858

1859-
def parse(input: str | Callable[[], str], name: str = "<string>"):
1860-
if isinstance(input, str):
1859+
def parse(source: str | Callable[[], str], name: str = "<string>"):
1860+
if isinstance(source, str):
18611861
tokens = tokenize.generate_tokens(
1862-
iter(input.splitlines(keepends=True)).__next__
1862+
iter(source.splitlines(keepends=True)).__next__
18631863
)
18641864
else:
1865-
tokens = tokenize.generate_tokens(input)
1865+
tokens = tokenize.generate_tokens(source)
18661866
return GlobalBlock(0, TokenIterator(name, tokens), [])
18671867

18681868

@@ -1878,5 +1878,5 @@ def setup_formatter(
18781878
mode.line_length = line_length
18791879

18801880
formatter.mode = mode
1881-
formatter.sort_direcives = sort_params
1881+
formatter.sort_directives = sort_params
18821882
return formatter

tests/test_formatter.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,6 +2462,23 @@ def test_fmt_off_sort_dedent(self):
24622462
"\n\n" + formatted2
24632463
)
24642464
assert setup_formatter(code, sort_params=True).get_formatted() == expected
2465+
code = (
2466+
"# fmt: off[sort]\n"
2467+
"if 1:\n"
2468+
" # fmt: on[sort]\n"
2469+
+ "".join(" " + i for i in code1.splitlines(keepends=True)).rstrip()
2470+
+ "\n"
2471+
+ code2.rstrip()
2472+
)
2473+
expected = (
2474+
"# fmt: off[sort]\n"
2475+
"if 1:\n"
2476+
f"{TAB}# fmt: on[sort]\n"
2477+
+ "".join(TAB + i for i in formatted0.splitlines(keepends=True)).rstrip()
2478+
+ "\n"
2479+
"\n\n" + formatted2
2480+
)
2481+
assert setup_formatter(code, sort_params=True).get_formatted() == expected
24652482

24662483
def test_fmt_off_sort_on_noeffect(self):
24672484
code1, formatted1 = TestSortFormatting.sorting_comprehensive

0 commit comments

Comments
 (0)