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:support VSCode API: GlobalEnvironmentVariableCollectoin #4171

Merged
merged 3 commits into from
Nov 20, 2024

Conversation

bk1012
Copy link
Member

@bk1012 bk1012 commented Nov 18, 2024

Types

  • 🎉 New Features

Background or solution

Support VSCode API: GlobalEnvironmentVariableCollectoin

Changelog

Summary by CodeRabbit

  • 新功能
    • 引入了 MultiKeyMap 类,提供多层次键值管理。
    • 添加了 NO_ROOT_URI 常量,表示无根 URI 的情况。
    • 新增 GlobalEnvironmentVariableCollection 接口,支持按范围检索环境变量集合。
  • 修复
    • 修正了 getEnviromentVariableCollection 方法名为 getEnvironmentVariableCollection,确保一致性。
  • 改进
    • 更新了 ExtHostTerminal 类以支持多键结构,改进环境变量管理。
    • 修改了 EnvironmentVariableCollection 类,使其扩展 GlobalEnvironmentVariableCollection

Copy link
Contributor

coderabbitai bot commented Nov 18, 2024

Walkthrough

此拉取请求引入了一个新的 MultiKeyMap<K, V> 类,用于管理多级映射,支持任意深度的键。该类包括多个方法,如 setgethasdeleteforEach,并在实现中强制执行键长度约束。此外,更新了多个文件中的方法名称,修复了拼写错误,并对环境变量集合的管理进行了结构性改进,采用了新的 GlobalEnvironmentVariableCollection 接口。

Changes

文件路径 变更摘要
packages/core-common/src/collections.ts 新增类 MultiKeyMap<K, V>,包含多个方法:createsetgethasdeleteforEach
packages/core-common/src/index.ts 新增导出:export * from './collections';
packages/extension/tests/hosted/api/vscode/ext.host.terminal.test.ts 修改方法名称:getEnviromentVariableCollectiongetEnvironmentVariableCollection
packages/extension/src/common/extension.ts 新增常量:export const NO_ROOT_URI = '<none>';
packages/extension/src/common/vscode/terminal.ts 修改接口 IExtHostTerminal 中的方法签名:getEnviromentVariableCollectiongetEnvironmentVariableCollection,返回类型改为 vscode.GlobalEnvironmentVariableCollection
packages/extension/src/hosted/api/vscode/env/envApiFactory.ts 修改 environmentVariableCollection 的返回类型:vscode.EnvironmentVariableCollectionvscode.GlobalEnvironmentVariableCollection,修复方法名称。
packages/extension/src/hosted/api/vscode/ext.host.extensions.ts 修改 ExtensionContext 类中的方法名称:getEnviromentVariableCollectiongetEnvironmentVariableCollection
packages/extension/src/hosted/api/vscode/ext.host.terminal.ts 修改 ExtHostTerminal 类中的多个方法和属性,更新 getEnvironmentVariableCollection 方法,新增多键管理结构。
packages/types/vscode/typings/vscode.d.ts 新增接口 GlobalEnvironmentVariableCollectionEnvironmentVariableScope,并添加方法 getScoped

Possibly related PRs

Suggested labels

🎨 feature

Suggested reviewers

  • Ricbet

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

🧹 Outside diff range and nitpick comments (8)
packages/core-common/src/index.ts (1)

38-38: 导出语句的位置建议调整

建议将相关的导出语句进行分组组织,将集合相关的导出与其他数据结构相关的导出(如 comparers)放在一起,以提高代码的可维护性。

建议应用以下更改:

export * from './comparers';
+export * from './collections';
export * from './problem-pattern';
-export * from './collections';
packages/extension/src/common/vscode/terminal.ts (1)

102-102: 建议添加方法文档

建议为 getEnvironmentVariableCollection 方法添加 JSDoc 文档,说明:

  • 方法的用途
  • 参数 extension 的作用
  • 返回值 GlobalEnvironmentVariableCollection 的使用说明
+  /**
+   * 获取指定扩展的全局环境变量集合
+   * @param extension 扩展属性
+   * @returns 全局环境变量集合实例
+   */
   getEnvironmentVariableCollection(extension: IExtensionProps): vscode.GlobalEnvironmentVariableCollection;
packages/core-common/src/collections.ts (3)

21-21: 建议为rootMap添加类型注解以提高类型安全性

当前rootMap未指定类型,可以考虑为其添加类型注解,提升代码的类型安全性和可读性。

建议修改为:

- private rootMap = new Map();
+ private rootMap = new Map<K, any>();

35-36: 建议使用更具体的错误类型

在多个方法中,当键长度不符合要求时,抛出了通用的Error。为提高错误信息的清晰度和代码的可维护性,建议使用更具体的错误类型,如RangeError或自定义错误。

例如,修改为:

- throw new Error(`inappropriate key length: ${key.length}, should be ${this.keyLength}`);
+ throw new RangeError(`Invalid key length: ${key.length}, expected: ${this.keyLength}`);

Also applies to: 53-54, 72-73, 91-92


112-122: 优化doForeach方法的类型注解

doForeach方法中,使用了any类型,可能会降低类型安全性。建议为参数和内部变量添加具体的类型注解,以增强类型检查和代码可读性。

例如,修改为:

- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- private doForeach(handler: (value: V, key: K[]) => void, currentMap: Map<any, any>, keys: K[]): void {
+ private doForeach(handler: (value: V, key: K[]) => void, currentMap: Map<K, any> | Map<K, V>, keys: K[]): void {
packages/extension/src/hosted/api/vscode/ext.host.terminal.ts (1)

600-601: 无需禁用未使用变量的 eslint 规则

参数 _scope 已经以下划线开头,根据 eslint 规则,未使用的参数会被自动忽略。因此,第600行的 // eslint-disable-next-line @typescript-eslint/no-unused-vars 注释是多余的,可以安全地移除以简化代码。

packages/types/vscode/typings/vscode.d.ts (2)

3187-3201: 建议优化 getScoped 方法的注释以提高可读性。

当前注释中的一些句子较为复杂,可能导致理解困难。建议对注释进行简化,使其更加清晰易懂。

建议修改注释如下:

 /**
  * 获取适用于指定作用域的环境变量集合。这使得能够仅在指定的作用域内修改终端环境变量,新的集合将在全局集合之后应用。
  *
- * Each object obtained through this method is isolated and does not impact objects for other scopes,
- * including the global collection.
+ * 通过此方法获得的每个对象都是独立的,不会影响其他作用域(包括全局集合)中的对象。
  *
  * @param scope 环境变量集合适用的作用域。
  *
- * If a scope parameter is omitted, collection applicable to all relevant scopes for that parameter is
- * returned. For instance, if the 'workspaceFolder' parameter is not specified, the collection that applies
- * across all workspace folders will be returned.
+ * 如果省略了 scope 参数,将返回适用于所有相关作用域的集合。例如,如果未指定 'workspaceFolder' 参数,将返回适用于所有工作区文件夹的集合。
  *
  * @returns 指定作用域的环境变量集合。
  */

3205-3214: EnvironmentVariableScope 接口定义清晰,但可补充更多说明。

目前 EnvironmentVariableScope 接口只有一个可选属性 workspaceFolder。建议在注释中提供更多关于该属性的使用示例,以帮助开发者更好地理解其用途。

建议在注释中添加使用示例:

 /**
  * 环境变量集合适用的作用域对象。
  */
 export interface EnvironmentVariableScope {
   /**
    * 指定要获取其环境变量集合的特定工作区文件夹。
+   *
+   * 示例:
+   * ```typescript
+   * const envCollection = globalEnvVarCollection.getScoped({ workspaceFolder: myWorkspaceFolder });
+   * ```
    */
   workspaceFolder?: WorkspaceFolder;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between dedba74 and e0612a0.

📒 Files selected for processing (9)
  • packages/core-common/src/collections.ts (1 hunks)
  • packages/core-common/src/index.ts (1 hunks)
  • packages/extension/__tests__/hosted/api/vscode/ext.host.terminal.test.ts (1 hunks)
  • packages/extension/src/common/extension.ts (1 hunks)
  • packages/extension/src/common/vscode/terminal.ts (1 hunks)
  • packages/extension/src/hosted/api/vscode/env/envApiFactory.ts (1 hunks)
  • packages/extension/src/hosted/api/vscode/ext.host.extensions.ts (1 hunks)
  • packages/extension/src/hosted/api/vscode/ext.host.terminal.ts (7 hunks)
  • packages/types/vscode/typings/vscode.d.ts (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • packages/extension/tests/hosted/api/vscode/ext.host.terminal.test.ts
  • packages/extension/src/common/extension.ts
  • packages/extension/src/hosted/api/vscode/ext.host.extensions.ts
🔇 Additional comments (7)
packages/core-common/src/index.ts (1)

38-38: 验证 collections 模块的实现

需要确认新导出的 collections 模块是否正确实现了 VSCode API 的 GlobalEnvironmentVariableCollection 接口要求。

运行以下脚本以验证实现:

packages/extension/src/hosted/api/vscode/env/envApiFactory.ts (1)

84-86: 代码更改正确实现了 GlobalEnvironmentVariableCollection API 支持

环境变量集合的 getter 实现看起来正确,通过扩展上下文获取全局环境变量集合。

运行以下脚本验证相关接口实现:

✅ Verification successful

Let me gather more information about the implementation details.


GlobalEnvironmentVariableCollection 实现验证完成

代码实现完全符合预期:

  • EnvironmentVariableCollection 类正确实现了 GlobalEnvironmentVariableCollection 接口
  • 包含了所有必要的方法(replaceappendprependgetdeleteclear 等)
  • 正确处理了持久化属性和作用域相关功能
  • 通过 _onDidChangeCollection 事件正确通知变更
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:验证 GlobalEnvironmentVariableCollection 接口实现和使用

# 检查接口定义
ast-grep --pattern 'interface GlobalEnvironmentVariableCollection {
  $$$
}'

# 检查实现类
ast-grep --pattern 'class $_ implements GlobalEnvironmentVariableCollection {
  $$$
}'

# 检查使用情况
rg "GlobalEnvironmentVariableCollection"

Length of output: 810


Script:

#!/bin/bash
# Check the implementation of EnvironmentVariableCollection class
ast-grep --pattern 'class EnvironmentVariableCollection implements vscode.GlobalEnvironmentVariableCollection {
  $$$
}'

# Check the implementation of getEnvironmentVariableCollection method
ast-grep --pattern 'getEnvironmentVariableCollection($_) {
  $$$
}'

# Check the interface definition in vscode.d.ts
rg -A 5 "interface GlobalEnvironmentVariableCollection extends EnvironmentVariableCollection" packages/types/vscode/typings/vscode.d.ts

Length of output: 8988

packages/extension/src/common/vscode/terminal.ts (1)

102-102: 验证类型更改的兼容性

请确认 GlobalEnvironmentVariableCollection 是否继承自 EnvironmentVariableCollection,以避免破坏现有的调用代码。

✅ Verification successful

Let me gather more information about the type definitions to verify the compatibility.


Let me try one more search to find the type definitions in other possible locations.


类型更改是安全的,无需担心兼容性问题

根据代码分析,我们可以确认:

  • GlobalEnvironmentVariableCollection 确实继承自 EnvironmentVariableCollection
  • 现有实现类 EnvironmentVariableCollection 已经实现了 GlobalEnvironmentVariableCollection 接口
  • 所有调用处都能正确处理这个类型

这个类型更改是向后兼容的,不会破坏现有代码。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:检查 GlobalEnvironmentVariableCollection 的类型定义和继承关系

# 搜索 GlobalEnvironmentVariableCollection 的定义
ast-grep --pattern 'interface GlobalEnvironmentVariableCollection extends $_'

# 搜索现有代码中对 getEnvironmentVariableCollection 的调用
rg -A 2 'getEnvironmentVariableCollection'

Length of output: 2126


Script:

#!/bin/bash
# 搜索 vscode 类型定义中的 EnvironmentVariableCollection 相关定义
rg -A 5 "EnvironmentVariableCollection" packages/extension/src/types/vscode.d.ts

# 搜索 GlobalEnvironmentVariableCollection 的使用和实现
rg -A 5 "GlobalEnvironmentVariableCollection" packages/extension/src/types/vscode.d.ts

# 搜索实际的实现类
ast-grep --pattern 'class $_ implements GlobalEnvironmentVariableCollection'

Length of output: 408


Script:

#!/bin/bash
# 搜索所有可能包含类型定义的文件
fd -e d.ts -x rg -A 5 "EnvironmentVariableCollection|GlobalEnvironmentVariableCollection" {}

# 搜索实现文件中的类型关系
rg -A 5 "implements.*EnvironmentVariableCollection"

# 查找可能的类型导入
rg "@types/vscode"

Length of output: 3193

packages/core-common/src/collections.ts (1)

70-73: 注意方法中键长度处理不一致

hasdelete方法中,允许键的长度小于等于keyLength,而在setget方法中,要求键的长度必须等于keyLength。请确认这是否为预期行为,或考虑统一键长度的处理方式,以避免使用上的混淆。

Also applies to: 89-92

packages/extension/src/hosted/api/vscode/ext.host.terminal.ts (2)

67-67: 使用 MultiKeyMap 实现多级键映射

在第67行中,将 environmentVariableCollections 的类型更改为 MultiKeyMap<string, EnvironmentVariableCollection>,支持基于 extension.idrootUri 的多级键。这样可以更高效地管理环境变量集合,代码实现正确。


528-537: 正确实现了 getEnvironmentVariableCollection 方法

在第528-537行,修改了 getEnvironmentVariableCollection 方法,增加了 rootUri 参数,并使用多级键获取或创建环境变量集合。匿名子类中重写了 getScoped 方法,实现了对作用域特定的环境变量集合的支持,设计合理,逻辑清晰。

packages/types/vscode/typings/vscode.d.ts (1)

3182-3204: 新增的接口设计合理,命名清晰。

GlobalEnvironmentVariableCollection 接口清晰地扩展了 EnvironmentVariableCollection,支持全局作用域的环境变量集合,这对于在所有作用域中管理环境变量非常有用。

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 44.00000% with 42 lines in your changes missing coverage. Please review.

Project coverage is 54.06%. Comparing base (f81dbbf) to head (e0612a0).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
packages/core-common/src/collections.ts 38.59% 30 Missing and 5 partials ⚠️
...tension/src/hosted/api/vscode/ext.host.terminal.ts 69.23% 3 Missing and 1 partial ⚠️
...tension/src/hosted/api/vscode/env/envApiFactory.ts 0.00% 2 Missing ⚠️
...nsion/src/hosted/api/vscode/ext.host.extensions.ts 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4171      +/-   ##
==========================================
- Coverage   54.07%   54.06%   -0.01%     
==========================================
  Files        1610     1612       +2     
  Lines       97809    97911     +102     
  Branches    20035    20051      +16     
==========================================
+ Hits        52892    52940      +48     
- Misses      37314    37360      +46     
- Partials     7603     7611       +8     
Flag Coverage Δ
jsdom 49.64% <44.00%> (-0.01%) ⬇️
node 15.56% <4.00%> (-0.02%) ⬇️

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.


🚨 Try these New Features:

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 5dccd6a into main Nov 20, 2024
13 checks passed
@hacke2 hacke2 deleted the feat/support-GlobalEnvironmentVariableCollectoin branch November 20, 2024 07:35
Aaaaash pushed a commit that referenced this pull request Dec 9, 2024
* feat: add typings

* chore: fix typo

* feat: api support
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