Skip to content

Commit 101bbd2

Browse files
committed
wip: add views
1 parent 587ee13 commit 101bbd2

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949
}
5050
}
5151
},
52+
"viewsContainers": {
53+
"activitybar": [
54+
{
55+
"id": "localstackActivityBar",
56+
"title": "LocalStack",
57+
"icon": "resources/icons/localstack.svg"
58+
}
59+
]
60+
},
61+
"views": {
62+
"localstackActivityBar": [
63+
{
64+
"id": "localstackTreeView",
65+
"name": "Items"
66+
}
67+
]
68+
},
5269
"commands": [
5370
{
5471
"command": "localstack.setup",

src/extension.ts

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
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";
416

517
import configureAws from "./plugins/configure-aws.ts";
618
import logs from "./plugins/logs.ts";
@@ -107,8 +119,99 @@ export async function activate(context: ExtensionContext) {
107119
timeTracker,
108120
});
109121
});
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+
});
110130
}
111131

112132
export async function deactivate() {
113133
await plugins.deactivate();
114134
}
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

Comments
 (0)