MCPcopy
hub / github.com/psf/black / left_hand_split

Function left_hand_split

src/black/linegen.py:875–938  ·  view source on GitHub ↗

Split line into many lines, starting with the first matching bracket pair. Note: this usually looks weird, only use this for function definitions. Prefer RHS otherwise. This is why this function is not symmetrical with :func:`right_hand_split` which also handles optional parentheses.

(
    line: Line, _features: Collection[Feature], mode: Mode
)

Source from the content-addressed store, hash-verified

873
874
875def left_hand_split(
876 line: Line, _features: Collection[Feature], mode: Mode
877) -> Iterator[Line]:
878 """Split line into many lines, starting with the first matching bracket pair.
879
880 Note: this usually looks weird, only use this for function definitions.
881 Prefer RHS otherwise. This is why this function is not symmetrical with
882 :func:`right_hand_split` which also handles optional parentheses.
883 """
884 for leaf_type in [token.LPAR, token.LSQB]:
885 tail_leaves: list[Leaf] = []
886 body_leaves: list[Leaf] = []
887 head_leaves: list[Leaf] = []
888 current_leaves = head_leaves
889 matching_bracket: Leaf | None = None
890 depth = 0
891 for index, leaf in enumerate(line.leaves):
892 if index == 2 and leaf.type == token.LSQB:
893 # A [ at index 2 means this is a type param, so start
894 # tracking the depth
895 depth += 1
896 elif depth > 0:
897 if leaf.type == token.LSQB:
898 depth += 1
899 elif leaf.type == token.RSQB:
900 depth -= 1
901 if (
902 current_leaves is body_leaves
903 and leaf.type in CLOSING_BRACKETS
904 and leaf.opening_bracket is matching_bracket
905 and isinstance(matching_bracket, Leaf)
906 # If the code is still on LPAR and we are inside a type
907 # param, ignore the match since this is searching
908 # for the function arguments
909 and not (leaf_type == token.LPAR and depth > 0)
910 ):
911 ensure_visible(leaf)
912 ensure_visible(matching_bracket)
913 current_leaves = tail_leaves if body_leaves else head_leaves
914 current_leaves.append(leaf)
915 if current_leaves is head_leaves:
916 if leaf.type == leaf_type and (
917 not (leaf_type == token.LPAR and depth > 0)
918 ):
919 matching_bracket = leaf
920 current_leaves = body_leaves
921 if matching_bracket and tail_leaves:
922 break
923 if not matching_bracket or not tail_leaves:
924 raise CannotSplit("No brackets found")
925
926 head = bracket_split_build_line(
927 head_leaves, line, matching_bracket, component=_BracketSplitComponent.head
928 )
929 body = bracket_split_build_line(
930 body_leaves, line, matching_bracket, component=_BracketSplitComponent.body
931 )
932 tail = bracket_split_build_line(

Callers

nothing calls this directly

Calls 5

ensure_visibleFunction · 0.90
CannotSplitClass · 0.85
bracket_split_build_lineFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected