diff --git a/snakefmt/parser/parser.py b/snakefmt/parser/parser.py index e4d7060..d9c0d45 100644 --- a/snakefmt/parser/parser.py +++ b/snakefmt/parser/parser.py @@ -1,4 +1,3 @@ -import sys import tokenize from abc import ABC, abstractmethod from typing import NamedTuple, Optional @@ -11,6 +10,7 @@ Vocabulary, add_token_space, is_newline, + re_add_curly_bracket_if_needed, ) from snakefmt.types import TAB, Token, TokenIterator, col_nb @@ -325,12 +325,4 @@ def get_next_queriable(self, snakefile: TokenIterator) -> Status: if not pythonable and token.type != tokenize.COMMENT: pythonable = True buffer += token.string - if ( - token is not None - and sys.version_info >= (3, 12) - and token.type == tokenize.FSTRING_MIDDLE - ): - if token.string.endswith("}"): - buffer += "}" - elif token.string.endswith("{"): - buffer += "{" + buffer += re_add_curly_bracket_if_needed(token) diff --git a/snakefmt/parser/syntax.py b/snakefmt/parser/syntax.py index 352c27c..c5e9bd7 100644 --- a/snakefmt/parser/syntax.py +++ b/snakefmt/parser/syntax.py @@ -44,6 +44,20 @@ spacing_triggers[tokenize.OP].add(tokenize.FSTRING_START) +def re_add_curly_bracket_if_needed(token: Token) -> str: + result = "" + if ( + token is not None + and sys.version_info >= (3, 12) + and token.type == tokenize.FSTRING_MIDDLE + ): + if token.string.endswith("}"): + result = "}" + elif token.string.endswith("{"): + result = "{" + return result + + def operator_skip_spacing(prev_token: Token, token: Token) -> bool: if prev_token.type != tokenize.OP and token.type != tokenize.OP: return False @@ -333,15 +347,7 @@ def parse_params(self, snakefile: TokenIterator): prev_token = None while True: cur_param = self.process_token(cur_param, prev_token) - if ( - self.token is not None - and sys.version_info >= (3, 12) - and self.token.type == tokenize.FSTRING_MIDDLE - ): - if self.token.string.endswith("}"): - cur_param.value += "}" - elif self.token.string.endswith("{"): - cur_param.value += "{" + cur_param.value += re_add_curly_bracket_if_needed(self.token) try: prev_token = self.token self.token = next(snakefile)