Skip to main content

brokk_bifrost_python/
adapter.rs

1//! The Python answers behind `PythonAdapter`.
2//!
3//! `LanguageAdapter` is analysis-owned, so the trait impl itself stays in
4//! `analyzer/python/adapter.rs`; every answer it gives comes from here or from
5//! [`crate::declarations`]. `synthesize_hydrated_units` is the one exception:
6//! it mutates `FileState`, an analysis type, and stays with the shell.
7
8use brokk_bifrost_core::analyzer::cognitive_complexity;
9use std::sync::LazyLock;
10
11use crate::declarations::python_is_decorated_function_boundary;
12
13pub const PYTHON_FILE_EXTENSION: &str = "py";
14
15/// Tree-sitter node-kind mapping used by the cognitive-complexity scorer
16/// for Python. Mirrors `ai.brokk.analyzer.python.CognitiveComplexityAnalysis`.
17pub static PYTHON_COGNITIVE_CONFIG: LazyLock<cognitive_complexity::Config> =
18    LazyLock::new(|| cognitive_complexity::Config {
19        if_types: &["if_statement"],
20        alternate_if_types: &["elif_clause"],
21        loop_types: &["for_statement", "while_statement"],
22        catch_types: &["except_clause"],
23        conditional_types: &["conditional_expression"],
24        case_types: &["case_clause"],
25        binary_types: &["boolean_operator"],
26        logical_operators: &["and", "or"],
27        named_function_boundary_types: &["function_definition"],
28        anonymous_function_types: &["lambda"],
29        named_function_boundary_predicate: Some(python_is_decorated_function_boundary),
30        ..cognitive_complexity::Config::empty()
31    });
32
33pub fn python_extract_call_receiver(reference: &str) -> Option<String> {
34    let trimmed = reference.trim();
35    let before_args = trimmed
36        .split_once('(')
37        .map(|(head, _)| head)
38        .unwrap_or(trimmed);
39    before_args
40        .rsplit_once('.')
41        .map(|(receiver, _)| receiver.to_string())
42}