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: improve line change code edits trigger rule #4373

Merged
merged 2 commits into from
Feb 17, 2025

Conversation

Ricbet
Copy link
Member

@Ricbet Ricbet commented Feb 14, 2025

Types

  • 🎉 New Features

Background or solution

规则一:未编辑过代码不触发,然后自上次编辑代码以来 60 秒内没有再次编辑过则不触发
规则二:当前文件弃用次数超过阈值(暂定 5 次)则不触发

Changelog

增加一些 line change 类型的 code edits 触发规则

Summary by CodeRabbit

  • 新功能

    • 编辑器智能补全功能现能更早启动,带来更及时的响应体验。
  • 重构

    • 优化了多行编辑及行变更的跟踪机制,提升状态更新和校验的精准性。
    • 改进了事件观察和数据处理逻辑,进一步增强了整体编辑交互的流畅性和稳定性。
    • 增强了可观察对象的功能,引入了新的观察机制以处理可观察值的变化。

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

coderabbitai bot commented Feb 14, 2025

Walkthrough

此次提交对编辑器内智能补全功能进行了更新。主要修改包括:调整智能补全控制器的注册时机(由编辑器渲染后延迟改为立即初始化)、增加新的常量用于处理视图显示的有效时长、以及重构智能补全控制器和行变化的处理逻辑,采用了基于 observable 的事件订阅方式,此外还更新了 observable 工具模块的部分函数签名和新增观察器函数。

Changes

文件 更改摘要
packages/ai-native/src/browser/ai-core.contribution.ts 修改 AINativeBrowserContribution 中注册智能补全控制器的时机,从 AfterFirstRender 改为 Eager,实现更早的初始化。
packages/ai-native/src/browser/contrib/intelligent-completions/... 添加常量 VALID_TIME=750;在 IntelligentCompletionsController 中引入新 observable 属性、重构 discardaccept 方法以及更新 reportData;在行变化源中新增多项 observable 属性及逻辑。
packages/monaco/src/common/observable.ts 更新 autorunDelta 函数签名(增加 reader 参数)并新增 onObservableChange 函数,以便实现对 observable 变化的监控。

Sequence Diagram(s)

sequenceDiagram
    participant E as 编辑器
    participant B as AINativeBrowserContribution
    participant IC as IntelligentCompletionsController
    participant O as Observable
    participant L as LineChangeSource

    E->>B: 启动编辑器
    B->>IC: Eager 时注册智能补全控制器
    IC->>O: 订阅多行编辑和内容变化事件
    E->>L: 触发行变化或内容更新
    L->>O: 发布更新事件
    O-->>IC: 通知状态变化
    IC->>IC: 调用 discard/accept 逻辑处理
Loading

Possibly related PRs

Suggested labels

🐞 bug

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.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c42110c and f6b1b6f.

📒 Files selected for processing (5)
  • packages/ai-native/src/browser/ai-core.contribution.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (7 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (3 hunks)
  • packages/monaco/src/common/observable.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/ai-native/src/browser/contrib/intelligent-completions/index.ts
  • packages/ai-native/src/browser/ai-core.contribution.ts
  • packages/monaco/src/common/observable.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: unittest (macos-latest, 18.x, node)
🔇 Additional comments (8)
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (4)

21-22: 常量定义清晰且合理!

常量定义明确且值设置合理:

  • DEPRECATED_LIMIT 限制了弃用次数为5次
  • CONTENT_CHANGE_VALID_TIME 设置了60秒的内容变更有效期

28-33: 存储实现简洁且文档完善!

deprecatedStore 的实现采用 Map 存储每个文件的弃用次数,并配有清晰的注释说明其用途和触发条件。


35-97: Observable 模式实现优雅!

代码通过 Observable 模式优雅地实现了状态管理:

  • 使用 positionChangeObs 监听光标位置变化
  • 使用 contentChangeObs 监听内容变化
  • 使用 latestContentChangeTimeObs 追踪最近的内容变更时间
  • 使用 isAllowTriggerObs 统一管理触发条件

触发规则逻辑清晰:

  1. 配置开关检查
  2. 弃用次数限制检查
  3. 编辑状态和时间检查

99-151: 事件处理实现完善!

mount 方法中的事件处理实现完善:

  • 正确处理 discard 事件,更新弃用计数
  • 正确处理 accept 事件,清理弃用记录
  • 使用 addDispose 确保资源正确释放
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (4)

116-123: 可见性状态追踪实现优雅!

使用 observableFromEvent 优雅地实现了多行编辑的可见性状态追踪:

  • 通过 contextKeyService 监听上下文变化
  • 使用 Event.filter 过滤出相关的状态变更
  • 实时反映 multiLineEditsIsVisible 的状态

353-384: 弃用逻辑实现完善!

使用 derivedHandleChanges 实现的弃用逻辑非常完善:

  • 追踪最后可见时间
  • 基于 VALID_TIME 判断是否有效
  • 正确处理状态报告
  • 清理相关资源

386-412: 采纳逻辑实现完善!

使用 derivedOpts 实现的采纳逻辑非常完善:

  • 正确处理状态报告
  • 处理多行装饰器的采纳
  • 正确处理虚拟编辑器的装饰映射
  • 清理相关资源

429-445: 光标位置变化处理实现合理!

光标位置变化的处理逻辑合理:

  • 监听光标位置变化
  • 检查位置是否超出范围
  • 使用 runWhenIdle 延迟处理弃用操作
✨ 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: 1

🧹 Nitpick comments (6)
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (3)

28-33: 存储弃用次数的映射可能存在生命周期管理隐患。

deprecatedStore 使用 Map<string, number> 来记录弃用计数,但若编辑器中打开了很多文件并未及时调用 acceptdiscard,该 Map 可能逐渐增大。建议在对应模型被关闭或销毁后及时清理,防止潜在的内存泄漏。


64-96: 对是否允许触发的计算逻辑建议支持可配置化。

目前 60 秒的阈值写死在 CONTENT_CHANGE_VALID_TIME 常量里。若日后需要动态调整,可考虑将其加入到用户可配置项以增强灵活度。不过整体逻辑保持简单也很直观,视项目需求酌情处理即可。


98-150: 弃用与接受逻辑令人关注未来交互和用户提示。

mount() 方法里对 discardaccept 分别更新 deprecatedStore,逻辑清晰。若在达到弃用上限后想提供用户提示或日志,可进一步在此处实现,以强化可观测与可维护性。

packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (1)

7-12: 使用硬编码 750 毫秒作为有效弃用时长可能需要场景化讨论。

数值虽然不大,但也可能不适用所有场景。除非本项目严格要求固定 750ms,否则可考虑提供配置或注释补充说明该数值的设计理由。

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

353-384: 优化了丢弃逻辑的实现

使用 derivedHandleChanges 重构了丢弃逻辑,增加了基于时间的有效性验证机制。建议:

  1. 考虑将 VALID_TIME 的值作为可配置项,方便根据不同场景调整超时时间。
  2. 添加日志记录,便于追踪丢弃操作的时间线。

386-412: 接受更改的实现逻辑完善

使用 derivedOpts 实现了更改接受的逻辑,并正确处理了虚拟编辑器的装饰器。建议添加错误处理:

 public accept = derivedOpts({ owner: this, equalsFn: () => false }, (reader) => {
+  try {
     const report = this.reportData.read(reader);
     report?.('isReceive');
 
     this.multiLineDecorationModel.accept();
 
     if (this.rewriteWidget) {
       this.rewriteWidget.accept();
       // ...
     }
 
     this.hide();
+  } catch (error) {
+    this.logger.error('Error accepting changes:', error);
+    this.hide();
+  }
 });
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 952f747 and c42110c.

📒 Files selected for processing (5)
  • packages/ai-native/src/browser/ai-core.contribution.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (7 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (3 hunks)
  • packages/monaco/src/common/observable.ts (3 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: ubuntu-latest, Node.js 20.x
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build-windows
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: build (macos-latest, 20.x)
🔇 Additional comments (10)
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (5)

3-10: 引入全局事件与可观测工具的导入看起来符合需求。

这些新增的导入与项目整体可观测模型的用法保持一致,便于监听光标与内容变动事件,功能结构清晰。


12-12: 智能补全控制器的引用。

此导入对后续逻辑有重要作用,用来对接 discard/accept 等事件,导入配置无问题。


21-22: 常量定义命名明确。

DEPRECATED_LIMITCONTENT_CHANGE_VALID_TIME 命名易懂,符合业务场景。可快速理解其用途。


35-46: 光标位置可观测对象的创建实现良好。

通过 observableFromEvent 监听光标位置变动逻辑简单可依赖。无明显问题。


47-62: 推导最新内容变动时间的派生逻辑清晰。

derivedHandleChanges 对变更时间进行记录,为后续判断是否触发有效编辑提供了基础,且符合常见可观测模式。

packages/monaco/src/common/observable.ts (2)

2-6: 新导入的接口和类型定义。

添加 IObservable, IObserver, IReader 等,让可观测机制更易理解,暂无可见问题。


73-91: 新增的 onObservableChange 函数实现符合可观测范式。

实现简单直观,可在多个观察者场景下灵活使用。功能上无明显问题,值得后续在更多场景推广。

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

1-34: 导入和常量声明看起来合理

新增的导入和常量声明增强了状态管理和上下文变更的处理能力。

Also applies to: 64-64


100-123: 使用 Observable 模式优化了可见性状态管理

通过 observableFromEvent 实现了对多行编辑可见性状态的响应式管理,提高了代码的可维护性。

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

262-264:

✅ Verification successful

更改了控制器的初始化时机

IntelligentCompletionsController 的实例化时机从 AfterFirstRender 改为 Eager,这样可以更早地初始化控制器。需要注意:

  1. 提前初始化可能会影响编辑器的启动性能
  2. 建议添加性能监控,确保这个改动不会显著影响编辑器的加载时间

🏁 Script executed:

#!/bin/bash
# 检查是否有其他组件也使用了 Eager 初始化
rg "EditorContributionInstantiation.Eager" -A 2 -B 2

Length of output: 1489


调整初始化时机:请监控性能影响

我们注意到 IntelligentCompletionsController 的实例化时机从 AfterFirstRender 调整为 Eager,这一变化使控制器能更早地初始化,并与 packages/monaco/src/browser/monaco.contribution.ts 中相似的实现保持一致。请注意以下几点:

  • 提前初始化可能会增加编辑器启动时的性能负担。
  • 建议在合并变更后添加性能监控,确保这一改动不会显著影响编辑器的加载速度。

Copy link

codecov bot commented Feb 14, 2025

Codecov Report

Attention: Patch coverage is 2.77778% with 105 lines in your changes missing coverage. Please review.

Project coverage is 54.09%. Comparing base (7d52295) to head (f6b1b6f).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...telligent-completions/source/line-change.source.ts 0.00% 38 Missing and 15 partials ⚠️
...-completions/intelligent-completions.controller.ts 0.00% 33 Missing and 7 partials ⚠️
packages/monaco/src/common/observable.ts 21.42% 11 Missing ⚠️
...c/browser/contrib/intelligent-completions/index.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4373      +/-   ##
==========================================
- Coverage   54.12%   54.09%   -0.04%     
==========================================
  Files        1639     1639              
  Lines      100362   100425      +63     
  Branches    21784    21789       +5     
==========================================
+ Hits        54320    54322       +2     
- Misses      38246    38295      +49     
- Partials     7796     7808      +12     
Flag Coverage Δ
jsdom 49.57% <2.77%> (-0.03%) ⬇️
node 12.26% <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.

hacke2
hacke2 previously approved these changes Feb 14, 2025
Copy link
Member

@hacke2 hacke2 left a comment

Choose a reason for hiding this comment

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

LGTM

@Ricbet Ricbet force-pushed the feat/improve-line-change-trigger branch from c42110c to f6b1b6f Compare February 17, 2025 05:30
Copy link
Member

@hacke2 hacke2 left a comment

Choose a reason for hiding this comment

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

LGTM

@hacke2 hacke2 merged commit 683127c into main Feb 17, 2025
12 checks passed
@hacke2 hacke2 deleted the feat/improve-line-change-trigger branch February 17, 2025 06:10
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