暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

技巧实战:如何用 Cursor 快速生成高质量技术文档

Coding 部落 2025-04-22
306

🧩 场景一:为函数/模块自动生成文档注释


✅ 示例:为 Go 语言函数生成专业英文注释

操作方式:

  1. 选中代码(或在代码上右键)

  2. 使用 Prompt(英文效果更好):

    Add a concise and professional English comment for this function, describing its purpose, parameters, and return value. Follow standard Go documentation style.


    示例输入:
      func CalculateTax(amount float64) float64 {
          return amount * 0.13
      }


      输出结果:
        // CalculateTax returns the tax amount for a given value,
        // based on a fixed tax rate of 13%.
        func CalculateTax(amount float64) float64 {
            return amount * 0.13
        }


        效果:简洁、专业、统一风格,适用于对接团队协作或开源项目。
        🧩 场景二:快速生成接口/模块使用文档
        ✅ 示例 Prompt(REST API Controller)
          Generate markdown-style documentation for this API handler, including endpoint, method, parameters, request/response examples, and error cases.


          可生成如下文档:
            ### POST api/v1/orders


            Create a new order.


            **Request Body:**
            ```json
            {
              "product_id": "string",
              "quantity"2
            }


            Response:
              {
                "order_id": "abc123",
                "status": "created"
              }


              Error Codes:

              • 400 Bad Request
                 – Invalid input

              • 500 Internal Server Error
                 – Server-side error


              效果:适合用于内部接口对接、OpenAPI 替代文档、前后端协作。

              🧩 场景三:自动生成项目 README

              进入项目根目录,使用如下 Prompt:

                Write a professional README.md for this project. Include a short description, how to install, how to run, technologies used, and contribution guide.
                效果:让 AI 自动总结目录结构、依赖和启动方式,适用于开源项目初始化。
                🧩 场景四:自动生成代码结构说明(给新同事看的文档)
                例如,你可以这样 prompt:
                  Explain the structure and purpose of this codebase. Include major components, how they interact, and which parts are responsible for business logic, data access, and configuration.


                  或者:
                    Act as a technical writer. Summarize the codebase for onboarding new engineers, focusing on design patterns, key flows, and module responsibilities.


                    效果:非常适合交接项目时写 Wiki 文档、团队内的知识传递。
                    📦 推荐 Prompt 模板合集(可收藏复用)
                    名称
                    Prompt 内容
                    英文函数注释
                    Write a concise comment describing what this function does, including parameters and return values.
                    接口文档
                    Generate markdown documentation for this REST API controller.
                    模块说明
                    Explain the role of this module in the application. Include how it interacts with other components.
                    项目概述
                    Write a README for this project, including usage and setup.
                    架构图说明
                    Generate a high-level system overview based on the code structure.

                    🔧 提示:如何提升质量?


                    • 上下文越完整,结果越准确:不要只让它看函数,可以从模块/多个文件选中。

                    • 尽量用英文 Prompt,Cursor 的 AI 更能理解写文档的意图。

                    • 可以后续追加修改 Prompt,比如:

                      "Now rewrite it in Chinese" 或
                      "Make it more concise and suitable for a beginner reader."


                    文章转载自Coding 部落,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                    评论