|
1 | 1 | import ms from "ms"; |
2 | | -import { StatusBarAlignment, window } from "vscode"; |
3 | | -import type { ExtensionContext } from "vscode"; |
| 2 | +import { |
| 3 | + EventEmitter, |
| 4 | + StatusBarAlignment, |
| 5 | + ThemeIcon, |
| 6 | + TreeItem, |
| 7 | + TreeItemCollapsibleState, |
| 8 | + window, |
| 9 | +} from "vscode"; |
| 10 | +import type { |
| 11 | + ExtensionContext, |
| 12 | + ProviderResult, |
| 13 | + TreeDataProvider, |
| 14 | + Event, |
| 15 | +} from "vscode"; |
4 | 16 |
|
5 | 17 | import configureAws from "./plugins/configure-aws.ts"; |
6 | 18 | import logs from "./plugins/logs.ts"; |
@@ -107,8 +119,99 @@ export async function activate(context: ExtensionContext) { |
107 | 119 | timeTracker, |
108 | 120 | }); |
109 | 121 | }); |
| 122 | + |
| 123 | + const provider = new ExampleTreeDataProvider(); |
| 124 | + |
| 125 | + // Register the provider under the view ID defined in package.json |
| 126 | + const treeView = window.createTreeView("localstackTreeView", { |
| 127 | + treeDataProvider: provider, |
| 128 | + showCollapseAll: false, |
| 129 | + }); |
110 | 130 | } |
111 | 131 |
|
112 | 132 | export async function deactivate() { |
113 | 133 | await plugins.deactivate(); |
114 | 134 | } |
| 135 | + |
| 136 | +class ExampleTreeDataProvider implements TreeDataProvider<TreeItem> { |
| 137 | + private readonly _onDidChangeTreeData = new EventEmitter< |
| 138 | + ExampleTreeItem | undefined | void |
| 139 | + >(); |
| 140 | + |
| 141 | + readonly onDidChangeTreeData: Event<ExampleTreeItem | undefined | void> = |
| 142 | + this._onDidChangeTreeData.event; |
| 143 | + |
| 144 | + // Toggle to show one or two items for demonstration |
| 145 | + private showTwoItems = true; |
| 146 | + |
| 147 | + /** |
| 148 | + * Triggers a refresh of the tree view. |
| 149 | + */ |
| 150 | + refresh(): void { |
| 151 | + this.showTwoItems = !this.showTwoItems; |
| 152 | + this._onDidChangeTreeData.fire(); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns children of a given element or root. |
| 157 | + * Root returns 1–2 items; no nested children in this minimal example. |
| 158 | + */ |
| 159 | + getChildren(element?: ExampleTreeItem): ProviderResult<TreeItem[]> { |
| 160 | + if (element) { |
| 161 | + // No nested children in this example |
| 162 | + return []; |
| 163 | + } |
| 164 | + |
| 165 | + const items: TreeItem[] = []; |
| 166 | + const one = new ExampleTreeItem("Item One", "one"); |
| 167 | + items.push(one); |
| 168 | + |
| 169 | + if (this.showTwoItems) { |
| 170 | + const two = new ExampleTreeItem("Item Two", "two"); |
| 171 | + items.push(two); |
| 172 | + } |
| 173 | + |
| 174 | + const instanceEndpoint = new TreeItem( |
| 175 | + "Instance endpoint", |
| 176 | + TreeItemCollapsibleState.None, |
| 177 | + ); |
| 178 | + instanceEndpoint.tooltip = "tooltip"; |
| 179 | + instanceEndpoint.description = "https://localhost.localstack.cloud:4566"; |
| 180 | + instanceEndpoint.iconPath = new ThemeIcon("globe"); |
| 181 | + items.push(instanceEndpoint); |
| 182 | + |
| 183 | + return items; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Returns a TreeItem to render in the view. |
| 188 | + */ |
| 189 | + getTreeItem(element: ExampleTreeItem): TreeItem { |
| 190 | + return element; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | + * A clickable TreeItem that runs the example.openItem command when selected. |
| 196 | + */ |
| 197 | +class ExampleTreeItem extends TreeItem { |
| 198 | + constructor( |
| 199 | + label: string, |
| 200 | + readonly id: string, |
| 201 | + ) { |
| 202 | + super(label); |
| 203 | + this.id = id; |
| 204 | + this.tooltip = `Click to open "${label}"`; |
| 205 | + this.description = id; |
| 206 | + this.iconPath = new ThemeIcon("localstack-logo"); |
| 207 | + this.command = { |
| 208 | + command: "example.openItem", |
| 209 | + title: "Open Item", |
| 210 | + arguments: [this], |
| 211 | + }; |
| 212 | + // No collapsible state, these are leaf nodes |
| 213 | + this.collapsibleState = TreeItemCollapsibleState.None; |
| 214 | + // Optional: contextValue enables context-menu targeting in contributes.menus |
| 215 | + this.contextValue = "exampleItem"; |
| 216 | + } |
| 217 | +} |
0 commit comments