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 TreeView.badge #4185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zhiyudangchu
Copy link

@zhiyudangchu zhiyudangchu commented Nov 26, 2024

Types

  • 🎉 New Features

Background or solution

Support VSCode API TreeView.badge

Changelog

Summary by CodeRabbit

  • 新功能

    • 引入了 BadgeWidget 接口,增强了徽章管理功能。
    • 在树视图中新增了设置徽章的功能,支持更复杂的徽章表示。
    • 在文件树和标签栏中实现了徽章动态更新,提升用户反馈。
  • 文档

    • 更新了 TreeViewWebviewView 接口,新增徽章属性以改善视觉表示。
  • 修复

    • 改进了徽章渲染逻辑,确保正确处理嵌套结构,增强了错误处理能力。

@CLAassistant
Copy link

CLAassistant commented Nov 26, 2024

CLA assistant check
All committers have signed the CLA.

@opensumi opensumi bot added the 🎨 feature feature required label Nov 26, 2024
@bk1012 bk1012 requested a review from erha19 November 26, 2024 09:33
@erha19
Copy link
Member

erha19 commented Nov 26, 2024

@zhiyudangchu 感谢贡献 ~ 麻烦先签署一下 CLA 协议 #4185 (comment)

@Ricbet Ricbet requested a review from bk1012 November 26, 2024 09:39
Copy link
Contributor

coderabbitai bot commented Nov 26, 2024

Walkthrough

此拉取请求引入了与徽章管理相关的多个接口和方法的更改,主要包括新接口BadgeWidget的定义以及对现有接口和类的更新,以支持更复杂的徽章数据结构。具体而言,多个组件的徽章属性类型从字符串更改为BadgeWidget | undefined,并添加了新的方法以设置和处理徽章。这些更改增强了用户界面中徽章的表示和交互能力。

Changes

文件路径 更改摘要
packages/core-browser/src/layout/layout.interface.ts 新增接口BadgeWidget,更新ViewExtViewContainerOptions接口中的badge属性类型为`BadgeWidget
packages/extension/src/browser/vscode/api/main.thread.treeview.ts 新增方法$setBadge,允许设置树视图的徽章,导入ViewBadge类型。
packages/extension/src/common/vscode/treeview.ts IMainThreadTreeView接口中新增方法$setBadge,更新TreeView接口以包含badge属性,新增接口ViewBadge
packages/extension/src/hosted/api/vscode/ext.host.treeview.ts TreeView接口和ExtHostTreeView类中新增badge属性,支持获取和设置徽章。
packages/file-tree-next/src/browser/file-tree-contribution.ts FileTreeContribution类中新增FileTreeEventService依赖,更新徽章显示逻辑以响应文件更改。
packages/file-tree-next/src/browser/file-tree.service.ts 新增公共方法calcNodeCount和私有方法recursionCalcNodeCount,计算文件树中的节点总数。
packages/file-tree-next/src/browser/services/file-tree-event.service.ts 新增FileTreeEventService类,定义文件更改事件的处理逻辑。
packages/main-layout/src/browser/accordion/accordion.service.ts 更新AccordionViewChangeEvent接口中的badge属性类型,更新updateViewBadge方法签名。
packages/main-layout/src/browser/accordion/section.view.tsx 更新CollapsePanelProps接口中的badge属性类型,调整AccordionSection组件的徽章渲染逻辑。
packages/main-layout/src/browser/tabbar-handler.ts 更新TabBarHandler类中的setBadge方法参数类型为`BadgeWidget
packages/main-layout/src/browser/tabbar/bar.view.tsx 更新IconTabViewTextTabView组件的徽章渲染逻辑以处理嵌套结构。
packages/main-layout/src/browser/tabbar/tabbar.service.ts 更新updateBadge方法参数类型为`BadgeWidget
packages/opened-editor/src/browser/services/opened-editor-model.service.ts 更新setExplorerTabBarBadge方法以使用新的徽章对象结构。
packages/types/vscode/typings/vscode.d.ts TreeView接口中新增可选属性badge,定义ViewBadge接口。
packages/types/vscode/typings/vscode.window.d.ts WebviewView接口中新增可选属性badge

Possibly related PRs

Suggested labels

🎨 feature

Suggested reviewers

  • bytemain
  • Ricbet
  • erha19
  • hacke2

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 [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/[email protected]: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/[email protected]: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > [email protected]: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > [email protected]: 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 > [email protected]: 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: 2

🧹 Outside diff range and nitpick comments (24)
packages/file-tree-next/src/browser/services/file-tree-event.service.ts (1)

14-17: 事件处理方法实现简洁有效

onFilesChangeEvent 方法使用 @OnEvent 装饰器正确订阅了文件变更事件,并通过发射空事件通知相关订阅者。这种实现方式简单且有效。

不过建议添加方法的返回值类型声明以提高代码的可维护性:

- onFilesChangeEvent() {
+ onFilesChangeEvent(): void {
packages/core-browser/src/layout/layout.interface.ts (3)

31-36: 接口设计合理,建议补充文档注释

新增的 BadgeWidget 接口设计完整,包含了徽章数值、提示文本以及相关的变更事件。建议为接口及其属性添加 JSDoc 文档注释,以提供更好的开发体验。

建议添加如下文档注释:

+/**
+ * 表示视图徽章的配置接口
+ */
 export interface BadgeWidget {
+  /** 徽章显示的数值 */
   badge?: number;
+  /** 徽章的悬停提示文本 */
   badgeTooltip?: string;
+  /** 徽章数值变更事件 */
   onDidChangeBadge?: Event<void>;
+  /** 徽章提示文本变更事件 */
   onDidChangeBadgeTooltip?: Event<void>;
 }

46-46: 简化类型声明,移除冗余的 undefined

由于使用了可选属性操作符 ?,类型中的 | undefined 是多余的。

建议简化类型声明:

-  badge?: BadgeWidget | undefined;
+  badge?: BadgeWidget;

70-70: 简化类型声明,保持一致性

同样地,这里的类型声明也可以移除冗余的 undefined。

建议应用相同的简化:

-  badge?: BadgeWidget | undefined;
+  badge?: BadgeWidget;
packages/main-layout/src/browser/tabbar-handler.ts (2)

4-4: 建议优化导入路径

当前导入路径使用了多层相对路径(../../../),这种方式可能会导致代码的脆弱性。建议考虑以下改进方案:

  1. 使用别名导入(alias imports)
  2. 创建统一的导出文件(barrel exports)

示例改进:

-import { BadgeWidget } from '../../../core-browser/src/layout';
+import { BadgeWidget } from '@opensumi/core-browser';

112-114: 方法签名更新符合预期

setBadge 方法的参数类型从 string 更新为 BadgeWidget | undefined 是一个很好的改进:

  • ✅ 支持了更丰富的徽章展示功能
  • ✅ 与 VSCode API 的 TreeView.badge 特性保持一致

建议添加 JSDoc 注释来说明新的参数类型及其用法。

/**
+ * 设置视图tab的徽章
+ * @param badge - 徽章配置对象,可包含徽章内容、提示文本等
+ * @example
+ * setBadge({
+ *   badge: '99+',
+ *   badgeTooltip: '未读消息数量'
+ * });
+ */
setBadge(badge: BadgeWidget | undefined) {
packages/main-layout/src/browser/accordion/section.view.tsx (3)

20-21: 建议优化导入路径

当前使用多层相对路径(../../../../)导入 BadgeWidget 不利于代码维护。建议使用包导入方式替代。

-import { BadgeWidget } from '../../../../core-browser/src/layout';
+import { BadgeWidget } from '@opensumi/ide-core-browser/lib/layout';

64-64: 建议完善属性文档注释

当前的注释 "Panel Badge" 过于简单。建议添加更详细的说明,包括 BadgeWidget 的用途和可能的值。

建议更新注释为:

-  // Panel Badge
+  // 面板徽章,用于显示状态或计数信息
+  // 类型:BadgeWidget 包含徽章的文本、样式等属性

191-191: 建议增强徽章渲染功能

当前实现只使用了 BadgeWidget 的 badge 属性。考虑以下建议:

  1. 可以利用 BadgeWidget 的其他属性来支持更丰富的徽章展示
  2. 建议添加徽章的悬停提示(tooltip)
  3. 考虑添加徽章的可访问性支持(ARIA 属性)
-            {metadata.badge && <div className={styles_section_badge}>{metadata.badge.badge}</div>}
+            {metadata.badge && (
+              <div 
+                className={cls(styles_section_badge, metadata.badge.className)}
+                title={metadata.badge.tooltip}
+                aria-label={metadata.badge.ariaLabel || metadata.badge.tooltip}
+              >
+                {metadata.badge.badge}
+              </div>
+            )}
packages/extension/src/common/vscode/treeview.ts (2)

144-148: 建议补充 badge 属性的使用示例

badge 属性的文档注释虽然说明了如何移除徽章,但建议添加一个使用示例来展示如何正确设置徽章。

建议在注释中添加如下示例:

   /**
    * TreeView 要显示的徽标
    * 要删除徽标,请设置为undefined
+   * 
+   * @example
+   * ```typescript
+   * treeView.badge = {
+   *   tooltip: '未读消息',
+   *   value: 3
+   * };
+   * ```
    */

161-174: ViewBadge 接口设计合理,建议增加值的范围约束

接口定义简洁明确,包含了必要的 tooltip 和 value 属性。但建议考虑添加对 value 属性的范围限制说明。

建议在注释中补充 value 的有效范围说明:

   /**
    * 徽标中显示的值
+   * @remarks
+   * - 值应为正整数
+   * - 建议限制在合理范围内(如 0-99)以确保显示效果
    */
   readonly value: number;
packages/main-layout/src/browser/tabbar/bar.view.tsx (3)

231-234: 优化徽章属性的条件检查逻辑

当前的条件检查存在冗余:badge && badge?.badge。可以简化为单个可选链操作符。

建议应用以下更改:

-        component.options?.badge &&
-        component.options.badge?.badge && (
+        component.options?.badge?.badge && (

257-257: 简化徽章值的条件渲染

当前的三元运算符使用方式不够简洁,undefined 作为备选值是多余的。

建议应用以下更改:

-          {component.options.badge ? component.options.badge.badge : undefined}
+          {component.options.badge?.badge}

徽章结构变更需要进一步统一

代码分析显示徽章(badge)的使用模式存在不一致:

  • packages/main-layout/src/browser/tabbar/bar.view.tsx 中使用了新的嵌套结构 badge?.badge
  • 其他组件(如 search、scm、markers 等)仍在直接访问 badge 属性

需要注意的关键位置:

  • packages/scm/src/browser/components/scm-resource-tree/scm-tree-node.view.tsx
  • packages/search/src/browser/tree/search-node.tsx
  • packages/markers/src/browser/tree/marker-node.tsx
  • packages/opened-editor/src/browser/opened-editor-node.tsx

建议统一更新这些组件以使用新的徽章结构,确保整个代码库的一致性。

🔗 Analysis chain

Line range hint 231-257: 验证徽章结构的一致性使用

代码改动显示徽章属性已更改为使用新的 BadgeWidget 接口。需要确保整个代码库中的徽章处理逻辑都已更新。

运行以下脚本来验证徽章属性的使用情况:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:检查代码库中徽章属性的使用模式

# 搜索所有直接访问 badge 属性的实例
echo "检查直接访问 badge 属性的实例:"
rg -t typescript -t tsx "\.badge\s*[^?.]" 

# 搜索使用新的嵌套结构的实例
echo "检查使用新的嵌套结构的实例:"
rg -t typescript -t tsx "\.badge\?.badge"

Length of output: 400


Script:

#!/bin/bash
# 修正:使用正确的文件类型标识
echo "检查直接访问 badge 属性的实例:"
rg --type-add 'ts:*.{ts,tsx}' -t ts "\.badge\s*[^?.]"

echo "检查使用新的嵌套结构的实例:"
rg --type-add 'ts:*.{ts,tsx}' -t ts "\.badge\?.badge"

# 补充:检查所有包含 badge 的代码
echo "检查所有包含 badge 的代码(带上下文):"
rg --type-add 'ts:*.{ts,tsx}' -t ts -A 2 -B 2 "\.badge"

Length of output: 26650

packages/opened-editor/src/browser/services/opened-editor-model.service.ts (1)

474-476: 徽章对象结构优化

代码改动将简单的徽章字符串更新为结构化的对象,这样的改动更加符合 VSCode API TreeView.badge 的规范。实现方式清晰且具有良好的扩展性。

建议考虑以下优化点:

  1. 为徽章对象定义专门的接口类型,提高代码的可维护性
  2. 考虑将徽章相关的字符串常量提取到配置文件中
+interface BadgeInfo {
+  badge: number;
+  badgeTooltip: string;
+}

-const badge = dirtyCount > 0 ? { badge: dirtyCount, badgeTooltip: dirtyMsg } : undefined;
+const badge: BadgeInfo | undefined = dirtyCount > 0 
+  ? { badge: dirtyCount, badgeTooltip: dirtyMsg } 
+  : undefined;
packages/extension/src/browser/vscode/api/main.thread.treeview.ts (1)

271-276: 建议增强错误处理机制

建议添加以下改进:

  1. 对无效的 treeViewId 进行错误处理
  2. 为 badge 对象的转换添加类型安全检查
  async $setBadge(treeViewId: string, badge: ViewBadge | undefined) {
+   if (!treeViewId) {
+     throw new Error('TreeView ID is required');
+   }
    const handler = this.mainLayoutService.getTabbarHandler(treeViewId);
    if (handler) {
+     const badgeData = badge && {
+       badge: badge.value,
+       badgeTooltip: badge.tooltip
+     };
-     handler.setBadge(badge ? { badge: badge.value, badgeTooltip: badge.tooltip } : undefined);
+     handler.setBadge(badgeData);
    }
  }
packages/main-layout/src/browser/accordion/accordion.service.ts (1)

Line range hint 220-227: 方法实现良好,建议改进错误消息!

updateViewBadge 方法的实现遵循了现有的更新方法模式,但错误日志消息可以更具体。

建议修改错误消息以更准确地反映操作类型:

-      this.logger.error(`No target view \`${viewId}\` found, unable to update accordion message`);
+      this.logger.error(`No target view \`${viewId}\` found, unable to update accordion badge`);
packages/extension/src/hosted/api/vscode/ext.host.treeview.ts (1)

326-326: 建议优化徽章相关的代码实现

当前实现可以进行以下优化:

  1. 私有属性 _badge 的初始化和类型声明可以合并
  2. 在 setter 中可以添加值相等性检查,避免不必要的更新

建议按照以下方式重构代码:

- private _badge?: ViewBadge = undefined;
+ private _badge?: ViewBadge;

  set badge(badge: ViewBadge | undefined) {
+   if (this._badge?.value === badge?.value && this._badge?.tooltip === badge?.tooltip) {
+     return;
+   }
    this._badge = badge;
    this.proxy.$setBadge(this.treeViewId, badge ? { value: badge.value, tooltip: badge.tooltip } : undefined);
  }

Also applies to: 503-509

packages/file-tree-next/src/browser/file-tree.service.ts (2)

854-859: 建议添加方法说明文档

这是一个公共方法,建议添加 JSDoc 注释说明其用途、返回值含义以及可能的副作用。

建议添加如下文档:

+ /**
+  * 计算文件树中的节点总数(不包括目录和 .sumi 文件)
+  * @returns {number} 符合条件的节点数量
+  */
  public calcNodeCount = () => {
    if (this._root) {
      return this.recursionCalcNodeCount(this._root);
    }
    return 0;
  };

854-879: 性能优化建议

对于大型文件树,递归遍历可能会导致性能问题。建议考虑以下优化:

  1. 添加缓存机制,避免重复计算
  2. 考虑使用迭代方式代替递归
  3. 实现增量更新机制,仅在文件树变化时更新计数
packages/main-layout/src/browser/tabbar/tabbar.service.ts (2)

46-46: 导入语句位置需要调整

建议将 BadgeWidget 的导入语句移动到其他 @opensumi 相关导入的分组中,以保持导入语句的组织一致性。

- import { BadgeWidget } from '../../../../core-browser/src/layout';
+ import {
+   CommandRegistry,
+   ComponentRegistryInfo,
+   ComponentRegistryProvider,
+   Deferred,
+   DisposableCollection,
+   Emitter,
+   Event,
+   IContextKey,
+   IContextKeyService,
+   IScopedContextKeyService,
+   KeybindingRegistry,
+   ResizeEvent,
+   SlotLocation,
+   ViewContextKeyRegistry,
+   WithEventBus,
+   createFormatLocalizedStr,
+   fastdom,
+   formatLocalize,
+   getTabbarCtxKey,
+   isDefined,
+   isUndefined,
+   localize,
+   toDisposable,
+   BadgeWidget,
+ } from '@opensumi/ide-core-browser';

Line range hint 197-202: 更新徽章方法的实现看起来不错

方法实现正确地处理了新的 BadgeWidget 类型,并保持了与现有组件更新逻辑的一致性。

建议添加以下改进:

  1. 添加参数类型注释以提高代码可读性
  2. 添加空值检查以提高代码健壮性
- updateBadge(containerId: string, value: BadgeWidget | undefined) {
+ updateBadge(containerId: string, value: BadgeWidget | undefined): void {
+   if (!containerId) {
+     return;
+   }
    const component = this.getContainer(containerId);
    if (component && component.options) {
      component.options.badge = value;
    }
    component?.fireChange(component);
  }
🧰 Tools
🪛 Biome (1.9.4)

[error] 199-200: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

packages/file-tree-next/src/browser/file-tree-contribution.ts (2)

175-183: 建议添加错误处理机制

事件监听器的实现逻辑正确,但建议添加错误处理来提高代码的健壮性。

建议按如下方式重构:

 this._disposables.add(
   Event.any<any>(this.fileTreeEventService.onDidChange)(() => {
+    try {
       const dirtyCount = this.fileTreeService.calcNodeCount();
       const accordionService = this.mainLayoutService.getAccordionService(EXPLORER_CONTAINER_ID);
       const badge = dirtyCount > 0 ? { badge: dirtyCount, badgeTooltip: dirtyCount + '' } : undefined;
       accordionService.updateViewBadge('file-explorer', badge);
+    } catch (error) {
+      console.error('更新文件树徽章时发生错误:', error);
+    }
   }),
 );

1281-1282: 空方法需要实现或移除

setExplorerTabBarBadge 方法目前为空,且在代码中没有被调用。建议:

  1. 如果计划在后续实现,请添加 TODO 注释说明用途
  2. 如果不需要,建议移除这个空方法
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e0d9eb7 and 4281a61.

📒 Files selected for processing (15)
  • packages/core-browser/src/layout/layout.interface.ts (3 hunks)
  • packages/extension/src/browser/vscode/api/main.thread.treeview.ts (2 hunks)
  • packages/extension/src/common/vscode/treeview.ts (3 hunks)
  • packages/extension/src/hosted/api/vscode/ext.host.treeview.ts (4 hunks)
  • packages/file-tree-next/src/browser/file-tree-contribution.ts (5 hunks)
  • packages/file-tree-next/src/browser/file-tree.service.ts (1 hunks)
  • packages/file-tree-next/src/browser/services/file-tree-event.service.ts (1 hunks)
  • packages/main-layout/src/browser/accordion/accordion.service.ts (3 hunks)
  • packages/main-layout/src/browser/accordion/section.view.tsx (3 hunks)
  • packages/main-layout/src/browser/tabbar-handler.ts (2 hunks)
  • packages/main-layout/src/browser/tabbar/bar.view.tsx (2 hunks)
  • packages/main-layout/src/browser/tabbar/tabbar.service.ts (2 hunks)
  • packages/opened-editor/src/browser/services/opened-editor-model.service.ts (1 hunks)
  • packages/types/vscode/typings/vscode.d.ts (2 hunks)
  • packages/types/vscode/typings/vscode.window.d.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/file-tree-next/src/browser/services/file-tree-event.service.ts

[error] 10-12: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

🔇 Additional comments (14)
packages/file-tree-next/src/browser/services/file-tree-event.service.ts (2)

1-5: 类的设计和继承结构合理

类的设计遵循了依赖注入模式,并正确继承了 WithEventBus 以支持事件处理功能。


6-8: 事件发射器实现规范

事件发射器的实现遵循了标准模式,通过私有的 _onDidChange 发射器和公开的 onDidChange 事件提供了良好的封装。

packages/extension/src/common/vscode/treeview.ts (2)

24-24: 新增的 $setBadge 方法签名正确且符合预期

方法签名清晰地定义了所需参数,包括树视图ID和徽章对象。参数类型的使用恰当,特别是将 badge 参数设置为可选(undefined)以支持徽章的移除功能。


161-174: 验证实现是否与 VSCode API 规范一致

请确保 ViewBadge 接口的实现与 VSCode API 的规范保持一致。

✅ Verification successful

ViewBadge 接口实现与 VSCode API 完全一致

从 VSCode 类型定义文件中可以看到,当前实现的 ViewBadge 接口与 VSCode 的原生定义完全匹配:

  • tooltip 属性:用于在徽标工具提示中显示的标签
  • value 属性:用于在徽标中显示的数值
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索 VSCode 相关的类型定义文件,确认接口实现的一致性
rg -t ts "interface.*Badge" 
rg -t ts "TreeView.*badge"

Length of output: 292


Script:

#!/bin/bash
# 查看 VSCode 类型定义中的 ViewBadge 接口具体实现
cat packages/types/vscode/typings/vscode.d.ts | grep -A 10 "interface ViewBadge"

# 查找是否有其他相关的 badge 定义
rg -t ts "badge.*:" -A 2

Length of output: 17138

packages/extension/src/browser/vscode/api/main.thread.treeview.ts (2)

45-45: 导入声明看起来没有问题!

ViewBadge 类型的导入位置正确,与相关类型放在一起,符合代码组织结构。


271-276: 实现逻辑正确,符合预期!

方法实现遵循了 RPC 方法的命名约定,并正确处理了 badge 的转换逻辑。

packages/main-layout/src/browser/accordion/accordion.service.ts (2)

46-46: 导入声明看起来不错!

BadgeWidget 的导入位置正确,并且遵循了项目的导入约定。


61-61: 接口更新符合预期!

AccordionViewChangeEvent 接口中 badge 属性类型的更新与 VSCode API 的 TreeView.badge 保持一致。

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

28-28: 导入 ViewBadge 类型

导入了必要的 ViewBadge 类型以支持树视图徽章功能。


126-131: TreeView 接口中的徽章属性实现正确

徽章属性的 getter 和 setter 实现符合预期:

  • getter 返回当前徽章状态
  • setter 更新徽章并同步到主线程
packages/types/vscode/typings/vscode.window.d.ts (1)

872-876: 代码变更看起来不错!

badge 属性的类型定义和文档注释都很清晰。属性被正确标记为可选,并且注释中说明了如何移除徽章。

让我们验证一下 ViewBadge 接口的定义:

✅ Verification successful

ViewBadge 接口已正确定义,代码变更完全符合预期!

packages/types/vscode/typings/vscode.d.ts 中找到了 ViewBadge 接口的定义,它包含了所有必需的属性:

  • tooltip: 用于显示徽章的工具提示文本
  • value: 用于显示徽章的数值

这个接口定义与 WebviewView 中新增的 badge 属性完全匹配,确保了类型安全性。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:查找 ViewBadge 接口的定义

# 在代码库中搜索 ViewBadge 接口定义
ast-grep --pattern 'interface ViewBadge {
  $$$
}'

Length of output: 1544

packages/file-tree-next/src/browser/file-tree-contribution.ts (1)

132-134: 依赖注入看起来没有问题!

FileTreeEventService 的注入遵循了类中既有的依赖注入模式。

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

1875-1879: 代码实现正确且符合规范!

TreeView 接口中新增的 badge 属性实现合理,类型定义清晰,文档注释完整。


1897-1912: 接口设计合理且完整!

ViewBadge 接口的设计考虑周全:

  • tooltip 属性支持无障碍访问和提示文本
  • value 属性使用 number 类型适合显示徽章计数

Comment on lines +10 to +12
constructor() {
super();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

移除不必要的构造函数

当前的构造函数仅调用父类构造函数,没有其他逻辑,可以安全移除。

建议应用以下修改:

-  constructor() {
-    super();
-  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constructor() {
super();
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 10-12: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

Comment on lines +861 to +879
private recursionCalcNodeCount = (root) => {
if (root) {
if (root.tooltip.includes('.sumi')) {
return 0;
}
let count = 0;
if (!root.filestat.isDirectory) {
++count;
} else {
if (root.children) {
for (const node of root.children) {
count += this.recursionCalcNodeCount(node);
}
}
}
return count;
}
return 0;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

需要添加类型信息和改进实现

建议对递归方法进行以下改进:

  1. 添加参数类型声明
  2. 将魔术字符串提取为常量
  3. 使用 reduce 简化实现
  4. 添加错误处理

建议按照以下方式重构代码:

+ private static readonly SUMI_FILTER = '.sumi';

- private recursionCalcNodeCount = (root) => {
+ private recursionCalcNodeCount = (root: Directory | File): number => {
+   try {
      if (root) {
-       if (root.tooltip.includes('.sumi')) {
+       if (root.tooltip.includes(FileTreeService.SUMI_FILTER)) {
          return 0;
        }
-       let count = 0;
        if (!root.filestat.isDirectory) {
-         ++count;
+         return 1;
        } else if (root.children) {
-         for (const node of root.children) {
-           count += this.recursionCalcNodeCount(node);
-         }
+         return root.children.reduce((sum, node) => 
+           sum + this.recursionCalcNodeCount(node), 0
+         );
        }
-       return count;
+       return 0;
      }
      return 0;
+   } catch (error) {
+     this.logger.error('Error calculating node count:', error);
+     return 0;
+   }
  };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private recursionCalcNodeCount = (root) => {
if (root) {
if (root.tooltip.includes('.sumi')) {
return 0;
}
let count = 0;
if (!root.filestat.isDirectory) {
++count;
} else {
if (root.children) {
for (const node of root.children) {
count += this.recursionCalcNodeCount(node);
}
}
}
return count;
}
return 0;
};
private static readonly SUMI_FILTER = '.sumi';
private recursionCalcNodeCount = (root: Directory | File): number => {
try {
if (root) {
if (root.tooltip.includes(FileTreeService.SUMI_FILTER)) {
return 0;
}
if (!root.filestat.isDirectory) {
return 1;
} else if (root.children) {
return root.children.reduce((sum, node) =>
sum + this.recursionCalcNodeCount(node), 0
);
}
return 0;
}
return 0;
} catch (error) {
this.logger.error('Error calculating node count:', error);
return 0;
}
};


private recursionCalcNodeCount = (root) => {
if (root) {
if (root.tooltip.includes('.sumi')) {
Copy link
Member

Choose a reason for hiding this comment

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

这是统计什么数量?为什么包含 。sumi 就返回 0 ?

super();
}

@OnEvent(FilesChangeEvent)
Copy link
Member

Choose a reason for hiding this comment

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

这里的事件没有啥引用?多余的

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.

3 participants