Skip to content

Commit

Permalink
refactor(workspace-tree): Future proof icon path resolution for minif…
Browse files Browse the repository at this point in the history
…ied pakcage

Current resource path resolution is using the source file as the base
directory, this would fail if we minify the package into single js.

Use extension path as the base directory is more reliable: it is also
working for the current packaging, and we just have to make sure the
resource layout of the minified package remains the same.
  • Loading branch information
cwahbong committed Oct 24, 2024
1 parent b01fdbc commit 7aa2938
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import { activateWrapperCommands } from "./bazel_wrapper_commands";
* @param context The extension context.
*/
export async function activate(context: vscode.ExtensionContext) {
const workspaceTreeProvider = new BazelWorkspaceTreeProvider();
const workspaceTreeProvider =
BazelWorkspaceTreeProvider.fromExtensionContext(context);
context.subscriptions.push(workspaceTreeProvider);

const codeLensProvider = new BazelBuildCodeLensProvider(context);
Expand Down
42 changes: 42 additions & 0 deletions src/extension/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as path from "path";
import * as vscode from "vscode";

/**
* Helper functions for getting the resource bundled inside this extension.
*/
export class Resources {
public static fromExtensionContext(
context: vscode.ExtensionContext,
): Resources {
return new Resources(context.extensionPath);
}

/**
* @param extensionPath The extension path, usually from the extension
* context.
*/
constructor(private readonly extensionPath: string) {}

/**
* Returns the icon path in string.
*
* @param name The icon file name.
*/
public getIconPath(name: string): string {
return path.join(this.extensionPath, "icons", `${name}.svg`);
}
}
4 changes: 2 additions & 2 deletions src/workspace-tree/bazel_package_tree_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export class BazelPackageTreeItem
return this.packagePath.substring(this.parentPackagePath.length + 1);
}

public getIcon(): vscode.ThemeIcon {
return vscode.ThemeIcon.Folder;
public getIconName(): undefined {
return undefined;
}

public getTooltip(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/workspace-tree/bazel_target_tree_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class BazelTargetTreeItem
return `${targetName} (${this.target.rule.ruleClass})`;
}

public getIcon(): vscode.ThemeIcon | string {
public getIconName(): string | undefined {
return getBazelRuleIcon(this.target);
}

Expand Down
4 changes: 2 additions & 2 deletions src/workspace-tree/bazel_tree_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export interface IBazelTreeItem {
/** Returns the text label of the tree item. */
getLabel(): string;

/** Returns the icon that should be shown next to the tree item. */
getIcon(): vscode.ThemeIcon | string | undefined;
/** Returns the icon name that should be shown next to the tree item. */
getIconName(): string | undefined;

/**
* Returns the tooltip that should be displayed when the user hovers over the
Expand Down
7 changes: 3 additions & 4 deletions src/workspace-tree/bazel_workspace_folder_tree_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// limitations under the License.

import * as vscode from "vscode";
import { BazelWorkspaceInfo } from "../bazel";
import { BazelQuery } from "../bazel";
import { BazelWorkspaceInfo, BazelQuery } from "../bazel";
import { getDefaultBazelExecutablePath } from "../extension/configuration";
import { blaze_query } from "../protos";
import { BazelPackageTreeItem } from "./bazel_package_tree_item";
Expand Down Expand Up @@ -42,8 +41,8 @@ export class BazelWorkspaceFolderTreeItem implements IBazelTreeItem {
return this.workspaceInfo.workspaceFolder.name;
}

public getIcon(): vscode.ThemeIcon {
return vscode.ThemeIcon.Folder;
public getIconName(): undefined {
return undefined;
}

public getTooltip(): string {
Expand Down
25 changes: 23 additions & 2 deletions src/workspace-tree/bazel_workspace_tree_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as vscode from "vscode";
import { BazelWorkspaceInfo } from "../bazel";
import { IBazelTreeItem } from "./bazel_tree_item";
import { BazelWorkspaceFolderTreeItem } from "./bazel_workspace_folder_tree_item";
import { Resources } from "../extension/resources";

/**
* Provides a tree of Bazel build packages and targets for the VS Code explorer
Expand All @@ -34,12 +35,20 @@ export class BazelWorkspaceTreeProvider

private disposables: vscode.Disposable[] = [];

public static fromExtensionContext(
context: vscode.ExtensionContext,
): BazelWorkspaceTreeProvider {
return new BazelWorkspaceTreeProvider(
Resources.fromExtensionContext(context),
);
}

/**
* Initializes a new tree provider with the given extension context.
*
* @param context The VS Code extension context.
*/
constructor() {
constructor(private readonly resources: Resources) {
const buildFilesWatcher = vscode.workspace.createFileSystemWatcher(
"**/{BUILD,BUILD.bazel}",
false,
Expand Down Expand Up @@ -87,6 +96,18 @@ export class BazelWorkspaceTreeProvider
return Promise.resolve([]);
}

private getIcon(element: IBazelTreeItem): string | vscode.ThemeIcon {
const iconName = element.getIconName();
if (iconName === undefined) {
if (element.mightHaveChildren()) {
return vscode.ThemeIcon.Folder;
} else {
return vscode.ThemeIcon.File;
}
}
return this.resources.getIconPath(iconName);
}

public getTreeItem(element: IBazelTreeItem): vscode.TreeItem {
const label = element.getLabel();
const collapsibleState = element.mightHaveChildren()
Expand All @@ -95,7 +116,7 @@ export class BazelWorkspaceTreeProvider

const treeItem = new vscode.TreeItem(label, collapsibleState);
treeItem.contextValue = element.getContextValue();
treeItem.iconPath = element.getIcon();
treeItem.iconPath = this.getIcon(element);
treeItem.tooltip = element.getTooltip();
treeItem.command = element.getCommand();
return treeItem;
Expand Down
10 changes: 2 additions & 8 deletions src/workspace-tree/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import * as path from "path";
import * as vscode from "vscode";
import { blaze_query } from "../protos";

/**
Expand Down Expand Up @@ -60,7 +58,7 @@ const SPECIFIC_RULE_CLASS_ICONS: Record<string, string> = {
*/
export function getBazelRuleIcon(
target: blaze_query.ITarget,
): string | vscode.ThemeIcon {
): string | undefined {
const ruleClass = target.rule.ruleClass;
let iconName = SPECIFIC_RULE_CLASS_ICONS[ruleClass];
if (!iconName) {
Expand All @@ -74,9 +72,5 @@ export function getBazelRuleIcon(
iconName = "test";
}
}
if (iconName) {
return path.join(__dirname, "../../../icons", `${iconName}.svg`);
} else {
return vscode.ThemeIcon.File;
}
return iconName;
}

0 comments on commit 7aa2938

Please sign in to comment.