Skip to content

Commit 9d69a58

Browse files
committed
DIRTY
1 parent 06ffc13 commit 9d69a58

File tree

9 files changed

+1910
-17
lines changed

9 files changed

+1910
-17
lines changed

codeflash/code_utils/code_extractor.py

Lines changed: 643 additions & 17 deletions
Large diffs are not rendered by default.

codeflash/optimization/function_optimizer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def generate_and_instrument_tests(
377377

378378
# note: this isn't called by the lsp, only called by cli
379379
def optimize_function(self) -> Result[BestOptimization, str]:
380+
get_opt_impact_metrics(self.function_to_optimize.file_path,self.function_to_optimize.qualified_name, self.project_root, self.test_cfg.tests_root)
380381
initialization_result = self.can_be_optimized()
381382
if not is_successful(initialization_result):
382383
return Failure(initialization_result.failure())

find_sorter_references.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to find all references to the sorter function from code_to_optimize/bubble_sort.py
4+
using Jedi's static analysis capabilities.
5+
"""
6+
7+
import jedi
8+
import os
9+
from pathlib import Path
10+
11+
12+
def find_function_references(file_path, line, column, project_root):
13+
"""
14+
Find all references to a function using Jedi.
15+
16+
Args:
17+
file_path: Path to the file containing the function
18+
line: Line number where the function is defined (1-indexed)
19+
column: Column number where the function name starts (0-indexed)
20+
project_root: Root directory of the project to search
21+
"""
22+
# Read the source code
23+
with open(file_path, 'r') as f:
24+
source = f.read()
25+
26+
# Create a Jedi Script object with project configuration
27+
project = jedi.Project(path=project_root)
28+
script = jedi.Script(source, path=file_path, project=project)
29+
30+
# Get the function definition at the specified position
31+
definitions = script.goto(line, column, follow_imports=True)
32+
33+
if not definitions:
34+
print(f"No definition found at {file_path}:{line}:{column}")
35+
return []
36+
37+
# Get the first definition (should be the function itself)
38+
definition = definitions[0]
39+
print(f"Found definition: {definition.name} at {definition.module_path}:{definition.line}")
40+
print(f"Type: {definition.type}")
41+
print("-" * 80)
42+
43+
# Use search_all to find all references to this function
44+
# We'll search for references by name throughout the project
45+
references = []
46+
try:
47+
# Use usages() method to get all references
48+
references = script.get_references(line, column, scope='project', include_builtins=False)
49+
except AttributeError:
50+
# Alternative approach using search
51+
print("Using alternative search method...")
52+
references = script.get_references(line, column, include_builtins=False)
53+
54+
return references
55+
56+
57+
def main():
58+
# Project root directory
59+
project_root = Path("/Users/aseemsaxena/Downloads/codeflash_dev/codeflash")
60+
61+
# Target file and function location
62+
target_file = project_root / "code_to_optimize" / "bubble_sort.py"
63+
64+
# The sorter function starts at line 1, column 4 (0-indexed)
65+
# "def sorter(arr):" - the function name 'sorter' starts at column 4
66+
line = 1 # Line number (1-indexed)
67+
column = 4 # Column number (0-indexed) - position of 's' in 'sorter'
68+
69+
print(f"Searching for references to 'sorter' function in {target_file}")
70+
print(f"Position: Line {line}, Column {column}")
71+
print("=" * 80)
72+
73+
# Find references
74+
references = find_function_references(target_file, line, column, project_root)
75+
76+
if references:
77+
print(f"\nFound {len(references)} reference(s) to 'sorter' function:")
78+
print("=" * 80)
79+
80+
# Group references by file
81+
refs_by_file = {}
82+
for ref in references:
83+
file_path = ref.module_path
84+
if file_path not in refs_by_file:
85+
refs_by_file[file_path] = []
86+
refs_by_file[file_path].append(ref)
87+
88+
# Display references organized by file
89+
for file_path, file_refs in sorted(refs_by_file.items()):
90+
print(f"\n📁 {file_path}")
91+
for ref in sorted(file_refs, key=lambda r: (r.line, r.column)):
92+
# Get the line content for context
93+
try:
94+
with open(file_path, 'r') as f:
95+
lines = f.readlines()
96+
if ref.line <= len(lines):
97+
line_content = lines[ref.line - 1].strip()
98+
print(f" Line {ref.line}, Col {ref.column}: {line_content}")
99+
else:
100+
print(f" Line {ref.line}, Col {ref.column}")
101+
except Exception as e:
102+
print(f" Line {ref.line}, Col {ref.column} (couldn't read line: {e})")
103+
else:
104+
print("\nNo references found to the 'sorter' function.")
105+
106+
print("\n" + "=" * 80)
107+
print("Search complete!")
108+
109+
110+
if __name__ == "__main__":
111+
main()

0 commit comments

Comments
 (0)