Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support code edits keybinding #4362

Merged
merged 3 commits into from
Feb 12, 2025
Merged

Conversation

Ricbet
Copy link
Member

@Ricbet Ricbet commented Feb 12, 2025

Types

  • 🎉 New Features

Background or solution

默认 alt + \, 可配置调整

Changelog

支持快捷键触发 code edits

Summary by CodeRabbit

  • 新功能
    • 优化了智能代码编辑体验,更新了编辑操作指令(包括触发、接受和放弃)以提升操作流畅性。
    • 增加了可配置的按键绑定选项,允许更灵活地管理代码编辑命令。
    • 引入实时触发机制,使代码编辑交互响应更迅速。
    • 新增代码编辑配置接口,支持自定义触发快捷键。

@Ricbet Ricbet requested review from hacke2 and erha19 February 12, 2025 08:03
@Ricbet Ricbet self-assigned this Feb 12, 2025
@opensumi opensumi bot added the 🎨 feature feature required label Feb 12, 2025
Copy link
Contributor

coderabbitai bot commented Feb 12, 2025

Walkthrough

本次 PR 主要对代码编辑功能进行了改进。更新集中在智能补全相关模块,替换了旧的多行补全命令为新的代码编辑命令,并增加了对应的按键绑定。在贡献模块中引入了 AINativeConfigService 来管理配置,同时在控制器中添加了新的可观察信号和 trigger 方法以响应代码编辑事务。此外,还在配置服务和类型定义中加入了代码编辑相关的配置支持。

Changes

文件 更改摘要
packages/ai-native/.../intelligent-completions.contribution.ts
packages/ai-native/.../intelligent-completions.controller.ts
在这两个文件中整合代码编辑支持。贡献模块引入 AINativeConfigService,替换了多行补全命令为代码编辑命令,新增 AI_CODE_EDITS_COMMANDS.TRIGGER 命令及相关按键绑定;控制器添加了可观察信号和 trigger 方法以处理代码编辑事务。
packages/core-browser/.../ai-config.service.ts
packages/core-common/.../index.ts
添加代码编辑配置支持。定义了 DEFAULT_CODE_EDITS_CONFIG 常量、internalCodeEdits 属性及 codeEdits getter,并引入了新的接口 IAINativeCodeEditsConfig,扩展了 IAINativeConfig 接口。
packages/core-browser/.../command.ts 更新命令常量:移除 AI_MULTI_LINE_COMPLETION_* 常量,新增 AI_CODE_EDITS_COMMANDS.ACCEPTAI_CODE_EDITS_COMMANDS.DISCARDAI_CODE_EDITS_COMMANDS.TRIGGER 常量,并调整了命令 ID 的命名。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant CC as IntelligentCompletionsContribution
    participant IC as IntelligentCompletionsController
    participant OS as 可观察信号

    U->>CC: 触发 AI_CODE_EDITS_COMMANDS.TRIGGER 命令/按键
    CC->>IC: 调用 trigger(tx) 方法
    IC->>OS: 触发 codeEditsTriggerSignal (tx)
Loading

Possibly related PRs

Suggested labels

⚙️ refactor

Suggested reviewers

  • hacke2
  • erha19
  • bytemain

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/config-array@0.13.0: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > glob@7.2.3: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (1)

50-59: 建议增加错误处理机制

在执行 trigger 命令时,建议添加错误处理:

  1. 捕获并处理可能的异常
  2. 添加适当的错误提示
 commands.registerCommand(AI_CODE_EDITS_TRIGGER, {
   execute: () => {
     const editor = this.workbenchEditorService.currentCodeEditor;
     if (editor) {
+      try {
         transaction((tx) => {
           IntelligentCompletionsController.get(editor.monacoEditor)?.trigger(tx);
         });
+      } catch (error) {
+        // 处理错误,例如显示通知
+        console.error('触发代码编辑失败:', error);
+      }
     }
   },
 });
packages/core-common/src/types/ai-native/index.ts (1)

84-89: 建议增强类型约束

为了提高类型安全性,建议为 triggerKeybinding 添加更具体的类型定义:

 export interface IAINativeCodeEditsConfig {
   /**
    * 触发 code edits 的快捷键
    */
-  triggerKeybinding?: string;
+  triggerKeybinding?: `${string}+${string}` | string;
 }
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (1)

401-403: 建议为 trigger 方法添加 JSDoc 注释

为了提高代码的可维护性和可读性,建议添加方法的用途说明、参数说明等文档。

+/**
+ * 触发代码编辑信号
+ * @param tx 事务对象,用于批量处理状态更新
+ */
 public trigger(tx: ITransaction): void {
   this.codeEditsTriggerSignal.trigger(tx);
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81c0f99 and 102451f.

📒 Files selected for processing (5)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (3 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (4 hunks)
  • packages/core-browser/src/ai-native/ai-config.service.ts (4 hunks)
  • packages/core-browser/src/ai-native/command.ts (1 hunks)
  • packages/core-common/src/types/ai-native/index.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: build-windows
  • GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (7)
packages/core-browser/src/ai-native/command.ts (1)

33-43: 代码结构清晰,命名规范!

新增的常量定义遵循了现有的命名模式,并且清晰地表达了其用途。

packages/core-browser/src/ai-native/ai-config.service.ts (2)

78-86: 实现优雅,逻辑清晰!

codeEdits getter的实现遵循了类中其他getter的模式,正确处理了配置合并逻辑。


36-38: 请验证快捷键绑定的兼容性

默认使用 alt+\ 作为触发快捷键,建议验证:

  1. 是否与其他常用IDE或编辑器的快捷键冲突
  2. 在不同操作系统和键盘布局下的可用性
✅ Verification successful

🌐 Web query:

在主流IDE(VS Code, IntelliJ)中是否有使用 alt+\ 作为默认快捷键的功能?

💡 Result:

在主流IDE中,Visual Studio Code 和 IntelliJ IDEA 均未将 Alt + \ 设置为默认快捷键。以下是具体分析:

Visual Studio Code

  • 默认布局操作:切换编辑器布局的默认快捷键为 Ctrl + \(Windows/Linux)或 ⌘\(Mac)[1][3][4]。
  • 多光标编辑:相关功能使用 Alt + ClickCtrl + Alt + ↓/↑ [1]。

IntelliJ IDEA

  • 布局调整:切换编辑器布局的默认快捷键为 Ctrl + Shift + F12 [1][3]。
  • 分屏操作:分屏编辑使用 Ctrl + Alt + Shift + 左/右方向键 [1]。

其他相关功能对比

功能 VS Code 快捷键 IntelliJ IDEA 快捷键
切换侧边栏 Ctrl + B Alt + 1
打开终端 Ctrl + ` Alt + F12
多光标选择 Alt + Click Alt + J(选中相同内容)

如果需要使用 Alt + \,可通过 自定义快捷键设置 实现(如修改键盘映射或安装插件)[9]。

Citations:


快捷键绑定兼容性验证

经过查询,主流IDE(VS Code 和 IntelliJ IDEA)均未将 Alt + \ 设置为默认快捷键,因此这项配置不会与它们默认的快捷键产生冲突。请继续采用当前配置;如发现特殊操作系统或键盘布局下存在问题,可考虑额外调整。

  • 文件:packages/core-browser/src/ai-native/ai-config.service.ts(行 36-38)
  • 默认 triggerKeybinding 设置为 alt+\ 与 VS Code 和 IntelliJ IDEA 的默认快捷键无冲突
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (1)

81-84: 快捷键注册实现完善!

使用配置服务获取触发快捷键,并正确注册了按键绑定。

packages/core-common/src/types/ai-native/index.ts (1)

101-104: 文档完善,接口清晰!

新增的配置项有完整的JSDoc注释,接口设计符合最佳实践。

packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (2)

91-91: 信号属性的类型和初始化看起来不错!

codeEditsTriggerSignal 的类型定义和初始化实现都很规范。

Also applies to: 97-97


444-456: handleChange 中的信号处理逻辑需要验证

请确保在触发信号时不会与其他上下文变更产生冲突。建议添加测试用例验证这种情况。

❌ Verification inconclusive

🏁 Script executed:

#!/bin/bash
# 搜索相关的测试文件
fd -g "*intelligent-completions.controller.test.ts" -x cat {} \;

# 搜索信号处理相关的测试用例
rg -A 5 "codeEditsTriggerSignal" 

Length of output: 4442


信号处理逻辑验证反馈

目前在代码中通过对比 codeEditsSourceCollection.codeEditsContextBeancodeEditsTriggerSignal 的变化来决定是否触发补全请求,逻辑上基本明确分离了两种触发情形。但在仓库中未发现针对这一逻辑的专门测试用例,无法完全确认在两信号同时发生或边缘情况时不会相互冲突。建议:

  • 添加单元测试验证:
    • 仅触发 codeEditsTriggerSignal 时返回 true
    • 仅触发 codeEditsSourceCollection.codeEditsContextBean 时根据 aiNativeContextKey.multiLineEditsIsVisible 的值返回相应逻辑;
    • 验证若两者同时发生时是否按预期处理(例如优先处理哪一信号,是否存在竞态或冲突情况)。

这种补充测试将有助于确保未来代码维护时行为的一致性和正确性。

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (2)

31-60: 建议增加错误处理机制

命令注册的实现逻辑清晰,但建议考虑以下改进:

  1. 在执行命令时增加 try-catch 块以处理潜在异常
  2. IntelligentCompletionsController.get() 返回 null 时提供适当的错误提示

建议参考以下改进方案:

 commands.registerCommand(AI_CODE_EDITS_TRIGGER, {
   execute: () => {
     const editor = this.workbenchEditorService.currentCodeEditor;
     if (editor) {
+      try {
+        const controller = IntelligentCompletionsController.get(editor.monacoEditor);
+        if (!controller) {
+          console.warn('IntelligentCompletionsController not found');
+          return;
+        }
         transaction((tx) => {
-          IntelligentCompletionsController.get(editor.monacoEditor)?.trigger(tx);
+          controller.trigger(tx);
         });
+      } catch (error) {
+        console.error('Failed to execute code edits trigger:', error);
+      }
     }
   },
 });

62-86: 建议添加配置说明文档

快捷键绑定的实现合理,特别是使用了配置服务来支持自定义触发键。建议添加以下改进:

  1. 为配置项添加注释,说明默认快捷键和自定义方式
  2. 考虑添加快捷键冲突检测机制

建议在代码前添加如下注释:

/**
 * 注册代码编辑相关的快捷键绑定
 * 
 * 支持的快捷键:
 * - 触发代码编辑:默认为 alt + \,可通过配置自定义
 * - 接受编辑:Tab 键
 * - 取消编辑:Esc 键
 */
registerKeybindings(keybindings: KeybindingRegistry): void {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 102451f and 0e26bbd.

📒 Files selected for processing (1)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: build-windows
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build (macos-latest, 20.x)
🔇 Additional comments (1)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (1)

1-20: 依赖注入和导入声明看起来结构合理!

代码遵循了依赖注入的最佳实践,通过 @Autowired 装饰器注入 AINativeConfigService

Also applies to: 28-29

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 11.11111% with 16 lines in your changes missing coverage. Please review.

Project coverage is 54.17%. Comparing base (81c0f99) to head (a6a755e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...-completions/intelligent-completions.controller.ts 0.00% 6 Missing and 1 partial ⚠️
...es/core-browser/src/ai-native/ai-config.service.ts 28.57% 4 Missing and 1 partial ⚠️
packages/core-browser/src/ai-native/command.ts 0.00% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4362      +/-   ##
==========================================
- Coverage   54.19%   54.17%   -0.02%     
==========================================
  Files        1639     1639              
  Lines      100278   100293      +15     
  Branches    21764    21759       -5     
==========================================
- Hits        54341    54337       -4     
- Misses      38173    38188      +15     
- Partials     7764     7768       +4     
Flag Coverage Δ
jsdom 49.65% <11.11%> (-0.02%) ⬇️
node 12.28% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (1)

46-55: 建议优化错误处理

新增的 trigger 命令实现中,建议添加错误处理逻辑以提高代码的健壮性。

 commands.registerCommand(AI_CODE_EDITS_COMMANDS.TRIGGER, {
   execute: () => {
     const editor = this.workbenchEditorService.currentCodeEditor;
     if (editor) {
+      try {
         transaction((tx) => {
           IntelligentCompletionsController.get(editor.monacoEditor)?.trigger(tx);
         });
+      } catch (error) {
+        console.error('Failed to trigger code edits:', error);
+      }
     }
   },
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0e26bbd and a6a755e.

📒 Files selected for processing (2)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (3 hunks)
  • packages/core-browser/src/ai-native/command.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: build-windows
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (2)
packages/core-browser/src/ai-native/command.ts (1)

33-45: 代码结构优化:命令统一管理

将相关命令统一放在 AI_CODE_EDITS_COMMANDS 命名空间下管理是个很好的改进:

  • 提高了代码的组织性和可维护性
  • 命令 ID 遵循一致的命名规范
  • 新增的 TRIGGER 命令支持了代码编辑的快捷键功能
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (1)

3-3: 配置服务集成合理

通过依赖注入引入 AINativeConfigService 来管理配置是个不错的设计选择,使得代码编辑功能的配置更加灵活。

Also applies to: 24-25

@Ricbet Ricbet merged commit 075e156 into main Feb 12, 2025
10 checks passed
@Ricbet Ricbet deleted the feat/support-code-edits-keybinding branch February 12, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎨 feature feature required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants