Skip to content
This repository was archived by the owner on Mar 27, 2026. It is now read-only.

Commit 6e9d976

Browse files
committed
update ai docs
1 parent b336511 commit 6e9d976

4 files changed

Lines changed: 172 additions & 22 deletions

File tree

File renamed without changes.

ai-docs/refactor-guide.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
Okay, let's break down the suggested refactoring for `csharp_parser.rs` and `filter_manager.rs` based on the guidelines and analysis.
2+
3+
## Refactoring `csharp_parser.rs`
4+
5+
The primary area for improvement is the `find_nodes` function, which is long and complex. We can refactor it by extracting logic for each node type into separate, smaller functions.
6+
7+
**Refactoring Steps for `csharp_parser.rs`:**
8+
9+
1. **Extract Node Handling Functions:** Create separate functions for handling each node type within `find_nodes`. This will make `find_nodes` shorter and the logic for each node type more isolated and easier to understand.
10+
11+
* `fn handle_method_declaration(node: Node, code: &str, file: &mut CSharpFile)`
12+
* `fn handle_property_declaration(node: Node, code: &str, file: &mut CSharpFile)`
13+
* `fn handle_using_directive(node: Node, code: &str, file: &mut CSharpFile)`
14+
* `fn handle_namespace_declaration(node: Node, code: &str, file: &mut CSharpFile)`
15+
* `fn handle_class_declaration(node: Node, code: &str, file: &mut CSharpFile)`
16+
17+
2. **Simplify `find_nodes`:** Update `find_nodes` to use these new handler functions within the `match` statement. `find_nodes` will primarily be responsible for traversing the tree and dispatching to the appropriate handler.
18+
19+
3. **Review and Simplify Property Declaration Handling:** The logic for properties (especially with accessors and arrow expressions) is complex. Within `handle_property_declaration`, consider further breaking down the accessor handling into helper functions if it remains too long or complex.
20+
21+
4. **Consider Unifying Change Detection Functions:** Evaluate if `node_contains_changes` and `method_contains_changes` can be unified or if `node_contains_changes` is truly necessary. If their logic is very similar, consolidation would reduce code duplication. If they are distinct, ensure their purpose is clear and they are named appropriately.
22+
23+
5. **Improve Clarity and Comments:** Review variable names within the new handler functions for clarity. Add comments to explain complex logic within these functions.
24+
25+
26+
**Example of Refactored `find_nodes` and extracted handler (Conceptual - may need adjustments based on exact logic):**
27+
28+
```rust
29+
impl CSharpParser {
30+
// ... (rest of CSharpParser implementation) ...
31+
32+
fn find_nodes(&self, node: Node, code: &str, file: &mut CSharpFile) {
33+
match node.kind() {
34+
"method_declaration" => self.handle_method_declaration(node, code, file),
35+
"property_declaration" => self.handle_property_declaration(node, code, file),
36+
"using_directive" => self.handle_using_directive(node, file),
37+
"namespace_declaration" => self.handle_namespace_declaration(node, file),
38+
"class_declaration" => self.handle_class_declaration(node, file),
39+
_ => {}
40+
}
41+
42+
let mut cursor = node.walk();
43+
for child in node.children(&mut cursor) {
44+
self.find_nodes(child, code, file);
45+
}
46+
}
47+
48+
fn handle_method_declaration(&self, node: Node, code: &str, file: &mut CSharpFile) {
49+
let start_line = node.start_position().row + 1;
50+
let end_line = node.end_position().row + 1;
51+
52+
// Find the signature line
53+
let signature_line = node.child_by_field_name("header")
54+
.map(|n| n.start_position().row + 1)
55+
.unwrap_or(start_line);
56+
57+
let text = node.utf8_text(code.as_bytes())
58+
.unwrap_or_default()
59+
.to_string();
60+
61+
file.methods.push(CSharpMethod {
62+
start_line,
63+
end_line,
64+
signature_line,
65+
text,
66+
has_changes: false,
67+
});
68+
}
69+
70+
fn handle_property_declaration(&self, node: Node, code: &str, file: &mut CSharpFile) {
71+
// ... (Extracted property declaration handling logic from original find_nodes) ...
72+
}
73+
74+
fn handle_using_directive(&self, node: Node, file: &mut CSharpFile) {
75+
let start_line = node.start_position().row + 1;
76+
let end_line = node.end_position().row + 1;
77+
file.using_statements.push((start_line, end_line));
78+
}
79+
80+
fn handle_namespace_declaration(&self, node: Node, file: &mut CSharpFile) {
81+
let start_line = node.start_position().row + 1;
82+
let end_line = node.end_position().row + 1;
83+
file.namespace_declarations.push((start_line, end_line));
84+
}
85+
86+
fn handle_class_declaration(&self, node: Node, file: &mut CSharpFile) {
87+
let start_line = node.start_position().row + 1;
88+
let end_line = node.end_position().row + 1;
89+
file.class_declarations.push((start_line, end_line));
90+
}
91+
92+
// ... (rest of CSharpParser implementation) ...
93+
}
94+
```
95+
96+
97+
## Refactoring `filter_manager.rs`
98+
99+
The `process_csharp_file` function is the most complex part of `filter_manager.rs`. We need to break it down into smaller, more manageable functions.
100+
101+
**Refactoring Steps for `filter_manager.rs`:**
102+
103+
1. **Extract Hunk Processing Stages into Functions:** Divide `process_csharp_file` into functions representing logical stages of processing.
104+
105+
* `fn calculate_context_lines_and_changes(hunk: &Hunk, rule: &FilterRule) -> (HashSet<usize>, Vec<usize>)`: Extracts the logic to compute `context_lines_set` and `change_locations`.
106+
* `fn identify_changed_and_contextual_methods(file_info: &CSharpFile, context_lines_set: &HashSet<usize>, rule: &FilterRule) -> (Vec<&CSharpMethod>, Vec<&CSharpMethod>)`: Extracts the logic to identify `changed_methods` and `contextual_methods`.
107+
* `fn process_hunk_lines(hunk: &Hunk, file_info: &CSharpFile, context_lines_set: &HashSet<usize>, changed_methods: &[&CSharpMethod], contextual_methods: &[&CSharpMethod], rule: &FilterRule) -> Vec<String>`: Extracts the line-by-line processing logic within the hunk. This function will handle the conditional inclusion of lines based on method type (changed, contextual, other).
108+
109+
2. **Simplify Logic within Extracted Functions:** Within each extracted function (especially `process_hunk_lines`), review and simplify the conditional logic for determining `should_include` and `should_add_placeholder`. Consider using helper functions for specific checks if the logic remains complex.
110+
111+
3. **Improve Variable Naming:** Ensure variable names in the extracted functions and `process_csharp_file` are descriptive and easy to understand.
112+
113+
4. **Replace `reconstruct_file_content` (Future Enhancement):** As a separate, but important step, plan to replace `reconstruct_file_content` with a proper mechanism to retrieve file content from Git using `GitOperations`. This might involve adding a method to `GitOperations` to get file content at a specific commit. This is crucial for long-term correctness and reliability.
114+
115+
5. **Consider C#-Specific Logic Module (Optional):** If `filter_manager.rs` becomes too large and complex due to the C#-specific filtering, think about moving the `process_csharp_file` function and related helper functions into a separate module (e.g., `csharp_filter.rs`) within the `filters` directory. This would further separate concerns.
116+
117+
118+
**Example of Refactored `process_csharp_file` (Conceptual - may need adjustments):**
119+
120+
```rust
121+
impl FilterManager {
122+
// ... (rest of FilterManager implementation) ...
123+
124+
fn process_csharp_file(&mut self, hunks: &[Hunk], rule: &FilterRule, code: &str) -> Vec<Hunk> {
125+
if !rule.include_method_body && !rule.include_signatures {
126+
return self.apply_context_filter(hunks, rule.context_lines);
127+
}
128+
129+
let file_info = self.csharp_parser.parse_file(code, hunks);
130+
let mut processed_hunks = Vec::new();
131+
132+
for hunk in hunks {
133+
let mut new_hunk = hunk.clone();
134+
135+
let (context_lines_set, _change_locations) = self.calculate_context_lines_and_changes(hunk, rule);
136+
let (changed_methods, contextual_methods) = self.identify_changed_and_contextual_methods(&file_info, &context_lines_set, rule);
137+
let new_lines = self.process_hunk_lines(hunk, &file_info, &context_lines_set, &changed_methods, &contextual_methods, rule);
138+
139+
new_hunk.lines = new_lines;
140+
new_hunk.new_count = new_hunk.lines.iter().filter(|l| !l.starts_with('-')).count();
141+
new_hunk.old_count = new_hunk.lines.iter().filter(|l| !l.starts_with('+')).count();
142+
143+
if !new_hunk.lines.is_empty() {
144+
processed_hunks.push(new_hunk);
145+
}
146+
}
147+
148+
processed_hunks
149+
}
150+
151+
fn calculate_context_lines_and_changes(&self, hunk: &Hunk, rule: &FilterRule) -> (HashSet<usize>, Vec<usize>) {
152+
// ... (Extracted context lines and changes calculation logic from original process_csharp_file) ...
153+
}
154+
155+
fn identify_changed_and_contextual_methods(&self, file_info: &CSharpFile, context_lines_set: &HashSet<usize>, rule: &FilterRule) -> (Vec<&CSharpMethod>, Vec<&CSharpMethod>) {
156+
// ... (Extracted method identification logic from original process_csharp_file) ...
157+
}
158+
159+
fn process_hunk_lines(&self, hunk: &Hunk, file_info: &CSharpFile, context_lines_set: &HashSet<usize>, changed_methods: &[&CSharpMethod], contextual_methods: &[&CSharpMethod], rule: &FilterRule) -> Vec<String> {
160+
// ... (Extracted line processing logic from original process_csharp_file) ...
161+
}
162+
163+
// ... (rest of FilterManager implementation) ...
164+
}
165+
```
166+
167+
168+
**Testing:**
169+
170+
After each refactoring step, especially after breaking down functions, ensure you run the existing tests to check for regressions. You should also add new unit tests specifically targeting the refactored functions and covering different scenarios of C# filtering (various combinations of `include_method_body`, `include_signatures`, and different code structures in C# files). This is crucial to ensure the refactoring maintains the original functionality and improves code quality.
171+
172+
By following these steps, you'll make the `csharp_parser.rs` and `filter_manager.rs` code more modular, easier to understand, and more maintainable, aligning with the principles of "Building Maintainable Software". Remember to proceed incrementally, testing after each step to ensure you don't introduce regressions.
File renamed without changes.

ai-workspace/progress.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)