#################################### Tutorial 4: Rules 与配置 #################################### .. include:: ../links.ref .. include:: ../tags.ref .. include:: ../abbrs.ref ============ ========================== **Abstract** Cursor Rules 配置与最佳实践 **Authors** Walter Fan **Status** WIP **Updated** |date| ============ ========================== .. contents:: :local: 什么是 Cursor Rules =================== Cursor Rules 是一种让 AI 遵循特定规范的机制。通过配置 Rules,你可以: - 定义代码风格和规范 - 指定技术栈和框架 - 设置项目特定的约定 - 提供领域知识和上下文 为什么需要 Rules ---------------- 没有 Rules 时: - AI 可能使用你不喜欢的代码风格 - AI 可能不了解你的项目结构 - AI 可能使用过时的 API - 每次都需要重复说明要求 有了 Rules: - AI 自动遵循你的规范 - 代码风格一致 - 减少重复沟通 - 提高生成质量 Rules 的类型 ============ Cursor 支持多种类型的 Rules: .. code-block:: text Rules 类型 ┌─────────────────────────────────────────────────────────────────┐ │ │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Global Rules │ │ Project Rules │ │ │ │ 全局规则 │ │ 项目规则 │ │ │ ├─────────────────┤ ├─────────────────┤ │ │ │ ~/.cursor/rules │ │ .cursor/rules │ │ │ │ 所有项目生效 │ │ 仅当前项目生效 │ │ │ └─────────────────┘ └─────────────────┘ │ │ │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │ .cursorrules │ │ Cursor Settings │ │ │ │ 项目根目录 │ │ 设置中配置 │ │ │ ├─────────────────┤ ├─────────────────┤ │ │ │ 简单配置 │ │ 图形界面配置 │ │ │ │ 向后兼容 │ │ 用户级别 │ │ │ └─────────────────┘ └─────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘ 1. .cursorrules 文件 -------------------- 最简单的方式,在项目根目录创建 ``.cursorrules`` 文件: .. code-block:: text # .cursorrules You are an expert Python developer. ## Code Style - Use Python 3.11+ features - Follow PEP 8 style guide - Use type hints for all functions - Write docstrings in Google style ## Project Structure - Use src/ layout - Tests go in tests/ directory - Use pytest for testing ## Dependencies - Use Poetry for dependency management - Prefer standard library over third-party 2. .cursor/rules 目录 --------------------- 更灵活的方式,可以按文件类型或模块配置不同规则: .. code-block:: text .cursor/ └── rules/ ├── global.md # 全局规则 ├── python.md # Python 特定规则 ├── typescript.md # TypeScript 特定规则 └── testing.md # 测试相关规则 **global.md**: .. code-block:: markdown # Global Rules ## General Principles - Write clean, readable code - Prefer simplicity over cleverness - Always handle errors appropriately ## Documentation - Add comments for complex logic - Keep README up to date **python.md**: .. code-block:: markdown --- description: Rules for Python files globs: ["**/*.py"] --- # Python Rules ## Style - Use Black for formatting - Use isort for imports - Maximum line length: 88 characters ## Type Hints - All functions must have type hints - Use `from __future__ import annotations` ## Error Handling - Use specific exception types - Always log exceptions 3. Cursor Settings ------------------ 在 Settings → Rules 中配置用户级别的规则: 1. 打开 Settings(Cmd+,) 2. 搜索 "Rules" 3. 在文本框中输入规则 这些规则会应用到所有项目。 Rules 语法详解 ============== 基本结构 -------- Rules 使用 Markdown 格式,支持 YAML front matter: .. code-block:: markdown --- description: 规则描述 globs: ["**/*.py", "**/*.pyi"] alwaysApply: false --- # 规则标题 ## 分类 1 - 规则内容 ## 分类 2 - 规则内容 Front Matter 选项 ----------------- .. list-table:: :header-rows: 1 :widths: 20 20 60 * - 选项 - 类型 - 说明 * - description - string - 规则的简短描述 * - globs - string[] - 文件匹配模式 * - alwaysApply - boolean - 是否总是应用(默认 false) Globs 模式示例 -------------- .. code-block:: yaml # 所有 Python 文件 globs: ["**/*.py"] # 特定目录 globs: ["src/**/*.py"] # 多种文件类型 globs: ["**/*.ts", "**/*.tsx"] # 排除测试文件 globs: ["**/*.py", "!**/test_*.py"] 实用 Rules 示例 =============== Python 项目 Rules ----------------- .. code-block:: markdown --- description: Python project rules globs: ["**/*.py"] --- # Python Development Rules ## Environment - Python 3.11+ - Use Poetry for dependency management - Use pyproject.toml for configuration ## Code Style - Follow PEP 8 - Use Black formatter (line length 88) - Use isort for import sorting - Use Ruff for linting ## Type Hints - All functions must have type hints - Use `from __future__ import annotations` - Use `typing` module for complex types ## Documentation - Use Google-style docstrings - Include type info in docstrings - Add examples for complex functions ## Error Handling - Use specific exception types - Create custom exceptions when needed - Always log exceptions with context ## Testing - Use pytest - Test file naming: test_*.py - Aim for 80%+ coverage - Use fixtures for common setup ## Example Function ```python from __future__ import annotations def calculate_total( items: list[dict[str, float]], tax_rate: float = 0.1, ) -> float: """Calculate total price with tax. Args: items: List of items with 'price' key. tax_rate: Tax rate as decimal. Defaults to 0.1. Returns: Total price including tax. Raises: ValueError: If items list is empty. Example: >>> items = [{'price': 10.0}, {'price': 20.0}] >>> calculate_total(items) 33.0 """ if not items: raise ValueError("Items list cannot be empty") subtotal = sum(item['price'] for item in items) return subtotal * (1 + tax_rate) ``` TypeScript/React 项目 Rules ---------------------------- .. code-block:: markdown --- description: TypeScript React project rules globs: ["**/*.ts", "**/*.tsx"] --- # TypeScript React Rules ## Environment - TypeScript 5.x - React 18.x - Next.js 14.x (App Router) ## Code Style - Use ESLint + Prettier - Use functional components - Use hooks, no class components ## Naming Conventions - Components: PascalCase (UserProfile.tsx) - Hooks: camelCase with 'use' prefix (useAuth.ts) - Utils: camelCase (formatDate.ts) - Types: PascalCase with suffix (UserType, UserProps) ## Component Structure ```tsx // 1. Imports import { useState } from 'react'; // 2. Types interface Props { title: string; onSubmit: (data: FormData) => void; } // 3. Component export function MyComponent({ title, onSubmit }: Props) { // 3.1 Hooks const [state, setState] = useState(''); // 3.2 Handlers const handleClick = () => {}; // 3.3 Render return