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: add code edits perferences #4155

Merged
merged 1 commit into from
Nov 7, 2024
Merged

Conversation

Ricbet
Copy link
Member

@Ricbet Ricbet commented Nov 7, 2024

Types

  • 🐛 Bug Fixes

Background or solution

Changelog

新增 code edits 相关配置项

Summary by CodeRabbit

  • 新功能

    • 添加了“代码编辑”设置部分,用户可以启用或禁用 Lint 错误和行变更的智能重写功能。
    • 新增本地化支持,提供中文和英文的设置标题及描述。
  • 文档

    • 更新了本地化文件以支持新设置的描述。

@Ricbet Ricbet requested review from hacke2 and erha19 November 7, 2024 02:47
@Ricbet Ricbet self-assigned this Nov 7, 2024
@opensumi opensumi bot added the 🎨 feature feature required label Nov 7, 2024
Copy link
Contributor

coderabbitai bot commented Nov 7, 2024

Walkthrough

此拉取请求引入了一系列与代码编辑相关的新设置和功能。主要更改包括在 AINativeBrowserContribution 类中注册新的设置部分“代码编辑”,其中包含两个本地化的首选项:CodeEditsLintErrorsCodeEditsLineChange。此外,多个文件中添加了对 PreferenceService 的依赖,以支持基于用户首选项的代码编辑操作。还更新了相关的本地化文件,以增强用户界面定制。

Changes

文件路径 更改摘要
packages/ai-native/src/browser/ai-core.contribution.ts 新增设置部分“代码编辑”,包含 CodeEditsLintErrorsCodeEditsLineChange 的注册。
packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts BaseCodeEditsSource 类中添加 protected preferenceService: PreferenceService; 属性。
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts 修改 doTrigger 方法,添加对行变更编辑启用状态的检查。
packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts 修改 doTrigger 方法,添加对 lint 错误启用状态的检查。
packages/ai-native/src/browser/preferences/schema.ts aiNativePreferenceSchema 中新增 CodeEditsLintErrorsCodeEditsLineChange 属性。
packages/core-common/src/settings/ai-native.ts AINativeSettingSectionsId 枚举中新增 CodeEditsLintErrorsCodeEditsLineChange 条目。
packages/i18n/src/common/en-US.lang.ts 新增与代码编辑相关的本地化条目。
packages/i18n/src/common/zh-CN.lang.ts 新增与代码编辑相关的本地化条目。

Possibly related PRs

Suggested labels

🎨 feature

Suggested reviewers

  • erha19
  • bytemain

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 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

🧹 Outside diff range and nitpick comments (6)
packages/core-common/src/settings/ai-native.ts (1)

19-24: 代码编辑设置的实现看起来不错,建议补充设置项的详细说明。

新增的代码编辑设置遵循了现有的命名规范和命名空间模式。不过,建议为这两个新的设置项添加详细的 JSDoc 注释,说明它们的具体用途和预期行为,就像 InlineDiffPreviewModeIntelligentCompletionsPromptEngineeringEnabled 那样。

建议添加如下注释:

  /**
   * Code edits settings
   */
+ /**
+  * 是否启用代码编辑时的 lint 错误检查
+  */
  CodeEditsLintErrors = 'ai.native.codeEdits.lintErrors',
+ /**
+  * 是否启用代码编辑时的行变更检查
+  */
  CodeEditsLineChange = 'ai.native.codeEdits.lineChange',
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (1)

33-35: 建议优化首选项检查的实现

代码有以下几点可以改进:

  1. 默认值硬编码为 false 可能不够灵活
  2. 条件判断可以更清晰
  3. 缺少错误处理机制

建议按照以下方式重构:

-    const isLineChangeEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLineChange, false);
-
-    if (!isLineChangeEnabled || !position) {
+    try {
+      const isLineChangeEnabled = this.preferenceService.getValid(
+        AINativeSettingSectionsId.CodeEditsLineChange,
+        this.defaultPreferences.lineChangeEnabled ?? false
+      );
+
+      if (!position) {
+        return;
+      }
+
+      if (!isLineChangeEnabled) {
+        return;
+      }

另外,建议在类级别定义默认配置:

private readonly defaultPreferences = {
  lineChangeEnabled: false,
} as const;
packages/ai-native/src/browser/preferences/schema.ts (2)

53-56: 建议添加首选项说明

这个新增的 CodeEditsLintErrors 首选项缺少描述信息。建议添加 description 字段来说明此功能的用途,以帮助用户理解该设置的作用。

建议按如下方式修改:

 [AINativeSettingSectionsId.CodeEditsLintErrors]: {
   type: 'boolean',
   default: false,
+  description: localize('preference.ai.native.codeEdits.lintErrors.description'),
 },

57-60: 建议添加首选项说明并考虑默认值

  1. 同样需要为 CodeEditsLineChange 添加描述信息
  2. 由于这两个功能都默认关闭,可能会影响功能的发现度。建议考虑是否将其默认开启,或在文档中明确说明这些新功能

建议按如下方式修改:

 [AINativeSettingSectionsId.CodeEditsLineChange]: {
   type: 'boolean',
   default: false,
+  description: localize('preference.ai.native.codeEdits.lineChange.description'),
 },
packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts (1)

94-96: 建议优化条件判断的顺序

当前实现中,即使 this.model 为空,也会先检查用户偏好设置。建议调整判断顺序以提高性能。

建议按照以下方式重构:

-    const isLintErrorsEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLintErrors, false);
-
-    if (!isLintErrorsEnabled || !this.model) {
+    if (!this.model) {
+      return;
+    }
+
+    const isLintErrorsEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLintErrors, false);
+    if (!isLintErrorsEnabled) {
      return;
    }

这样可以:

  1. 避免在 this.model 为空时不必要的偏好设置检查
  2. 提高代码的可读性,使逻辑更清晰
packages/ai-native/src/browser/ai-core.contribution.ts (1)

357-369: 建议完善代码编辑功能的架构设计

为了确保新增的设置项能够正确影响代码编辑功能的行为,建议:

  1. 在相关的代码编辑处理器中添加对这些设置的响应逻辑
  2. 考虑添加设置变更的事件监听,以便实时更新功能行为
  3. 为新增的设置项添加默认值配置
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 926108b and 0cdd8c2.

📒 Files selected for processing (8)
  • packages/ai-native/src/browser/ai-core.contribution.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts (2 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (2 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts (2 hunks)
  • packages/ai-native/src/browser/preferences/schema.ts (1 hunks)
  • packages/core-common/src/settings/ai-native.ts (1 hunks)
  • packages/i18n/src/common/en-US.lang.ts (1 hunks)
  • packages/i18n/src/common/zh-CN.lang.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/i18n/src/common/zh-CN.lang.ts
🔇 Additional comments (7)
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (2)

2-2: 导入声明看起来没有问题!

新增的 AINativeSettingSectionsId 导入正确地用于首选项检查。


33-33: 验证首选项配置的完整性

需要确保首选项键 CodeEditsLineChange 在系统中被正确定义和使用。

运行以下脚本来验证首选项的定义和使用:

✅ Verification successful

Let me gather more information about the preference definition and usage.


首选项配置已正确定义和使用

首选项键 CodeEditsLineChange 在系统中已完整配置:

  • packages/core-common/src/settings/ai-native.ts 中定义了键名 ai.native.codeEdits.lineChange
  • packages/ai-native/src/browser/preferences/schema.ts 中定义了布尔类型配置和默认值
  • packages/ai-native/src/browser/ai-core.contribution.ts 中注册了相应的本地化配置

当前代码中对该首选项的使用方式正确且符合预期。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查首选项键的定义
echo "检查 CodeEditsLineChange 的定义:"
rg -l "CodeEditsLineChange" --type ts

# 检查首选项的其他使用位置
echo "检查首选项的使用位置:"
ast-grep --pattern 'getValid($_, $AINativeSettingSectionsId.CodeEditsLineChange$_)'

Length of output: 590


Script:

#!/bin/bash
# 检查 CodeEditsLineChange 在 settings 定义文件中的具体定义
echo "在 settings 定义文件中的定义:"
rg "CodeEditsLineChange" packages/core-common/src/settings/ai-native.ts -A 3 -B 3

# 检查 schema 中的定义
echo "\n在 schema 文件中的定义:"
rg "CodeEditsLineChange" packages/ai-native/src/browser/preferences/schema.ts -A 3 -B 3

# 检查 contribution 中的使用
echo "\n在 contribution 文件中的使用:"
rg "CodeEditsLineChange" packages/ai-native/src/browser/ai-core.contribution.ts -A 3 -B 3

Length of output: 1085

packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts (1)

2-8: 导入声明看起来没有问题!

新增的 AINativeSettingSectionsId 导入与功能需求相符。

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

2-2: 导入语句正确添加!

PreferenceService 的导入位置恰当,且与新增的依赖注入属性相对应。


67-69: 验证派生类中的 PreferenceService 使用情况

PreferenceService 的注入实现正确。由于这是一个基类的改动,建议验证所有派生类是否正确使用了这个新增的服务。

✅ Verification successful

Based on the search results, I can now generate the final response since I have enough information to verify the usage of preferenceService in derived classes.

已验证 PreferenceService 在代码库中的使用情况

BaseCodeEditsSource 的派生类中没有直接使用 preferenceService 属性,但在基类中添加此属性是合理的,因为:

  • 代码库中大量使用了 PreferenceService 来管理配置项
  • 这个属性的添加为派生类提供了访问和管理配置的能力
  • 使用 protected 访问级别确保了派生类可以安全地访问此服务
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:查找所有继承自 BaseCodeEditsSource 的类,以验证它们是否正确使用了 preferenceService

# 搜索继承自 BaseCodeEditsSource 的类定义
ast-grep --pattern 'export class $_ extends BaseCodeEditsSource {
  $$$
}'

# 搜索这些类中是否使用了 preferenceService
rg "this\.preferenceService\." -A 3

Length of output: 66323

packages/ai-native/src/browser/ai-core.contribution.ts (1)

357-369: 代码编辑设置部分实现正确!

设置部分的实现逻辑完整,并且正确地放置在了AI原生设置组中。建议为这些新的设置项添加详细的说明文档,以帮助用户更好地理解它们的用途。

运行以下脚本验证本地化键是否存在:

✅ Verification successful

本地化键已正确实现!

所有新添加的本地化键都已在中英文语言文件中完整定义,包括:

  • 设置组标题 preference.ai.native.codeEdits.title
  • Lint Error 智能改写开关 preference.ai.native.codeEdits.lintErrors
  • Line Change 智能改写开关 preference.ai.native.codeEdits.lineChange
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:验证新添加的本地化键是否在语言文件中定义

# 测试:搜索本地化键。预期:在语言文件中找到对应的定义。
rg "preference\.ai\.native\.codeEdits\." packages/i18n/src/common/

Length of output: 781

packages/i18n/src/common/en-US.lang.ts (1)

1498-1501: 本地化字符串添加正确!

新增的代码编辑首选项本地化字符串结构清晰,命名规范,与现有模式保持一致。

@Ricbet
Copy link
Member Author

Ricbet commented Nov 7, 2024

/next

@opensumi
Copy link
Contributor

opensumi bot commented Nov 7, 2024

🎉 PR Next publish successful!

3.5.1-next-1730948411.0

Copy link

codecov bot commented Nov 7, 2024

Codecov Report

Attention: Patch coverage is 20.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 54.22%. Comparing base (3c0ecf3) to head (0cdd8c2).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...telligent-completions/source/line-change.source.ts 0.00% 2 Missing and 1 partial ⚠️
...ntelligent-completions/source/lint-error.source.ts 0.00% 2 Missing and 1 partial ⚠️
...ser/contrib/intelligent-completions/source/base.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4155   +/-   ##
=======================================
  Coverage   54.22%   54.22%           
=======================================
  Files        1598     1598           
  Lines       97652    97663   +11     
  Branches    19976    19984    +8     
=======================================
+ Hits        52954    52961    +7     
- Misses      37125    37129    +4     
  Partials     7573     7573           
Flag Coverage Δ
jsdom 49.79% <20.00%> (+<0.01%) ⬆️
node 15.60% <20.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.

@erha19 erha19 merged commit d7142f9 into main Nov 7, 2024
14 checks passed
@erha19 erha19 deleted the feat/support-code-edits-perferences branch November 7, 2024 07:49
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