feat(editor): 支持编辑器语法高亮和特定文件名 - #45
Conversation
- 将 `tempfile` 从开发依赖移至主依赖,支持运行时临时文件创建 - 添加 `edit_text_with_suffix()` 函数,允许指定文件扩展名以启用编辑器语法高亮 - 添加 `edit_text_with_filename()` 函数,支持使用特定文件名(如 `COMMIT_EDITMSG`)让编辑器识别文件类型 - 提取公共的编辑文本验证逻辑到 `validate_edited_text()` 函数 - 更新提交、配置、分割命令使用新的编辑函数,为 TOML 和提交消息提供正确的语法高亮
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough编辑器封装层新增带后缀和提交消息文件名的编辑接口。配置和 split 编辑使用 Changes编辑器文件类型提示
Estimated code review effort: 2 (Simple) | ~12 minutes Sequence Diagram(s)sequenceDiagram
participant Command
participant EditorUI
participant ExternalEditor
participant Validator
Command->>EditorUI: 请求编辑并传入文件类型信息
EditorUI->>ExternalEditor: 打开 `.toml` 或 `COMMIT_EDITMSG` 文件
ExternalEditor-->>EditorUI: 返回编辑后的内容
EditorUI->>Validator: 校验内容和空内容策略
Validator-->>Command: 返回编辑结果或取消状态
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces edit_text_with_suffix and edit_text_with_filename to allow system editors to infer correct syntax highlighting based on file extensions or specific filenames (e.g., .toml for configuration and COMMIT_EDITMSG for commits). Feedback suggests handling GcopError::UserCancelled gracefully in the config command to avoid hard exits, and robustly handling cases where external editors delete the temporary file by catching NotFound errors.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
|
||
| // Open the editor using the edit crate (automatic fallback: $VISUAL > $EDITOR > platform preset list) | ||
| let edited = edit::edit(&content)?; | ||
| let edited = ui::edit_text_with_suffix(&content, ".toml")?; |
There was a problem hiding this comment.
在之前的实现中,config edit 直接调用了 edit::edit,该方法在用户清空内容时不会触发取消逻辑。现在改用 ui::edit_text_with_suffix 后,如果用户清空并保存文件以取消编辑,该方法会返回 GcopError::UserCancelled。
目前代码中直接使用 ? 传播了该错误,这会导致 CLI 抛出硬错误并以非零状态码退出。建议在这里捕获 GcopError::UserCancelled,并优雅地输出提示信息(如 config.unchanged)然后正常退出(返回 Ok(())),从而提供更友好的用户体验。
let edited = match ui::edit_text_with_suffix(&content, ".toml") {
Ok(edited) => edited,
Err(GcopError::UserCancelled) => {
println!("{}", ui::info(&rust_i18n::t!("config.unchanged"), colored));
return Ok(());
}
Err(e) => return Err(e),
};There was a problem hiding this comment.
我给文件 NotFound 加上了 UserCancel 的静默错误回退,不过这样有可能会吞掉真正的文件 IO 错误,想问一下怎么样做比较好?
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2fe1654ff9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/ui/editor.rs`:
- Around line 40-43: The error message in the GcopError::InvalidInput call is
hardcoded as an English string, which violates the project's i18n convention.
Replace the hardcoded string "Editor filename must not contain path separators:
{}" with a t!("key") macro call using an appropriate localization key. Then add
the corresponding translation entries to both locales/en.yml and
locales/zh-CN.yml files to provide translations in both English and Chinese,
ensuring the placeholder for the filename is properly handled in both locale
files.
- Around line 38-39: Variable names in the editor.rs file are not following the
repository's Rust coding guidelines which require camelCase for variable names.
Rename the snake_case variables `file_name` to `fileName` and `temp_dir` to
`tempDir` throughout the file, including at lines 38-39 and lines 46-47 where
these variables are declared and used, to maintain consistency with the
established naming conventions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 84c41a2c-af11-440d-8caf-a887625a29f0
📒 Files selected for processing (5)
Cargo.tomlsrc/commands/commit.rssrc/commands/config.rssrc/commands/split.rssrc/ui/editor.rs
- 为 `edit_text_with_suffix` 添加 `reject_empty` 参数,允许调用点灵活控制是否拒绝空内容 - 配置编辑(config)使用 `false` 允许清空配置,分组编辑(split)使用 `true` 拒绝空内容 - 移除 `edit_text_with_filename` 中的路径分隔符验证,简化文件名处理逻辑 - 改进文件读取错误处理,当文件不存在时返回 `UserCancelled` 错误代替 IO 错误 - 移除不再使用的 `std::path::Path` 导入
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/ui/editor.rs`:
- Around line 40-53: The `filename` parameter in the `edit_text_with_filename`
function lacks validation to prevent path traversal attacks, which contradicts
the PR objective to explicitly reject filenames containing path separators. Add
validation at the start of the function to check if the filename contains path
separators (such as `/`, `\`, `..`, or absolute paths) and return an appropriate
error if found. Use the `t!()` macro for the error message to support
internationalization, then add the corresponding translation keys to both
`locales/en.yml` and `locales/zh-CN.yml` following the project's i18n
conventions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 503f9839-af9b-4da8-bb65-4525e4232416
📒 Files selected for processing (3)
src/commands/config.rssrc/commands/split.rssrc/ui/editor.rs
🚧 Files skipped from review as they are similar to previous changes (2)
- src/commands/split.rs
- src/commands/config.rs
- 将 `edit_text_with_filename` 重命名为 `edit_commit_message`,明确其专用于提交消息编辑的用途 - 引入 `COMMIT_EDITMSG_FILENAME` 常量,防止任意文件名导致的安全风险 - 将 `edit_text_with_suffix` 和 `edit_commit_message` 标记为 `pub(crate)`,收紧 API 可见性 - 新增 `validate_edited_text` 单元测试,覆盖空内容处理和用户取消场景 - 更新 CHANGELOG,记录编辑器文件类型检测功能改进
There was a problem hiding this comment.
Graphify reviewed this change.
Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).
Graphify review — findings
This PR adds editor filename/suffix handling so external editors can infer syntax highlighting. It introduces edit_text_with_suffix (which builds a temp file with a given suffix like .toml and an option to reject empty edits) and edit_commit_message (which writes to a COMMIT_EDITMSG-named temp file), refactoring the shared trimming/empty-check logic into a validate_edited_text helper with accompanying unit tests. Call sites are updated accordingly: the commit command uses edit_commit_message, while config edit and split-group edit use .toml-suffixed temp files, and tempfile is moved from dev-dependencies to regular dependencies with a changelog entry.
No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 207 functions depend on the 206 functions this change touches.
Health — grade A; 4 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):
compute_scope_info()— 3 callers, 4 callees (medium)run_with_deps()— 1 callers, 8 callees (medium)run_split_flow()— 1 callers, 8 callees (medium)main()— 0 callers, 13 callees (high)
Verification — 207 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 207 function(s) in the blast radius were not formally verified this run
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
变更内容
这个 pull request 改进了
gcop-rs调用外部编辑器时的临时文件命名方式,让编辑器可以根据文件后缀或特定文件名识别内容类型。具体改动包括以下内容。
edit_text_with_suffix(),用于在编辑临时文件时指定后缀,例如.toml。edit_text_with_filename(),用于在编辑临时文件时指定完整文件名,例如COMMIT_EDITMSG。validate_edited_text(),避免不同编辑入口重复实现同一段逻辑。config edit使用.toml后缀打开编辑器。.toml后缀打开编辑器。COMMIT_EDITMSG文件名打开编辑器。tempfile从开发依赖移动到运行时依赖,因为指定文件名的编辑流程需要在运行时创建临时目录和文件。行为变化
编辑配置和 split 分组时,编辑器现在更容易识别为 TOML 文件。
编辑提交消息时,编辑器现在更容易识别为 Git commit message。
如果用户清空编辑内容,仍然保持原有的取消逻辑。
测试
我验证了以下内容。
gcop config edit会使用带.toml后缀的临时文件打开编辑器。.toml后缀的临时文件打开编辑器。COMMIT_EDITMSG文件名打开编辑器。相关 Issue
Close #44
Summary by CodeRabbit
.toml文件后缀,提升编辑器中的语法高亮与编辑体验。COMMIT_EDITMSG文件名。