Python Function References
A VS Code/Cursor extension that analyzes Python functions and displays reference counts with detailed hover information.
Features
- Function Reference Counting: Automatically scans all Python files in your workspace and counts how many times each function is referenced
- Inline Decorations: Shows reference counts directly next to function definitions
- Hover Details: Hover over function definitions to see exactly where they are called
- Real-time Updates: Automatically refreshes when Python files are saved
- Workspace-wide Analysis: Recursively searches all Python files in your workspace
How It Works
- The extension scans all
.py files in your workspace recursively
- It identifies function definitions using regex pattern matching
- For each function, it searches for references (function calls) across all Python files
- Reference counts are displayed inline next to function definitions
- Hover over a function definition to see detailed information about where it's called
Usage
Automatic Analysis
- The extension automatically activates when you open a Python file
- It will scan your entire workspace and display reference counts
- Reference counts update automatically when you save Python files
Manual Commands
- Refresh References: Use
Ctrl+Shift+P → "Refresh Python Function References" to manually refresh
- Toggle Extension: Use
Ctrl+Shift+P → "Toggle Python Function References" to enable/disable
Visual Indicators
- Reference counts appear in gray text after function definitions
- Green text indicates functions with references
- Gray text indicates functions with no references
- Hover over any function definition to see detailed reference locations
Configuration
Access settings via File > Preferences > Settings and search for "Python Function References":
pythonFunctionRefs.enabled : Enable/disable the extension (default: true)
pythonFunctionRefs.refreshOnSave : Auto-refresh when Python files are saved (default: true)
Installation for Local Testing
- Clone/Download this extension to your local machine
- Install Dependencies:
npm install
- Compile TypeScript:
npm run compile
- Install Extension:
- Open VS Code/Cursor
- Go to Extensions view (
Ctrl+Shift+X )
- Click the
... menu and select "Install from VSIX..."
- Or use Command Palette (
Ctrl+Shift+P ) → "Extensions: Install from VSIX..."
- Navigate to the extension folder and select the generated
.vsix file (after packaging)
Development
Building the Extension
# Install dependencies
npm install
# Compile TypeScript
npm run compile
# Watch for changes during development
npm run watch
Packaging for Distribution
# Install vsce globally
npm install -g vsce
# Package the extension
vsce package
This will create a .vsix file that can be shared and installed.
Testing
- Open the extension folder in VS Code/Cursor
- Press
F5 to launch a new Extension Development Host window
- Open a Python project in the new window
- The extension should automatically activate and show function reference counts
How It Identifies Functions
The extension uses regex pattern matching to identify Python function definitions:
- Matches
def function_name( patterns
- Ignores private functions starting with
_ (except special methods like __init__ )
- Captures function name, file location, and line number
How It Counts References
Reference counting uses simple pattern matching:
- Searches for
function_name( patterns across all Python files
- Excludes the line where the function is defined
- Stores reference location (file, line, context) for hover display
Limitations
- Uses regex-based parsing (not AST), so may have edge cases with complex code
- Only detects direct function calls, not dynamic calls or imports
- Private functions (starting with
_ ) are ignored by default
- May not handle all Python syntax edge cases
Contributing
Feel free to submit issues and enhancement requests!
License
MIT License - see LICENSE file for details.
| |