|
1 | 1 | #!/usr/bin/env python3 |
2 | | -"""Example of using the ripgrep search script programmatically.""" |
| 2 | +"""Example usage of the function reference finder.""" |
3 | 3 |
|
4 | | -import json |
| 4 | +from find_function_references import find_function_references |
| 5 | +from find_function_references_detailed import find_function_references_detailed |
5 | 6 |
|
6 | | -from ripgrep_search import search_with_ripgrep |
7 | 7 |
|
8 | | -# Search for any pattern you want |
9 | | -pattern = "sorter" # Change this to any pattern you need |
10 | | -results = search_with_ripgrep(pattern) |
| 8 | +def example_basic_usage(): |
| 9 | + """Example of basic usage - just get list of files.""" |
| 10 | + # Example: Find references to a function |
| 11 | + filepath = "path/to/your/file.py" |
| 12 | + function_name = "your_function_name" |
11 | 13 |
|
12 | | -# Access the results as a dictionary |
13 | | -print(f"Found matches in {len(results)} files") |
| 14 | + # Find references (will auto-detect project root) |
| 15 | + reference_files = find_function_references(filepath, function_name) |
14 | 16 |
|
15 | | -# Iterate through the results |
16 | | -for filepath, occurrences in results.items(): |
17 | | - print(f"\n{filepath}: {len(occurrences)} matches") |
18 | | - for line_no, content in occurrences[:3]: # Show first 3 matches per file |
19 | | - print(f" Line {line_no}: {content[:80]}...") |
| 17 | + print(f"Files containing references to {function_name}:") |
| 18 | + for file in reference_files: |
| 19 | + print(f" - {file}") |
20 | 20 |
|
21 | | -# Save results to a JSON file if needed |
22 | | -with open("search_results.json", "w") as f: |
23 | | - json.dump(results, f, indent=2) |
| 21 | + return reference_files |
24 | 22 |
|
25 | | -# Or filter results for specific files |
26 | | -python_files_only = {path: matches for path, matches in results.items() if path.endswith(".py")} |
27 | 23 |
|
28 | | -print(f"\nPython files with matches: {len(python_files_only)}") |
| 24 | +def example_detailed_usage(): |
| 25 | + """Example of detailed usage - get line numbers and context.""" |
| 26 | + # Example: Find references with detailed information |
| 27 | + filepath = "path/to/your/file.py" |
| 28 | + function_name = "your_function_name" |
| 29 | + project_root = "/path/to/project/root" # Optional |
| 30 | + |
| 31 | + # Find detailed references |
| 32 | + references = find_function_references_detailed(filepath, function_name, project_root) |
| 33 | + |
| 34 | + print(f"\nDetailed references to {function_name}:") |
| 35 | + for file, refs in references.items(): |
| 36 | + print(f"\nFile: {file}") |
| 37 | + for ref in refs: |
| 38 | + print(f" Line {ref['line']}: {ref['context']}") |
| 39 | + |
| 40 | + return references |
| 41 | + |
| 42 | + |
| 43 | +def example_programmatic_usage(): |
| 44 | + """Example of using in your own Python code.""" |
| 45 | + import jedi |
| 46 | + |
| 47 | + # Direct Jedi usage for more control |
| 48 | + source_code = """ |
| 49 | +def my_function(x, y): |
| 50 | + return x + y |
| 51 | +
|
| 52 | +result = my_function(1, 2) |
| 53 | +""" |
| 54 | + |
| 55 | + # Create a script object |
| 56 | + script = jedi.Script(code=source_code) |
| 57 | + |
| 58 | + # Find the function definition (line 2, column 4 for 'my_function') |
| 59 | + references = script.get_references(line=2, column=4) |
| 60 | + |
| 61 | + print("Direct Jedi references:") |
| 62 | + for ref in references: |
| 63 | + print(f" Line {ref.line}, Column {ref.column}: {ref.description}") |
| 64 | + |
| 65 | + # You can also search for names |
| 66 | + names = script.get_names() |
| 67 | + for name in names: |
| 68 | + if name.type == "function": |
| 69 | + print(f"Found function: {name.name} at line {name.line}") |
| 70 | + |
| 71 | + |
| 72 | +def example_find_all_functions_and_their_references(): |
| 73 | + """Example of finding all functions in a file and their references.""" |
| 74 | + import os |
| 75 | + |
| 76 | + import jedi |
| 77 | + |
| 78 | + def find_all_functions_with_references(filepath: str): |
| 79 | + """Find all functions in a file and their references.""" |
| 80 | + with open(filepath) as f: |
| 81 | + source_code = f.read() |
| 82 | + |
| 83 | + script = jedi.Script(code=source_code, path=filepath) |
| 84 | + |
| 85 | + # Get all defined names |
| 86 | + names = script.get_names() |
| 87 | + |
| 88 | + functions_and_refs = {} |
| 89 | + |
| 90 | + for name in names: |
| 91 | + if name.type == "function": |
| 92 | + # Get references for this function |
| 93 | + refs = script.get_references(line=name.line, column=name.column) |
| 94 | + |
| 95 | + ref_locations = [] |
| 96 | + for ref in refs: |
| 97 | + if ref.module_path and str(ref.module_path) != filepath: |
| 98 | + ref_locations.append({"file": str(ref.module_path), "line": ref.line, "column": ref.column}) |
| 99 | + |
| 100 | + functions_and_refs[name.name] = {"definition_line": name.line, "references": ref_locations} |
| 101 | + |
| 102 | + return functions_and_refs |
| 103 | + |
| 104 | + # Example usage |
| 105 | + filepath = "your_module.py" |
| 106 | + if os.path.exists(filepath): |
| 107 | + all_refs = find_all_functions_with_references(filepath) |
| 108 | + |
| 109 | + for func_name, info in all_refs.items(): |
| 110 | + print(f"\nFunction: {func_name} (defined at line {info['definition_line']})") |
| 111 | + if info["references"]: |
| 112 | + print(" Referenced in:") |
| 113 | + for ref in info["references"]: |
| 114 | + print(f" - {ref['file']}:{ref['line']}") |
| 115 | + else: |
| 116 | + print(" No external references found") |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + print("=" * 60) |
| 121 | + print("Function Reference Finder - Usage Examples") |
| 122 | + print("=" * 60) |
| 123 | + |
| 124 | + print("\nNote: Update the file paths and function names in the examples") |
| 125 | + print("before running them with your actual code.\n") |
| 126 | + |
| 127 | + # Uncomment to run examples: |
| 128 | + # example_basic_usage() |
| 129 | + # example_detailed_usage() |
| 130 | + # example_programmatic_usage() |
| 131 | + # example_find_all_functions_and_their_references() |
0 commit comments