Skip to content

feat(editor): 支持编辑器语法高亮和特定文件名 - #45

Merged
AptS-1547 merged 4 commits into
AptS-1547:masterfrom
YewFence:feat/file-format-support
Aug 13, 2026
Merged

AptS-1547 merged 4 commits into
AptS-1547:masterfrom
YewFence:feat/file-format-support

Conversation

@YewFence

@YewFence YewFence commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

变更内容

这个 pull request 改进了 gcop-rs 调用外部编辑器时的临时文件命名方式,让编辑器可以根据文件后缀或特定文件名识别内容类型。

具体改动包括以下内容。

  1. 新增 edit_text_with_suffix(),用于在编辑临时文件时指定后缀,例如 .toml。
  2. 新增 edit_text_with_filename(),用于在编辑临时文件时指定完整文件名,例如 COMMIT_EDITMSG。
  3. 将编辑结果的空内容校验提取到 validate_edited_text(),避免不同编辑入口重复实现同一段逻辑。
  4. 让 config edit 使用 .toml 后缀打开编辑器。
  5. 让 split 分组编辑使用 .toml 后缀打开编辑器。
  6. 让提交消息编辑使用 COMMIT_EDITMSG 文件名打开编辑器。
  7. 将 tempfile 从开发依赖移动到运行时依赖,因为指定文件名的编辑流程需要在运行时创建临时目录和文件。

行为变化

编辑配置和 split 分组时,编辑器现在更容易识别为 TOML 文件。

编辑提交消息时,编辑器现在更容易识别为 Git commit message。

如果用户清空编辑内容,仍然保持原有的取消逻辑。

测试

我验证了以下内容。

  1. gcop config edit 会使用带 .toml 后缀的临时文件打开编辑器。
  2. split 分组编辑会使用带 .toml 后缀的临时文件打开编辑器。
  3. 提交消息编辑会使用 COMMIT_EDITMSG 文件名打开编辑器。
  4. 原有的空内容校验逻辑仍然保留。

相关 Issue

Close #44

Summary by CodeRabbit

  • 改进
    • 配置和分组内容编辑时使用 .toml 文件后缀,提升编辑器中的语法高亮与编辑体验。
    • 提交信息编辑时使用标准的 COMMIT_EDITMSG 文件名。
    • 优化空白内容处理,避免在不适用的场景下意外提交空内容。
  • 文档
    • 更新变更日志,说明编辑器文件类型与命名规则。

- 将 `tempfile` 从开发依赖移至主依赖,支持运行时临时文件创建
- 添加 `edit_text_with_suffix()` 函数,允许指定文件扩展名以启用编辑器语法高亮
- 添加 `edit_text_with_filename()` 函数,支持使用特定文件名(如 `COMMIT_EDITMSG`)让编辑器识别文件类型
- 提取公共的编辑文本验证逻辑到 `validate_edited_text()` 函数
- 更新提交、配置、分割命令使用新的编辑函数,为 TOML 和提交消息提供正确的语法高亮
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c281056d-8024-43ab-9606-3cc01558d00d

📥 Commits

Reviewing files that changed from the base of the PR and between 1c14fa5 and 276e681.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/commands/commit.rs
  • src/ui/editor.rs

📝 Walkthrough

Walkthrough

编辑器封装层新增带后缀和提交消息文件名的编辑接口。配置和 split 编辑使用 .toml 临时文件。提交消息编辑使用 COMMIT_EDITMSG 文件名。验证逻辑支持按调用场景决定是否拒绝空内容。

Changes

编辑器文件类型提示

Layer / File(s) Summary
运行时依赖提升
Cargo.toml
tempfile = "3.27" 从 [dev-dependencies] 移动到 [dependencies]。
编辑器接口与验证逻辑
src/ui/editor.rs
edit_text 委托给 edit_text_with_suffix。新增 edit_commit_message,使用固定文件名 COMMIT_EDITMSG。验证函数新增 reject_empty 参数,并增加空内容测试。
命令调用点更新
src/commands/commit.rs, src/commands/config.rs, src/commands/split.rs, CHANGELOG.md
commit 使用 edit_commit_message;config 和 split 使用 .toml 后缀及不同的空内容策略;变更记录同步更新。

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: 返回编辑结果或取消状态
Loading

Suggested reviewers: apts-1547

Poem

.toml 后缀落入临时文件,
COMMIT_EDITMSG 让提交消息归位。
编辑器终于识别内容,
空白校验各按规则执行,
猫猫,这次文件名没再迷路。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确概括了主要改动:为编辑器添加语法高亮和特定文件名支持,涵盖了PR的核心目标。
Linked Issues check ✅ Passed 所有代码变更完整满足 #44 的需求:config/split 使用 .toml 后缀,commit 使用 COMMIT_EDITMSG 文件名,tempfile 迁至运行时依赖。
Out of Scope Changes check ✅ Passed 所有修改都围绕编辑器临时文件命名展开,不存在无关的变更,范围清晰且可控。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/commands/config.rs Outdated

// 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")?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在之前的实现中,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),
        };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我给文件 NotFound 加上了 UserCancel 的静默错误回退,不过这样有可能会吞掉真正的文件 IO 错误,想问一下怎么样做比较好?

Comment thread src/ui/editor.rs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/commands/config.rs Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1b7d108 and 2fe1654.

📒 Files selected for processing (5)
  • Cargo.toml
  • src/commands/commit.rs
  • src/commands/config.rs
  • src/commands/split.rs
  • src/ui/editor.rs

Comment thread src/ui/editor.rs Outdated
Comment thread src/ui/editor.rs Outdated
- 为 `edit_text_with_suffix` 添加 `reject_empty` 参数,允许调用点灵活控制是否拒绝空内容
- 配置编辑(config)使用 `false` 允许清空配置,分组编辑(split)使用 `true` 拒绝空内容
- 移除 `edit_text_with_filename` 中的路径分隔符验证,简化文件名处理逻辑
- 改进文件读取错误处理,当文件不存在时返回 `UserCancelled` 错误代替 IO 错误
- 移除不再使用的 `std::path::Path` 导入

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2fe1654 and 1c14fa5.

📒 Files selected for processing (3)
  • src/commands/config.rs
  • src/commands/split.rs
  • src/ui/editor.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/commands/split.rs
  • src/commands/config.rs

Comment thread src/ui/editor.rs Outdated
@AptS-1547 AptS-1547 added enhancement New feature or request rust Pull requests that update rust code labels Aug 13, 2026
- 将 `edit_text_with_filename` 重命名为 `edit_commit_message`,明确其专用于提交消息编辑的用途
- 引入 `COMMIT_EDITMSG_FILENAME` 常量,防止任意文件名导致的安全风险
- 将 `edit_text_with_suffix` 和 `edit_commit_message` 标记为 `pub(crate)`,收紧 API 可见性
- 新增 `validate_edited_text` 单元测试,覆盖空内容处理和用户取消场景
- 更新 CHANGELOG,记录编辑器文件类型检测功能改进
@AptS-1547
AptS-1547 merged commit 338b394 into AptS-1547:master Aug 13, 2026
8 of 10 checks passed

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 27.02703% with 27 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/ui/editor.rs 29.41% 24 Missing ⚠️
src/commands/commit.rs 0.00% 1 Missing ⚠️
src/commands/config.rs 0.00% 1 Missing ⚠️
src/commands/split.rs 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request rust Pull requests that update rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

编辑配置文件和提交消息时,打开的临时文件缺少文件类型提示,导致编辑器语法高亮不可用

2 participants