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

Function list_comments

src/black/comments.py:90–134  ·  view source on GitHub ↗

Return a list of :class:`ProtoComment` objects parsed from the given `prefix`.

(prefix: str, *, is_endmarker: bool, mode: Mode)

Source from the content-addressed store, hash-verified

88
89@lru_cache(maxsize=4096)
90def list_comments(prefix: str, *, is_endmarker: bool, mode: Mode) -> list[ProtoComment]:
91 """Return a list of :class:`ProtoComment` objects parsed from the given `prefix`."""
92 result: list[ProtoComment] = []
93 if not prefix or "#" not in prefix:
94 return result
95
96 consumed = 0
97 nlines = 0
98 ignored_lines = 0
99 form_feed = False
100 for index, full_line in enumerate(re.split("\r?\n|\r", prefix)):
101 consumed += len(full_line) + 1 # adding the length of the split '\n'
102 match = re.match(r"^(\s*)(\S.*|)$", full_line)
103 assert match
104 whitespace, line = match.groups()
105 if not line:
106 nlines += 1
107 if "\f" in full_line:
108 form_feed = True
109 if not line.startswith("#"):
110 # Escaped newlines outside of a comment are not really newlines at
111 # all. We treat a single-line comment following an escaped newline
112 # as a simple trailing comment.
113 if line.endswith("\\"):
114 ignored_lines += 1
115 continue
116
117 if index == ignored_lines and not is_endmarker:
118 comment_type = token.COMMENT # simple trailing comment
119 else:
120 comment_type = STANDALONE_COMMENT
121 comment = make_comment(line, mode=mode)
122 result.append(
123 ProtoComment(
124 type=comment_type,
125 value=comment,
126 newlines=nlines,
127 consumed=consumed,
128 form_feed=form_feed,
129 leading_whitespace=whitespace,
130 )
131 )
132 form_feed = False
133 nlines = 0
134 return result
135
136
137def normalize_trailing_prefix(leaf: LN, total_consumed: int) -> None:

Callers 6

generate_commentsFunction · 0.85
convert_one_fmt_off_pairFunction · 0.85
is_fmt_onFunction · 0.85

Calls 4

make_commentFunction · 0.85
ProtoCommentClass · 0.85
matchMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected