阿猫的博客

阿猫的博客

使用 Cursor 进行 Code Review(简易版)

2025-03-10

前言

周刊介绍了未来已来?-- 基于 cursor 的 ai code review这篇文章,马上仿照文章的方式给项目配了一个 mdc(之前的 .cursorrule),效果还不错,快马加鞭分享一下。

配置

首先是按 Cmd + Shift + P 呼出命令面板,输入 rule ,选择 File: New Cursor Rule,然后输入这次新加规则的名称 code-review,按回车。这时候就会生成一个新的文件。

生成之后应该是下面的样子,有两个空需要填。

复制粘贴一下内容,注意 description 和 globs 要填在空上。globs 根据项目需要改成对应的正则。

---
description: Review code changes.
globs: *.go
---
You are a experience software developer, and your job is to review my code changes and try to find bugs or other possible improvements.

You can run a git command to get the changes in the stage.

You should start by summarizing the changes and give some preliminary overviews. Then followed by the issues or improvements you find in the code changes. If everything is fine, you can leave the second part empty.

Note that:
- Do NOT comment on product or business decisions. Focus ONLY on technical issues.
- Do NOT comment on documentation and testing.
- Do NOT comment on issues that should be caught by static linters and compiler as they are already handled in CI.

Your output should look like:

<example>
# Overview

The changes include:

- xxx
- xxx

# Issues
## xxxx

```
// some code snippets
```

some comments
</example>

可以根据实际情况调整一下里面的内容,不过这个效果已经不错了。

使用

Cmd + L 呼出侧面板,切换到 COMPOSER 页签,模型选择 claude-3.7-sonnet-thinking(或者其他思考模型,例如 deepseek-r1),框中输入 Review changes. 然后回车即可。

中间可能会要求运行几个 git 命令,这是因为我希望它只去考虑最近变更的代码,需要手动点击运行。稍等片刻即可看到结果,看起来还不错,都是些很有意义的建议。

模型方面,可以试试不同的模型看看哪些更好,我只测试了 claude-3.7-sonnetclaude-3.7-sonnet-thinking ,其中 thinking 模型输出了更加有用的建议,而不是 nit(可改可不改的问题)。需要中文结果的话,可以把 Prompt 翻译成中文或者要求它用中文输出。

One more thing

目前的流程还需要手动允许 git 命令执行,基于 mcp 的方法可以让代码逻辑直接生成 diff,省去中间的操作。后续有时间再详细探索。