-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblocks.py
More file actions
46 lines (36 loc) · 1.49 KB
/
blocks.py
File metadata and controls
46 lines (36 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Den Rozhnovskiy
from __future__ import annotations
from typing import TYPE_CHECKING
from .merge import coerce_positive_int, merge_overlapping_items
if TYPE_CHECKING:
from .types import GroupItem, GroupItemLike, GroupItemsLike, GroupMap, GroupMapLike
def block_item_sort_key(item: GroupItemLike) -> tuple[str, str, int, int]:
start_line = coerce_positive_int(item.get("start_line")) or 0
end_line = coerce_positive_int(item.get("end_line")) or 0
return (
str(item.get("filepath", "")),
str(item.get("qualname", "")),
start_line,
end_line,
)
def merge_block_items(items: GroupItemsLike) -> list[GroupItem]:
return merge_overlapping_items(items, sort_key=block_item_sort_key)
def prepare_block_report_groups(block_groups: GroupMapLike) -> GroupMap:
"""
Convert sliding block windows into maximal merged regions for reporting.
Block hash keys remain unchanged.
"""
prepared: GroupMap = {}
for key, items in block_groups.items():
merged = merge_block_items(items)
if merged:
prepared[key] = merged
else:
prepared[key] = [
dict(item) for item in sorted(items, key=block_item_sort_key)
]
return prepared