]>
git.madduck.net Git - etc/vim.git/blobdiff - src/black/trans.py
madduck's git repository
Every one of the projects in this repository is available at the canonical
URL git://git.madduck.net/madduck/pub/<projectpath> — see
each project's metadata for the exact URL.
All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@ git. madduck. net .
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
SSH access, as well as push access can be individually
arranged .
If you use my repositories frequently, consider adding the following
snippet to ~/.gitconfig and using the third clone URL listed for each
project:
[url "git://git.madduck.net/madduck/"]
insteadOf = madduck:
from typing import (
Any,
Callable,
from typing import (
Any,
Callable,
Collection,
Dict,
Iterable,
Collection,
Dict,
Iterable,
+import sys
+
+if sys.version_info < (3, 8):
+ from typing_extensions import Final
+else:
+ from typing import Final
+
+from mypy_extensions import trait
from black.rusty import Result, Ok, Err
from black.rusty import Result, Ok, Err
return Err(cant_transform)
return Err(cant_transform)
-@dataclass # type: ignore
class StringTransformer(ABC):
"""
An implementation of the Transformer protocol that relies on its
class StringTransformer(ABC):
"""
An implementation of the Transformer protocol that relies on its
- line_length: int
- normalize_strings: bool
- __name__ = "StringTransformer"
+ __name__: Final = "StringTransformer"
+
+ # Ideally this would be a dataclass, but unfortunately mypyc breaks when used with
+ # `abc.ABC`.
+ def __init__(self, line_length: int, normalize_strings: bool) -> None:
+ self.line_length = line_length
+ self.normalize_strings = normalize_strings
@abstractmethod
def do_match(self, line: Line) -> TMatchResult:
@abstractmethod
def do_match(self, line: Line) -> TMatchResult:
class CustomSplitMapMixin:
"""
This mixin class is used to map merged strings to a sequence of
class CustomSplitMapMixin:
"""
This mixin class is used to map merged strings to a sequence of
the resultant substrings go over the configured max line length.
"""
the resultant substrings go over the configured max line length.
"""
- _Key = Tuple[StringID, str]
- _CUSTOM_SPLIT_MAP: Dict[_Key, Tuple[CustomSplit, ...]] = defaultdict(tuple)
+ _Key: ClassVar = Tuple[StringID, str]
+ _CUSTOM_SPLIT_MAP: ClassVar[Dict[_Key, Tuple[CustomSplit, ...]]] = defaultdict(
+ tuple
+ )
@staticmethod
def _get_key(string: str) -> "CustomSplitMapMixin._Key":
@staticmethod
def _get_key(string: str) -> "CustomSplitMapMixin._Key":
return key in self._CUSTOM_SPLIT_MAP
return key in self._CUSTOM_SPLIT_MAP
-class StringMerger(CustomSplitMapMixin, StringTransformer ):
+class StringMerger(StringTransformer, CustomSplitMapMixin ):
"""StringTransformer that merges strings together.
Requirements:
"""StringTransformer that merges strings together.
Requirements:
* The target string is not a multiline (i.e. triple-quote) string.
"""
* The target string is not a multiline (i.e. triple-quote) string.
"""
+ STRING_OPERATORS: Final = [
token.EQEQUAL,
token.GREATER,
token.GREATEREQUAL,
token.EQEQUAL,
token.GREATER,
token.GREATEREQUAL,
-class StringSplitter(CustomSplitMapMixin, BaseStringSplitter ):
+class StringSplitter(BaseStringSplitter, CustomSplitMapMixin ):
"""
StringTransformer that splits "atom" strings (i.e. strings which exist on
lines by themselves).
"""
StringTransformer that splits "atom" strings (i.e. strings which exist on
lines by themselves).
CustomSplit objects and add them to the custom split map.
"""
CustomSplit objects and add them to the custom split map.
"""
+ MIN_SUBSTR_SIZE: Final = 6
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
(?<!\{) (?:\{\{)* \{ (?!\{)
(?:
[^\{\}]
(?<!\{) (?:\{\{)* \{ (?!\{)
(?:
[^\{\}]
-class StringParenWrapper(CustomSplitMapMixin, BaseStringSplitter ):
+class StringParenWrapper(BaseStringSplitter, CustomSplitMapMixin ):
"""
StringTransformer that splits non-"atom" strings (i.e. strings that do not
exist on lines by themselves).
"""
StringTransformer that splits non-"atom" strings (i.e. strings that do not
exist on lines by themselves).
+ DEFAULT_TOKEN: Final = 20210605
- START = 1
- DOT = 2
- NAME = 3
- PERCENT = 4
- SINGLE_FMT_ARG = 5
- LPAR = 6
- RPAR = 7
- DONE = 8
+ START: Final = 1
+ DOT: Final = 2
+ NAME: Final = 3
+ PERCENT: Final = 4
+ SINGLE_FMT_ARG: Final = 5
+ LPAR: Final = 6
+ RPAR: Final = 7
+ DONE: Final = 8
# Lookup Table for Next State
# Lookup Table for Next State
- _goto: Dict[Tuple[ParserState, NodeType], ParserState ] = {
+ _goto: Final[Dict[Tuple[ParserState, NodeType], ParserState] ] = {
# A string trailer may start with '.' OR '%'.
(START, token.DOT): DOT,
(START, token.PERCENT): PERCENT,
# A string trailer may start with '.' OR '%'.
(START, token.DOT): DOT,
(START, token.PERCENT): PERCENT,