Cursor AI
Interacting with your Codebase
Demo Codebase QA
Discover how the codebase feature transforms your repository into a searchable, context-aware knowledge base. By querying your entire project structure, you can quickly locate configuration blocks, data-processing routines, and CLI commands—boosting developer productivity and easing onboarding.
Complete Application Example
Below is a sample Flask application (app.py
) that we’ll use to demonstrate codebase queries:
# app.py
import csv
import sqlite3
import os
import hashlib
import logging
from datetime import datetime
from flask import Flask, render_template, request, redirect, url_for, flash, session, g
# Initialize Flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dev' # Change this to a random secret key in production
app.config['DATABASE'] = os.path.join(app.instance_path, 'task_manager.sqlite')
# Ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
def read_csv(file_path):
"""Read rows from a CSV file and print them."""
with open(file_path, 'r') as f:
csvreader = csv.reader(f)
for row in csvreader:
print(row)
# Database connection functions
def get_db():
"""Return a database connection, creating it if necessary."""
if 'db' not in g:
g.db = sqlite3.connect(
app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
"""Close the database connection at teardown."""
db = g.pop('db', None)
if db is not None:
db.close()
app.teardown_appcontext(close_db)
# Initialize the database using a schema file
def init_db():
db = get_db()
with app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
print('Initialized the database.')
@app.cli.command('init-db')
def init_db_command():
"""Clear existing data and create new tables."""
init_db()
print('Initialized the database.')
Warning
Be sure to replace app.config['SECRET_KEY']
with a secure, random value before deploying to production.
Querying the Codebase
Leverage natural-language queries to pinpoint exactly what you need:
1. Finding the Database Connection
Ask: “Show me all the code used to connect to the database.”
def get_db():
"""Return a database connection, creating it if necessary."""
if 'db' not in g:
g.db = sqlite3.connect(
app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
Configuration line:
app.config['DATABASE'] = os.path.join(app.instance_path, 'task_manager.sqlite')
Teardown registration:
app.teardown_appcontext(close_db)
2. Locating CSV-Related Code
Ask: “Which parts of the code deal with CSV files?”
def read_csv(file_path):
"""Read rows from a CSV file and print them."""
with open(file_path, 'r') as f:
csvreader = csv.reader(f)
for row in csvreader:
print(row)
3. How the Database Is Initialized
Ask: “How does our application initialize a database?”
def init_db():
db = get_db()
with app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
print('Initialized the database.')
And the CLI command:
@app.cli.command('init-db')
def init_db_command():
"""Clear existing data and create new tables."""
init_db()
print('Initialized the database.')
Advanced Codebase Queries
Use @codebase
filters to refine searches by file type or pattern:
@codebase include:*.py exclude:*.js Show me all functions related to authentication
Filter | Description | Example |
---|---|---|
include:<ext> | Include only files with the given extension | include:*.py |
exclude:<ext> | Exclude files by extension | exclude:*.js |
name:<pattern> | Search files by name pattern | name:auth* |
Why Use This Feature?
- Rapidly locate configuration and connection logic
- Explore data-processing functions (e.g., CSV import)
- Inspect CLI commands and initialization routines
- Simplify refactoring by searching specific patterns
Note
Combine multiple filters and search terms to focus on specific modules or code patterns across your project.
Whether you’re migrating from SQLite to PostgreSQL, integrating an ORM like SQLAlchemy, or diving into an unfamiliar codebase, the codebase feature provides instant, context-aware insights into your entire repository.
Links and References
Watch Video
Watch video content