掌握光标规则:智能 AI 集成开发者指南

2025-06-08

掌握光标规则:智能 AI 集成开发者指南

什么是游标规则?

游标规则是定义 AI 如何与代码库交互的配置文件。它们通过为 AI 交互设置准则、约束和行为模式,提供情境感知的帮助。

三种类型的游标规则

1.全局AI规则(设置)

这些规则位于 Cursor 中Settings -> General,为所有 AI 交互建立了核心原则。它们定义了基本的行为模式,并且与语言无关。以下是一个有力的例子:

<CORE_PRINCIPLES>
1. EXPLORATION OVER CONCLUSION
- Never rush to conclusions
- Keep exploring until a solution emerges naturally
- Question every assumption and inference

2. DEPTH OF REASONING
- Break down complex thoughts into simple steps
- Embrace uncertainty and revision
- Express thoughts in natural conversation

3. THINKING PROCESS
- Show work-in-progress thinking
- Acknowledge and explore alternatives
- Frequently reassess and revise
</CORE_PRINCIPLES>

<OUTPUT_FORMAT>
Responses must follow:
  <CONTEMPLATOR>
  - Begin with foundational observations
  - Question thoroughly
  - Show natural progression
  </CONTEMPLATOR>

  <FINAL_ANSWER>
  - Clear, concise summary
  - Note remaining questions
  </FINAL_ANSWER>
</OUTPUT_FORMAT>
Enter fullscreen mode Exit fullscreen mode

这种结构将人工智能从一个简单的答案生成器转变为一个深思熟虑的合作者,可以彻底探索解决方案并展示其推理过程。

示例:具有推理能力的人工智能的高级规则

2. 项目范围规则(.cursorrules)

项目根目录中的单个文件作为项目特定约定的主要指南:

# Framework Standards
- Follow Rails architectural patterns
- Use service objects for business logic
- Reference patterns (@service-objects.md)

# Quality Controls
- Follow RuboCop guidelines
- Enforce test coverage minimums
- Ban SQL queries in views
Enter fullscreen mode Exit fullscreen mode

3. 模式特定规则(.cursor/rules/*.mdc)

这些基于 Markdown 的域配置文件在 Cursor v0.45 中引入,针对特定的文件模式:

---
Description: Rails Controller Standards
Globs: app/controllers/**/*.rb
---

# Guidelines
- Keep controllers skinny
- Use before_action for repeating logic
- Follow RESTful conventions
Enter fullscreen mode Exit fullscreen mode

注意:在当前版本中,这些规则仅在代理模式下添加。聊天和普通撰写仍使用.cursorrules

代理方法:游戏规则改变者

现代游标规则已从被动规则列举转变为主动代理指令。例如:

You are instructa, a senior Rails developer with superpowers! ⚡

# Agent Behavior
- Read Roadmap.md first
- Plan database schema changes
- Use ViewComponents for complex UI
- Write system tests for critical paths

# Code Standards
- Follow Rails conventions
- Use concerns for shared logic
- Tests must pass before merge
Enter fullscreen mode Exit fullscreen mode

专业提示

1. 使用“@语法”的参考架构

无需写冗长的解释,只需参考您的文档:

# Bad
- Controllers should use service objects for complex business logic...

# Good
- Follow service object patterns defined in @docs/architecture/services.md
- See implementation examples in @docs/examples/service_objects/
Enter fullscreen mode Exit fullscreen mode

2. 战略全局模式

创建有重点的、分层的模式:

# Too broad
Globs: **/*.rb

# Better
Globs:
  app/services/**/*.rb
  app/models/**/*.rb
  !app/models/legacy/**/*.rb  # Exclude legacy
Enter fullscreen mode Exit fullscreen mode

3. 合并规则

创建可组合规则集:

# .cursor/rules/base_ruby.mdc
Description: Base Ruby standards

# .cursor/rules/rails_controllers.mdc
@base_ruby.mdc
Description: Controller-specific rules
Globs: app/controllers/**/*.rb
Enter fullscreen mode Exit fullscreen mode

4.可维护的组织

按域划分的结构规则:

.cursor/rules/
  ├── rails8.mdc
  ├── models/
  │   ├── active_record.mdc
  │   └── postgresql.mdc
  ├── controllers/
  │   ├── api.mdc
  │   └── web.mdc
  └── views/
      ├── erb.mdc
      └── components.mdc
Enter fullscreen mode Exit fullscreen mode

游标规则的强大之处在于它能够在保持灵活性的同时提供上下文指导。它将 AI 从通用工具转变为能够感知项目的编码伙伴,能够理解您的架构、约定和目标。

鏂囩珷鏉ユ簮锛�https://dev.to/dpaluy/mastering-cursor-rules-a-developers-guide-to-smart-ai-integration-1k65
PREV
FastAPI 生产设置指南 🏁⚡️🚀 GenAI LIVE!| 2025 年 6 月 4 日
NEXT
新开发者如何找到开源项目?绝佳的首个 PR 机会