-
Notifications
You must be signed in to change notification settings - Fork 433
chore: improve code edits source trigger #4114
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
Conversation
Walkthrough此次更改涉及多个文件,主要集中在智能补全功能的代码编辑处理上。新增了 Changes
Possibly related PRs
Suggested labels
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Outside diff range and nitpick comments (5)
packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (1)
38-46
: 建议添加类的文档注释!新增的 CodeEditsResultValue 类实现合理,但建议添加以下文档:
- 类的用途说明
- 构造函数参数说明
- items getter 方法的返回值说明
建议添加如下文档:
+/** + * 封装代码编辑结果的值对象,负责管理结果的生命周期 + */ export class CodeEditsResultValue extends Disposable { + /** + * @param raw 原始的代码编辑结果 + */ constructor(private readonly raw: ICodeEditsResult) { super(); } + /** + * @returns 代码编辑项数组 + */ public get items(): ICodeEdit[] { return this.raw.items; } }packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (1)
Line range hint
13-15
: 建议为优先级添加注释说明
priority = 2
的值对于代码维护者来说可能不够直观。建议添加注释说明该优先级在整个触发系统中的定位。建议添加如下注释:
@Injectable({ multiple: true }) export class LineChangeCodeEditsSource extends BaseCodeEditsSource { + // 行变更触发的优先级为中等(2),低于语法错误触发(3),高于普通触发(1) public priority = 2;
packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts (1)
Line range hint
92-98
: 代码更改符合优化目标,建议添加错误日志代码变更从
launchProvider
切换到setBean
的方式很好地实现了 PR 的目标,通过统一收集触发源后再决定是否调用 API,可以有效减少不必要的 API 调用。同时对relativeWorkspacePath
的空值处理也更加健壮。建议添加错误日志记录,以便于问题排查:
this.setBean({ typing: ECodeEditsSource.LinterErrors, data: { relativeWorkspacePath: relativeWorkspacePath?.path ?? resource.path, errors: markers.map((marker) => MarkerErrorData.toData(marker)), }, + // 添加日志记录 + console.debug('[LintErrorCodeEditsSource] Processing lint errors:', { + path: relativeWorkspacePath?.path ?? resource.path, + errorCount: markers.length + }); });packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (2)
3-10
: 移除未使用的导入内容在第3-10行中,导入了多个模块。请检查以下导入是否被实际使用:
AINativeSettingSectionsId
Event
IDisposable
如果这些导入未被使用,建议将其移除,以优化代码。
407-409
: 在结果为空时处理在第407-409行中,如果
result
为空或result.items
为空数组,可能需要相应地处理,以避免后续操作中的潜在错误。建议添加一个
else
块,明确处理未获取到结果的情况,提升代码的可读性。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (5)
- packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (3 hunks)
- packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (4 hunks)
- packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts (5 hunks)
- packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (1 hunks)
- packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (16)
packages/ai-native/src/browser/contrib/intelligent-completions/index.ts (2)
1-1
: 导入语句正确且必要!新增的 Disposable 导入用于实现资源管理,符合项目规范。
21-22
: 类型定义格式优化合理!将联合类型的每个成员分行展示,提高了代码的可读性,同时保持了类型定义的完整性。
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts (1)
Line range hint
46-52
: 代码变更符合优化目标!使用
setBean
替代直接调用 provider 的方式很好地实现了触发优先级的管理。通过传递完整的行号信息(当前行和前一行),为上层处理提供了更多上下文,有利于做出更准确的触发决策。packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts (12)
4-4
: 导入ConstructorOf确认无误
10-10
: 正确导入了ICodeEditor
20-21
: 导入ICodeEditsContextBean正确
22-36
: 新增的CodeEditsContextBean类实现正确该类正确继承了Disposable,并实现了必要的属性和方法。
42-42
: 在BaseCodeEditsSource类中添加了codeEditsContextBean属性属性声明正确,类型符合预期。
55-57
: 修改了resetBean方法在事务中重置codeEditsContextBean的值,确保数据一致性。
61-65
: 新增了setBean方法该方法创建新的CodeEditsContextBean实例,并在事务中设置codeEditsContextBean。
74-81
: 在CodeEditsSourceCollection类中添加了cancellationTokenSource和codeEditsContextBean属性这些属性的添加有助于管理异步操作和状态。
101-104
: 添加了observerCodeEditsContextBean用于监听所有source的codeEditsContextBean实现方式正确,利于统一管理。
109-109
: 对observerCodeEditsContextBean进行了防抖处理使用debouncedObservable合并多次连续的事务通知。
112-116
: 正确筛选出有更新的source以获取最新的codeEditsContextBean逻辑清晰,代码实现正确。
130-133
: 在事务中设置codeEditsContextBean确保了只通知最高优先级的结果,操作正确。
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts (1)
64-66
: 确保依赖注入的正确性在第64-66行,引入了
intelligentCompletionsRegistry
的 getter 方法。请确认IntelligentCompletionsRegistryToken
已正确注册且可被注入,以避免在运行时出现未定义的错误。您可以运行以下脚本以验证
IntelligentCompletionsRegistryToken
的注册情况:✅ Verification successful
Let me try a different search strategy to verify the token registration.
让我再检查一下依赖注入的具体实现。
基于已收集的信息,我可以生成最终响应了。
依赖注入配置已正确实现
通过代码分析,我们发现:
IntelligentCompletionsRegistryToken
在packages/core-common/src/types/ai-native/index.ts
中被正确定义为 Symbol- 在
packages/ai-native/src/browser/index.ts
中已经注册了相关的提供者和贡献- 多个组件都正确引用并注入了该 Token,包括 inline-completions 和 intelligent-completions 控制器
因此,依赖注入的配置是正确的,不会出现运行时未定义的错误。
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # 描述:检查 IntelligentCompletionsRegistryToken 是否已在容器中注册 # 期望结果:应找到 IntelligentCompletionsRegistryToken 的注册代码 rg 'provide\(\s*IntelligentCompletionsRegistryToken' --type tsLength of output: 64
Script:
#!/bin/bash # 搜索 IntelligentCompletionsRegistryToken 的定义和使用 rg "IntelligentCompletionsRegistryToken" -A 3 --type ts # 搜索可能的注册模式 ast-grep --pattern 'const IntelligentCompletionsRegistryToken = $_'Length of output: 5168
Script:
#!/bin/bash # 检查相关的模块注册文件 fd "module.ts|contribution.ts" --type f --exec rg -l "IntelligentCompletions"Length of output: 315
Script:
#!/bin/bash # 查找可能的依赖注入配置 rg "registerSingleton|createChild|bindProvider" -A 2 --type ts | rg -A 2 "IntelligentCompletions"Length of output: 97
Script:
#!/bin/bash # 检查 ai-native 模块中的依赖注入配置 rg "providers.*=.*\[" -A 10 --type ts packages/ai-native/src/browser/ # 检查 IntelligentCompletionsRegistry 的实现 ast-grep --pattern 'class IntelligentCompletionsRegistry { $$$ }'Length of output: 939
packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts
Show resolved
Hide resolved
packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts
Show resolved
Hide resolved
.../ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts
Show resolved
Hide resolved
.../ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4114 +/- ##
==========================================
- Coverage 54.25% 54.25% -0.01%
==========================================
Files 1598 1598
Lines 97561 97564 +3
Branches 19948 19954 +6
==========================================
- Hits 52935 52932 -3
- Misses 37076 37083 +7
+ Partials 7550 7549 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
/next |
🎉 PR Next publish successful! 3.4.5-next-1729664938.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Types
Background or solution
这里原有的逻辑是:
不同的触发行为都会先去请求用户注册的 provider api,拿到补全结果后再显示,如果这些触发行为同时被触发了(例如 lint_error 和 line_change)则按优先级来决定显示哪个补全结果
这样的逻辑就导致会频繁的调用 provider api
现在改进的逻辑是:
先按优先级决定要触发哪种行为,再根据行为去调用 provider api,保证只调用一次
Changelog
改进 code edits api 的触发行为规则
Summary by CodeRabbit
CodeEditsResultValue
和CodeEditsContextBean
类,增强了代码编辑的处理能力。