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

fix: diff editor model not exist error #4109

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

erha19
Copy link
Member

@erha19 erha19 commented Oct 21, 2024

Types

  • 🐛 Bug Fixes

Background or solution

原有 Diff Editor 状态恢复失效主要是存在时序问题

即在新的 Diff Model 未渲染,不存在折叠代码等数据时进行恢复,最终导致恢复失败。

本次修复移除了多余的逻辑,让 Diff 编辑器的状态恢复时序回归正常留存,避免了 Model 丢失/未更新等问题出现

Changelog

修复 Diff 编辑器的状态恢复逻辑

Summary by CodeRabbit

  • 新功能

    • 移除 AppConfig 接口中的 enableRestoreDiffEditorState 属性,简化配置选项。
    • 更新编辑器类以增强状态管理和选项处理,特别是在差异编辑上下文中。
    • 新增方法以改进资源管理和状态恢复,确保编辑器组反映最后已知状态。
    • 新增 getLineChanges 方法,以便在差异编辑器中检索行变更。
  • 修复

    • 更新事件处理逻辑,以确保正确的编辑器上下文维护。
  • 文档

    • 添加新的类型和接口,改善编辑器状态和行变更的处理。

@erha19 erha19 requested review from bytemain and Ricbet October 21, 2024 07:48
@opensumi opensumi bot added the 🐞 bug Something isn't working label Oct 21, 2024
@erha19 erha19 requested a review from hacke2 October 21, 2024 07:53
Copy link
Contributor

coderabbitai bot commented Oct 21, 2024

Walkthrough

此拉取请求主要涉及对 AppConfig 接口的修改,具体是移除了 enableRestoreDiffEditorState 属性。此外,对 editor-collection.service.tsworkbench-editor.service.ts 文件中的编辑器类进行了多项更新,增强了状态管理和资源管理功能。editor.ts 文件则引入了新的接口和函数,以改善编辑器视图状态的处理。

Changes

文件路径 变更摘要
packages/core-browser/src/react-providers/config-provider.tsx 移除 AppConfig 接口中的 enableRestoreDiffEditorState 属性。
packages/editor/src/browser/editor-collection.service.ts 更新 BrowserCodeEditorBrowserDiffEditor 类,增强状态恢复和编辑器选项处理,添加 bindPreventNavigation 工具函数,修改了多个方法的签名。
packages/editor/src/browser/workbench-editor.service.ts 更新 WorkbenchEditorServiceImplEditorGroup 类,增强资源管理和事件处理,添加新方法以改善编辑器状态恢复。
packages/editor/src/common/editor.ts 添加新接口 ICodeEditorViewStateIDiffEditorViewState,引入 isTextEditorViewState 函数,修改 IDiffEditor 接口以包含 getLineChanges 方法。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant EditorService
    participant DiffEditor
    participant ConfigProvider

    User->>ConfigProvider: 请求配置
    ConfigProvider-->>User: 返回配置
    User->>EditorService: 打开Diff编辑器
    EditorService->>DiffEditor: 恢复状态
    DiffEditor-->>EditorService: 返回状态
    EditorService-->>User: 显示Diff编辑器
Loading

Possibly related PRs

Suggested labels

🐞 bug

Suggested reviewers

  • Ricbet
  • bytemain
  • hacke2

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 (2)
packages/editor/src/common/editor.ts (2)

992-992: 新增的 getLineChanges 方法增强了差异编辑器的功能

在 IDiffEditor 接口中添加 getLineChanges 方法是一个很好的改进。这个方法允许获取行级别的变更信息,可能对于以下场景很有用:

  1. 高亮显示具体的变更行
  2. 生成详细的差异报告
  3. 实现自定义的差异处理逻辑

建议考虑添加一些文档注释,说明返回值 ILineChange[] 的结构和可能的 null 情况。


993-1011: 新增的 isTextEditorViewState 函数提高了类型安全性

这个新增的函数很好地实现了对编辑器视图状态的类型检查,有助于提高代码的类型安全性。函数的实现逻辑看起来是正确的,并且涵盖了代码编辑器和差异编辑器的视图状态。

建议考虑以下小改进以提高可读性:

  1. 添加函数的文档注释,说明其用途和返回值含义。
  2. 考虑使用 early return 模式简化逻辑,例如:
if (!viewState) {
  return false;
}

if ('modified' in viewState) {
  return isTextEditorViewState(viewState.modified);
}

return !!(
  'contributionsState' in viewState &&
  'viewState' in viewState &&
  Array.isArray(viewState.cursorState)
);

这样可以减少嵌套并提高代码的可读性。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 06b3924 and 3d6732e.

📒 Files selected for processing (4)
  • packages/core-browser/src/react-providers/config-provider.tsx (0 hunks)
  • packages/editor/src/browser/editor-collection.service.ts (5 hunks)
  • packages/editor/src/browser/workbench-editor.service.ts (0 hunks)
  • packages/editor/src/common/editor.ts (2 hunks)
💤 Files with no reviewable changes (2)
  • packages/core-browser/src/react-providers/config-provider.tsx
  • packages/editor/src/browser/workbench-editor.service.ts
🧰 Additional context used
🔇 Additional comments (8)
packages/editor/src/common/editor.ts (1)

25-26: 新增的类型导入有助于改善编辑器状态管理

这些新增的类型导入(ICodeEditorViewState 和 IDiffEditorViewState)看起来是为了增强编辑器视图状态的管理能力。这可能会改善编辑器状态的保存和恢复功能,提高用户体验。

packages/editor/src/browser/editor-collection.service.ts (7)

34-34: 确认导入的有效性

导入 isTextEditorViewState 用于后续状态类型检查,确保状态恢复过程的安全性和可靠性。


514-517: 在销毁编辑器前保存当前状态

dispose 方法中添加了调用 this.saveCurrentState();,确保在移除编辑器之前保存当前的视图状态,便于后续的状态恢复。


516-519: 在状态恢复前验证状态类型

在恢复视图状态前,检查 state 是否为有效的编辑器视图状态,确保只有有效的状态才会被恢复,防止潜在的错误。


619-619: 更新 restoreState 方法以接受选项参数

restoreState 方法增加了 options: IResourceOpenOptions 参数,使其能够根据传入的选项调整状态恢复逻辑。


622-625: 验证状态类型并调整光标状态以避免重复选中

在恢复状态前,检查 state 是否为有效的编辑器视图状态。如果选项中指定了 rangeoriginalRange,则清空 state.modified!.cursorState 以防止重复的光标选中状态。


658-659: 移除模型缓存并直接创建视图模型

compare 方法中移除了缓存逻辑,直接通过 this.monacoDiffEditor.createViewModel({ original, modified }) 创建新的视图模型,简化了代码,提高了状态同步的可靠性。


673-676: 在恢复状态前等待差异计算完成

新增了 await model?.waitForDiff();,确保在调用 this.restoreState(options); 之前,差异计算已经完成,以防止在 Diff 尚未渲染时恢复状态而导致的问题。

Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 28.57143% with 15 lines in your changes missing coverage. Please review.

Project coverage is 54.30%. Comparing base (06b3924) to head (3d6732e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/editor/src/common/editor.ts 33.33% 7 Missing and 1 partial ⚠️
...es/editor/src/browser/editor-collection.service.ts 22.22% 4 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4109   +/-   ##
=======================================
  Coverage   54.30%   54.30%           
=======================================
  Files        1596     1596           
  Lines       97473    97476    +3     
  Branches    19939    19948    +9     
=======================================
+ Hits        52931    52935    +4     
- Misses      36997    36998    +1     
+ Partials     7545     7543    -2     
Flag Coverage Δ
jsdom 49.86% <28.57%> (+<0.01%) ⬆️
node 15.61% <4.76%> (+<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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants