Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""What is measured, and the dispatch from a file to its language scanner."""
|
|
from dataclasses import dataclass
|
|
|
|
KINDS = ("fn_lines", "complexity", "nesting", "width", "params")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Fn:
|
|
"""One function: its qualified key, its span (1-based, inclusive) and its five measures."""
|
|
key: str
|
|
start: int
|
|
end: int
|
|
fn_lines: int
|
|
complexity: int
|
|
nesting: int
|
|
width: int # lines wider than the limit inside the span
|
|
test: bool
|
|
params: int = 0 # parameters in the signature, `self` included
|
|
|
|
def measures(self):
|
|
return {kind: getattr(self, kind) for kind in KINDS}
|
|
|
|
|
|
def line_width(line, tab_width):
|
|
"""Columns of a line: a tab counts `tab_width`, the line ending does not count."""
|
|
return len(line.rstrip("\r\n").replace("\t", " " * tab_width))
|
|
|
|
|
|
def wide_lines(text, limit, tab_width):
|
|
"""1-based numbers of the lines wider than `limit` columns."""
|
|
return {n for n, line in enumerate(text.splitlines(), 1) if line_width(line, tab_width) > limit}
|
|
|
|
|
|
def allowed(kind, limits):
|
|
"""What a measure may be without an exemption: the limit, except `width`, which counts wide lines (none allowed)."""
|
|
return 0 if kind == "width" else limits[kind]
|
|
|
|
|
|
def over(measures, limits):
|
|
"""{kind: value} for the measures above what is allowed."""
|
|
return {kind: value for kind, value in measures.items() if value > allowed(kind, limits)}
|
|
|
|
|
|
def count_wide(wide, start, end):
|
|
return sum(1 for n in wide if start <= n <= end)
|