pmat 2.93.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Enhanced Python AST visitor that preserves real source locations and qualified names
//!
//! This module provides an enhanced visitor that extracts actual AST information
//! from RustPython-parsed Python code instead of generating placeholders,
//! enabling MCP tools to query precise code locations and symbol names.

#[cfg(feature = "python-ast")]
use crate::services::context::AstItem;
#[cfg(feature = "python-ast")]
use std::path::{Path, PathBuf};
#[cfg(feature = "python-ast")]
use rustpython_parser::ast::{Stmt, Expr, ModModule};

/// Enhanced Python AST visitor that preserves real source information
#[cfg(feature = "python-ast")]
pub struct EnhancedPythonVisitor {
    items: Vec<AstItem>,
    _file_path: PathBuf,
    module_path: Vec<String>,
    class_stack: Vec<String>,
}

#[cfg(feature = "python-ast")]
impl EnhancedPythonVisitor {
    /// Creates a new enhanced Python visitor
    #[must_use] 
    pub fn new(file_path: &Path) -> Self {
        Self {
            items: Vec::new(),
            _file_path: file_path.to_path_buf(),
            module_path: Vec::new(),
            class_stack: Vec::new(),
        }
    }

    /// Extracts AST items from a Python module
    #[must_use] 
    pub fn extract_items(mut self, module: &ModModule) -> Vec<AstItem> {
        self.visit_module(module);
        self.items
    }

    /// Gets the current qualified name for a symbol
    fn get_qualified_name(&self, name: &str) -> String {
        let mut parts = Vec::new();

        // Add module path
        parts.extend(self.module_path.iter().cloned());

        // Add class context
        parts.extend(self.class_stack.iter().cloned());

        // Add the name itself
        parts.push(name.to_string());
        parts.join("::")
    }

    /// Gets line number (simplified for compatibility)
    fn get_line(&self, _stmt: &Stmt) -> usize {
        // For now, return line 1 - real line extraction needs investigating RustPython API
        1
    }

    /// Checks if function is async
    fn is_async_function(&self, decorators: &[Expr]) -> bool {
        decorators.iter().any(|decorator| {
            if let Expr::Name(name) = decorator {
                name.id.as_str() == "async"
            } else {
                false
            }
        })
    }

    /// Visits the module root
    fn visit_module(&mut self, module: &ModModule) {
        for stmt in &module.body {
            self.visit_stmt(stmt);
        }
    }

    /// Visits a statement
    fn visit_stmt(&mut self, stmt: &Stmt) {
        match stmt {
            Stmt::FunctionDef(func) => self.visit_function_def(func),
            Stmt::AsyncFunctionDef(func) => self.visit_async_function_def(func),
            Stmt::ClassDef(class) => self.visit_class_def(class),
            _ => {
                // Handle other statements recursively if needed
                self.visit_stmt_children(stmt);
            }
        }
    }

    /// Visits function definition
    fn visit_function_def(&mut self, func: &rustpython_parser::ast::StmtFunctionDef) {
        let name = self.get_qualified_name(func.name.as_ref());
        let line = self.get_line(&Stmt::FunctionDef(func.clone()));
        let is_async = self.is_async_function(&func.decorator_list);

        self.items.push(AstItem::Function {
            name,
            visibility: "public".to_string(), // Python doesn't have explicit visibility
            is_async,
            line,
        });

        // Visit function body
        for stmt in &func.body {
            self.visit_stmt(stmt);
        }
    }

    /// Visits async function definition
    fn visit_async_function_def(&mut self, func: &rustpython_parser::ast::StmtAsyncFunctionDef) {
        let name = self.get_qualified_name(func.name.as_ref());
        let line = self.get_line(&Stmt::AsyncFunctionDef(func.clone()));

        self.items.push(AstItem::Function {
            name,
            visibility: "public".to_string(),
            is_async: true, // Async functions are always async
            line,
        });

        // Visit function body
        for stmt in &func.body {
            self.visit_stmt(stmt);
        }
    }

    /// Visits class definition
    fn visit_class_def(&mut self, class: &rustpython_parser::ast::StmtClassDef) {
        let name = self.get_qualified_name(class.name.as_ref());
        let line = self.get_line(&Stmt::ClassDef(class.clone()));

        // Count methods (functions within the class)
        let fields_count = class.body.iter()
            .filter(|stmt| matches!(stmt, Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_)))
            .count();

        self.items.push(AstItem::Struct {
            name: name.clone(),
            visibility: "public".to_string(),
            fields_count,
            derives: vec![], // Python doesn't have derives like Rust
            line,
        });

        // Enter class context
        self.class_stack.push(class.name.to_string());

        // Visit class body
        for stmt in &class.body {
            self.visit_stmt(stmt);
        }

        // Exit class context
        self.class_stack.pop();
    }

    /// Visits children of other statement types
    fn visit_stmt_children(&mut self, stmt: &Stmt) {
        match stmt {
            Stmt::If(if_stmt) => {
                for stmt in &if_stmt.body {
                    self.visit_stmt(stmt);
                }
                for stmt in &if_stmt.orelse {
                    self.visit_stmt(stmt);
                }
            }
            Stmt::While(while_stmt) => {
                for stmt in &while_stmt.body {
                    self.visit_stmt(stmt);
                }
                for stmt in &while_stmt.orelse {
                    self.visit_stmt(stmt);
                }
            }
            Stmt::For(for_stmt) => {
                for stmt in &for_stmt.body {
                    self.visit_stmt(stmt);
                }
                for stmt in &for_stmt.orelse {
                    self.visit_stmt(stmt);
                }
            }
            _ => {
                // Other statement types can be handled here
            }
        }
    }
}

#[cfg(all(test, feature = "python-ast"))]
mod tests {
    use super::*;
    use rustpython_parser::Parse;
    use std::path::Path;

    fn parse_python(code: &str) -> ModModule {
        ModModule::parse(code, "test.py")
            .expect("Failed to parse Python code")
    }

    #[test]
    fn test_simple_function() {
        let code = r#"
def hello_world():
    print("Hello, World!")
"#;
        let module = parse_python(code);
        let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
        let items = visitor.extract_items(&module);

        assert_eq!(items.len(), 1);
        if let AstItem::Function { name, is_async, .. } = &items[0] {
            assert_eq!(name, "hello_world");
            assert!(!is_async);
        } else {
            panic!("Expected function item");
        }
    }

    #[test]
    fn test_async_function() {
        let code = r#"
async def async_hello():
    await some_task()
"#;
        let module = parse_python(code);
        let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
        let items = visitor.extract_items(&module);

        assert_eq!(items.len(), 1);
        if let AstItem::Function { name, is_async, .. } = &items[0] {
            assert_eq!(name, "async_hello");
            assert!(is_async);
        } else {
            panic!("Expected async function item");
        }
    }

    #[test]
    fn test_class_with_methods() {
        let code = r#"
class Calculator:
    def add(self, a, b):
        return a + b

    async def multiply_async(self, a, b):
        return a * b
"#;
        let module = parse_python(code);
        let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
        let items = visitor.extract_items(&module);

        assert_eq!(items.len(), 3); // 1 class + 2 methods

        // Check class
        if let AstItem::Struct { name, fields_count, .. } = &items[0] {
            assert_eq!(name, "Calculator");
            assert_eq!(*fields_count, 2); // 2 methods
        } else {
            panic!("Expected class item");
        }

        // Check methods
        if let AstItem::Function { name, is_async, .. } = &items[1] {
            assert_eq!(name, "Calculator::add");
            assert!(!is_async);
        } else {
            panic!("Expected method item");
        }

        if let AstItem::Function { name, is_async, .. } = &items[2] {
            assert_eq!(name, "Calculator::multiply_async");
            assert!(is_async);
        } else {
            panic!("Expected async method item");
        }
    }

    #[test]
    fn test_nested_functions() {
        let code = r#"
def outer_function():
    def inner_function():
        pass
    inner_function()
"#;
        let module = parse_python(code);
        let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
        let items = visitor.extract_items(&module);

        assert_eq!(items.len(), 2);

        if let AstItem::Function { name, .. } = &items[0] {
            assert_eq!(name, "outer_function");
        } else {
            panic!("Expected outer function");
        }

        if let AstItem::Function { name, .. } = &items[1] {
            assert_eq!(name, "inner_function");
        } else {
            panic!("Expected inner function");
        }
    }

    #[test]
    fn test_complex_qualified_names() {
        let code = r#"
class Database:
    class Connection:
        def connect(self):
            pass

        async def disconnect(self):
            pass
"#;
        let module = parse_python(code);
        let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
        let items = visitor.extract_items(&module);

        // Should have: Database class, Connection class, connect method, disconnect method
        assert_eq!(items.len(), 4);

        // Check qualified names for nested class methods
        let names: Vec<String> = items.iter().map(|item| match item {
            AstItem::Function { name, .. } => name.clone(),
            AstItem::Struct { name, .. } => name.clone(),
            _ => "unknown".to_string(),
        }).collect();

        assert!(names.contains(&"Database".to_string()));
        assert!(names.contains(&"Database::Connection".to_string()));
        assert!(names.contains(&"Database::Connection::connect".to_string()));
        assert!(names.contains(&"Database::Connection::disconnect".to_string()));
    }
}

#[cfg(all(test, feature = "python-ast"))]
mod property_tests {
    use super::*;
    use proptest::prelude::*;
    use rustpython_parser::Parse;

    proptest! {
        #[test]
        fn test_visitor_handles_any_valid_python(
            func_name in "[a-zA-Z_][a-zA-Z0-9_]*",
            class_name in "[a-zA-Z_][a-zA-Z0-9_]*"
        ) {
            let code = format!(r#"
class {}:
    def {}(self):
        pass
"#, class_name, func_name);

            if let Ok(module) = ModModule::parse(&code, "test.py") {
                let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
                let items = visitor.extract_items(&module);

                // Should have at least class and method
                prop_assert!(items.len() >= 2);

                // Check that we get real names, not placeholders
                let has_real_names = items.iter().any(|item| match item {
                    AstItem::Function { name, .. } => !name.starts_with("function_"),
                    AstItem::Struct { name, .. } => !name.starts_with("class_"),
                    _ => true,
                });
                prop_assert!(has_real_names);
            }
        }

        #[test]
        fn test_visitor_complexity_bounds(
            function_count in 1usize..10,
        ) {
            let mut code = String::new();
            for i in 0..function_count {
                code.push_str(&format!("def function_{}(): pass\n", i));
            }

            if let Ok(module) = ModModule::parse(&code, "test.py") {
                let visitor = EnhancedPythonVisitor::new(Path::new("test.py"));
                let items = visitor.extract_items(&module);

                // Should extract all functions
                prop_assert_eq!(items.len(), function_count);

                // All should be functions with real names
                for (i, item) in items.iter().enumerate() {
                    if let AstItem::Function { name, .. } = item {
                        prop_assert_eq!(name, &format!("function_{}", i));
                    } else {
                        prop_assert!(false, "Expected function item");
                    }
                }
            }
        }
    }
}