Handle fmt:off/on blocks that contain only comments. Returns True if a block was converted, False otherwise.
(
leaf: Leaf,
comment: ProtoComment,
previous_consumed: int,
mode: Mode,
)
| 243 | |
| 244 | |
| 245 | def _handle_comment_only_fmt_block( |
| 246 | leaf: Leaf, |
| 247 | comment: ProtoComment, |
| 248 | previous_consumed: int, |
| 249 | mode: Mode, |
| 250 | ) -> bool: |
| 251 | """Handle fmt:off/on blocks that contain only comments. |
| 252 | |
| 253 | Returns True if a block was converted, False otherwise. |
| 254 | """ |
| 255 | all_comments = list_comments(leaf.prefix, is_endmarker=False, mode=mode) |
| 256 | |
| 257 | # Find the first fmt:off and its matching fmt:on |
| 258 | fmt_off_idx = None |
| 259 | fmt_on_idx = None |
| 260 | for idx, c in enumerate(all_comments): |
| 261 | if fmt_off_idx is None and contains_fmt_directive(c.value, FMT_OFF): |
| 262 | fmt_off_idx = idx |
| 263 | if ( |
| 264 | fmt_off_idx is not None |
| 265 | and idx > fmt_off_idx |
| 266 | and contains_fmt_directive(c.value, FMT_ON) |
| 267 | ): |
| 268 | fmt_on_idx = idx |
| 269 | break |
| 270 | |
| 271 | # Only proceed if we found both directives |
| 272 | if fmt_on_idx is None or fmt_off_idx is None: |
| 273 | return False |
| 274 | |
| 275 | comment = all_comments[fmt_off_idx] |
| 276 | fmt_on_comment = all_comments[fmt_on_idx] |
| 277 | original_prefix = leaf.prefix |
| 278 | |
| 279 | # Build the hidden value |
| 280 | start_pos = comment.consumed |
| 281 | end_pos = fmt_on_comment.consumed |
| 282 | content_between_and_fmt_on = original_prefix[start_pos:end_pos] |
| 283 | hidden_value = comment.value + "\n" + content_between_and_fmt_on |
| 284 | |
| 285 | if hidden_value.endswith("\n"): |
| 286 | hidden_value = hidden_value[:-1] |
| 287 | |
| 288 | # Build the standalone comment prefix - preserve all content before fmt:off |
| 289 | # including any comments that precede it |
| 290 | if fmt_off_idx == 0: |
| 291 | # No comments before fmt:off, use previous_consumed |
| 292 | pre_fmt_off_consumed = previous_consumed |
| 293 | else: |
| 294 | # Use the consumed position of the last comment before fmt:off |
| 295 | # This preserves all comments and content before the fmt:off directive |
| 296 | pre_fmt_off_consumed = all_comments[fmt_off_idx - 1].consumed |
| 297 | |
| 298 | standalone_comment_prefix = ( |
| 299 | original_prefix[:pre_fmt_off_consumed] + "\n" * comment.newlines |
| 300 | ) |
| 301 | |
| 302 | fmt_off_prefix = original_prefix.split(comment.value)[0] |
no test coverage detected