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

WIP: support inline completion highlight #4166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Ricbet
Copy link
Member

@Ricbet Ricbet commented Nov 12, 2024

Types

  • 🎉 New Features

Background or solution

hightlight_inline_completion.mp4
  • 内联补全高亮
  • 配置开关
  • 修复 column 变化时的高亮 token 错位问题

Changelog

内联补全支持代码高亮

Summary by CodeRabbit

  • 新功能

    • 添加了智能补全高亮设置,用户可以自定义是否启用此功能。
    • 引入了 GhostTextTokenizationAdditionalLinesWidget 类,增强了代码编辑器中的内联补全功能。
  • 样式

    • 新增 CSS 类 .inline_completion_ghost_text_decoration,用于样式化编辑器中的幽灵文本。
  • 本地化

    • 添加了英语和中文的本地化条目,以支持智能补全高亮设置的语言选项。

@Ricbet Ricbet requested review from hacke2 and erha19 November 12, 2024 11:56
@Ricbet Ricbet self-assigned this Nov 12, 2024
@Ricbet Ricbet linked an issue Nov 12, 2024 that may be closed by this pull request
Copy link

codecov bot commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 0.42735% with 233 lines in your changes missing coverage. Please review.

Project coverage is 54.00%. Comparing base (bb5e972) to head (1930886).

Files with missing lines Patch % Lines
...s/decoration/ghost-text-tokenization.decoration.ts 0.00% 160 Missing and 30 partials ⚠️
...-completions/intelligent-completions.controller.ts 0.00% 36 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4166      +/-   ##
==========================================
- Coverage   54.12%   54.00%   -0.13%     
==========================================
  Files        1598     1599       +1     
  Lines       97740    97960     +220     
  Branches    20001    20058      +57     
==========================================
+ Hits        52901    52902       +1     
- Misses      37244    37428     +184     
- Partials     7595     7630      +35     
Flag Coverage Δ
jsdom 49.57% <0.42%> (-0.12%) ⬇️
node 15.54% <0.42%> (-0.04%) ⬇️

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.

@Ricbet Ricbet changed the title WIP: support inline completion highlight support inline completion highlight Nov 14, 2024
@Ricbet Ricbet force-pushed the feat/support-inline-completion-hightlight branch from 6deed40 to 1930886 Compare November 14, 2024 02:53
Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

Walkthrough

该拉取请求引入了多个与智能补全功能相关的更改。主要新增了一个设置选项,用于高亮显示智能补全。此外,新增了两个类以管理编辑器中的“幽灵文本”功能,并更新了现有的接口和样式文件以支持这些新功能。整体结构保持不变,但增强了用户自定义选项和编辑器的响应能力。

Changes

文件路径 更改摘要
packages/ai-native/src/browser/ai-core.contribution.ts 新增设置选项 IntelligentCompletionsHighlight,更新 registerSettingregisterSettingSection 方法。
packages/ai-native/src/browser/contrib/intelligent-completions/decoration/ghost-text-tokenization.decoration.ts 新增类 GhostTextTokenizationAdditionalLinesWidget,用于管理幽灵文本的标记和渲染。
packages/ai-native/src/browser/contrib/intelligent-completions/index.ts 新增接口 IGhostTextWidgetModelEnhanced,扩展现有接口以增强功能。
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts 更新 IntelligentCompletionsController 类,重构方法以改进可读性和功能,新增导入以增强可观察性。
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.module.less 新增 CSS 类 .inline_completion_ghost_text_decoration,用于样式化幽灵文本。
packages/ai-native/src/browser/preferences/schema.ts aiNativePreferenceSchema 中新增 IntelligentCompletionsHighlight 属性。
packages/core-common/src/settings/ai-native.ts AINativeSettingSectionsId 枚举中新增 IntelligentCompletionsHighlight 条目。
packages/i18n/src/common/en-US.lang.ts 新增本地化条目 preference.ai.native.intelligentCompletions.highlight
packages/i18n/src/common/zh-CN.lang.ts 新增中文本地化条目 preference.ai.native.intelligentCompletions.highlight

Possibly related PRs

Suggested labels

🎨 feature

Suggested reviewers

  • bytemain
  • erha19

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.

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:*"


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

🧹 Outside diff range and nitpick comments (10)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.module.less (1)

9-14: 幽灵文本装饰样式实现得当

新增的 .inline_completion_ghost_text_decoration 类:

  • 正确使用了 VS Code 的主题变量
  • 通过透明度和斜体样式清晰地区分了幽灵文本
  • 边框样式增强了可见性

建议考虑添加以下改进:

 .inline_completion_ghost_text_decoration {
   background-color: var(--vscode-editorGhostText-background);
   border: 1px solid var(--vscode-editorGhostText-border);
   opacity: 0.34;
   font-style: italic;
+  pointer-events: none;
 }

添加 pointer-events: none 可以确保幽灵文本不会干扰用户的鼠标交互。

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

19-19: 建议添加注释说明

新增的枚举成员 IntelligentCompletionsHighlight 实现符合现有代码规范,且与其他智能补全相关的配置项放在一起,结构清晰。建议为保持一致性,添加类似其他配置项的文档注释,说明该配置的具体用途。

建议添加如下注释:

+  /**
+   * Whether to enable highlighting for intelligent completions
+   */
  IntelligentCompletionsHighlight = 'ai.native.intelligentCompletions.highlight',
packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (1)

11-13: 新接口设计合理,建议补充文档说明

IGhostTextWidgetModelEnhanced 接口的设计符合需求,通过扩展 IGhostTextWidgetModel 来支持内联补全高亮功能。但建议添加以下改进:

  1. 为接口添加 JSDoc 注释,说明其用途和设计意图
  2. targetCompletionModel 属性添加详细的注释说明
+/**
+ * 增强的幽灵文本部件模型,用于支持内联补全高亮功能
+ */
 export interface IGhostTextWidgetModelEnhanced extends IGhostTextWidgetModel {
+  /**
+   * 目标补全模型,用于管理和控制内联补全的状态
+   * 支持可设置的观察者模式,并确保正确释放资源
+   */
   readonly targetCompletionModel: ISettableObservable<InlineCompletionsModel | undefined, void> & IDisposable;
 }
packages/ai-native/src/browser/preferences/schema.ts (1)

53-56: 代码结构合理,建议添加描述信息

新增的智能补全高亮配置项结构清晰,与其他智能补全相关配置放在一起,便于维护。但建议添加描述信息以提升用户体验。

建议按如下方式添加描述:

 [AINativeSettingSectionsId.IntelligentCompletionsHighlight]: {
   type: 'boolean',
   default: false,
+  description: localize('preference.ai.native.intelligentCompletions.highlight.description'),
 },
packages/ai-native/src/browser/ai-core.contribution.ts (1)

355-358: 建议提取设置注册逻辑以提高可维护性

为了提高代码的可维护性和可读性,建议将智能补全相关的设置注册逻辑提取到一个单独的方法中。

建议按照以下方式重构:

+ private registerIntelligentCompletionsSettings(registry: ISettingRegistry) {
+   registry.registerSettingSection(AI_NATIVE_SETTING_GROUP_ID, {
+     title: localize('preference.ai.native.intelligentCompletions.title'),
+     preferences: [
+       {
+         id: AINativeSettingSectionsId.IntelligentCompletionsCacheEnabled,
+         localized: 'preference.ai.native.intelligentCompletions.cache.enabled',
+       },
+       // ... other intelligent completion settings
+       {
+         id: AINativeSettingSectionsId.IntelligentCompletionsHighlight,
+         localized: 'preference.ai.native.intelligentCompletions.highlight',
+       },
+     ],
+   });
+ }

  registerSetting(registry: ISettingRegistry) {
    // ... other settings registration
    if (this.aiNativeConfigService.capabilities.supportsInlineCompletion) {
-     registry.registerSettingSection(...)
+     this.registerIntelligentCompletionsSettings(registry);
    }
  }
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (5)

115-116: 建议将 DisposableStore 声明为类的成员变量

在第115行和第116行,多次创建 alwaysVisibleDisposablehighlightDisposable 可能造成不必要的资源消耗。建议将它们声明为类的成员变量,以便在整个类的生命周期内管理这些可释放的资源。


Line range hint 143-165: 避免直接访问私有属性 _suggestWidgetAdaptor

在此代码段中,直接访问了 inlineCompletionsController 的私有属性 _suggestWidgetAdaptor。这种做法可能导致未来的兼容性问题,因为私有属性可能会在类的实现中发生变化。建议使用公开的接口或方法来实现相同的功能,以提高代码的稳定性和可维护性。


268-277: 关注偏好设置变化时资源的正确释放

在监听 IntelligentCompletionsHighlight 偏好设置变化时,注册和注销功能可能导致资源未正确释放。建议在 unregisterHighlight 中销毁 highlightDisposable 后,不要立即重新创建 DisposableStore,而是在需要时再创建,以避免不必要的资源分配。


222-227: 优化 unregisterAlwaysVisible 方法中的资源管理

unregisterAlwaysVisible 方法中,销毁 alwaysVisibleDisposable 后立即重新创建一个新的 DisposableStore。建议仅在需要时再创建新的实例,以避免额外的资源消耗。


271-276: 确保偏好设置变化时资源的正确管理

在偏好设置 IntelligentCompletionsHighlight 变化时,需确保 highlightDisposable 的正确创建和销毁。建议在注销时不立即创建新的 DisposableStore,而是在下次注册时再创建,以优化资源管理。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between bb5e972 and 1930886.

📒 Files selected for processing (9)
  • packages/ai-native/src/browser/ai-core.contribution.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/decoration/ghost-text-tokenization.decoration.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 (8 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.module.less (1 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)
🔇 Additional comments (11)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.module.less (1)

Line range hint 1-7: 现有的建议样式类看起来不错!

这些类使用了正确的变量命名约定,并且样式实现合理。

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

2-5: 验证新增依赖项的必要性

新增的 Monaco 相关导入看起来是合理的,它们支持了内联补全高亮的核心功能。但建议验证这些依赖项的使用情况。

✅ Verification successful

导入依赖项验证完成

这些导入的类型和接口在代码库中都有明确的使用:

  • IDisposable 用于资源释放管理
  • ISettableObservable 用于可设置的观察者模式实现
  • IGhostTextWidgetModel 用于幽灵文本小部件模型扩展
  • InlineCompletionsModel 用于内联补全模型集成

这些导入都是必要的,它们支持了智能补全功能的核心实现。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:验证新导入的类型和接口的使用情况

# 检查 IDisposable 的使用
rg "IDisposable" -t typescript

# 检查 ISettableObservable 的使用
rg "ISettableObservable" -t typescript

# 检查 IGhostTextWidgetModel 的使用
rg "IGhostTextWidgetModel" -t typescript

# 检查 InlineCompletionsModel 的使用
rg "InlineCompletionsModel" -t typescript

Length of output: 292


Script:

#!/bin/bash
# 修正:移除 -t typescript 标志,直接搜索这些类型和接口的使用

# 检查 IDisposable 的使用
rg "IDisposable"

# 检查 ISettableObservable 的使用
rg "ISettableObservable"

# 检查 IGhostTextWidgetModel 的使用
rg "IGhostTextWidgetModel"

# 检查 InlineCompletionsModel 的使用
rg "InlineCompletionsModel"

# 检查文件中这些类型的实际使用
rg -A 5 "class|interface|type" "packages/ai-native/src/browser/contrib/intelligent-completions/index.ts"

Length of output: 105545

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

355-358: 代码实现符合预期!

新增的智能补全高亮设置遵循了现有的设置注册模式,并且正确地集成到了智能补全设置组中。


355-358: 请验证新设置在UI组件中的处理逻辑

需要确保新增的智能补全高亮设置在相关的UI组件中得到正确处理。

✅ Verification successful

Let's examine the implementation details in these files to verify the UI handling.


智能补全高亮设置已正确集成到UI组件中

通过代码验证,该设置已经完整地集成到系统中:

  • 在控制器中实现了正确的设置监听和处理逻辑(registerHighlight/unregisterHighlight)
  • 在schema中定义了合适的类型(boolean)和默认值
  • 在核心设置中定义了正确的设置标识符

设置的UI处理逻辑完整且符合系统架构要求,无需进一步修改。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索智能补全高亮设置的使用情况
echo "检查智能补全高亮设置的使用情况..."

# 使用 ast-grep 搜索设置的使用
ast-grep --pattern 'IntelligentCompletionsHighlight'

# 使用 ripgrep 搜索可能的引用
rg -l "IntelligentCompletionsHighlight"

Length of output: 561


Script:

#!/bin/bash
# 检查设置在控制器中的具体使用
echo "检查智能补全高亮设置在控制器中的实现..."
rg -A 5 "IntelligentCompletionsHighlight" packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts

echo "\n检查设置的schema定义..."
rg -A 5 "IntelligentCompletionsHighlight" packages/ai-native/src/browser/preferences/schema.ts

echo "\n检查设置的类型定义..."
rg -A 5 "IntelligentCompletionsHighlight" packages/core-common/src/settings/ai-native.ts

Length of output: 1394

packages/i18n/src/common/zh-CN.lang.ts (1)

1263-1263: 翻译内容准确且符合规范!

新增的智能补全高亮功能的翻译准确表达了设置项的用途,并且与其他智能补全相关的翻译保持一致的风格。

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

1498-1499: 智能补全高亮配置项的本地化字符串添加正确!

新增的本地化字符串格式规范,描述清晰,并且与其他智能补全相关的配置项保持一致的命名风格。

packages/ai-native/src/browser/contrib/intelligent-completions/decoration/ghost-text-tokenization.decoration.ts (3)

378-385: 正确地清理了视图区域资源

clear 方法中,调用了 changeAccessor.removeZone 并将 this._viewZoneId 重置为 undefined,确保了视图区域资源的正确释放,防止了内存泄漏。


1-35: 代码结构清晰,符合规范

导入的模块和定义的接口清晰明确,遵循了良好的代码风格和项目规范。代码的组织方式有助于维护和理解。


256-257: 验证频繁调用 forceTokenization 对性能的影响

在虚拟模型中多次调用 tokenization.forceTokenization 方法(例如第256行和第406行),可能会对编辑器性能产生影响,特别是在处理大型文件或高频率操作时。建议验证这些调用是否会导致性能问题。

您可以运行以下脚本来统计代码库中 forceTokenization 的调用次数和位置,以评估其可能的性能影响:

Also applies to: 406-407

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

241-245: 代码逻辑清晰,设置获取正确

对于 isHighlight 的获取和默认值设置,逻辑正确,符合预期。


15-22: 导入的模块合理且必要

新增的导入模块如 IObservableconstObservablemapObservableArrayCached 等对于功能实现是必要的,导入合理。

@Ricbet Ricbet changed the title support inline completion highlight WIP: support inline completion highlight Nov 14, 2024
@Ricbet
Copy link
Member Author

Ricbet commented Nov 14, 2024

monaco 在 9 月份的时候已经实现了这一功能,后面我们只需要升级 monaco 即可,所以本 PR 可以不用合了
相关 issue: microsoft/vscode#225400
相关 PR: microsoft/vscode#227547

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] 内联补全提示支持代码高亮
1 participant