test view

This commit is contained in:
Sandeep Somavarapu
2018-10-29 19:09:34 +01:00
parent a359721cf3
commit 26a092783d
3 changed files with 111 additions and 3 deletions

View File

@ -7,7 +7,6 @@
"engines": {
"vscode": "^1.25.0"
},
"enableProposedApi": true,
"categories": [
"Other"
],
@ -16,8 +15,10 @@
"onView:ftpExplorer",
"onView:jsonOutline",
"onView:fileExplorer",
"onView:testView",
"onLanguage:json",
"onLanguage:jsonc"
"onLanguage:jsonc",
"onCommand:sample.reveal"
],
"main": "./out/extension",
"contributes": {
@ -50,6 +51,10 @@
{
"id": "fileExplorer",
"name": "File Explorer"
},
{
"id": "testView",
"name": "Test View"
}
]
},
@ -117,6 +122,10 @@
{
"command": "fileExplorer.openFile",
"title": "Open File"
},
{
"command": "testView.reveal",
"title": "Reveal"
}
],
"menus": {

View File

@ -6,6 +6,7 @@ import { DepNodeProvider } from './nodeDependencies'
import { JsonOutlineProvider } from './jsonOutline'
import { FtpExplorer } from './ftpExplorer'
import { FileExplorer } from './fileExplorer';
import { TestView } from './testView';
export function activate(context: vscode.ExtensionContext) {
@ -27,4 +28,7 @@ export function activate(context: vscode.ExtensionContext) {
// Samples of `window.createView`
new FtpExplorer(context);
new FileExplorer(context);
}
// Test View
new TestView(context);
}

View File

@ -0,0 +1,95 @@
import * as vscode from 'vscode';
export class TestView {
constructor(context: vscode.ExtensionContext) {
const view = vscode.window.createTreeView('testView', { treeDataProvider: aNodeWithIdTreeDataProvider() });
vscode.commands.registerCommand('testView.reveal', async () => {
const key = await vscode.window.showInputBox();
if (key) {
await view.reveal({ key }, { focus: true, select: false });
}
});
}
}
const tree = {
'a': {
'aa': {
'aaa': {
'aaaa': {
'aaaaa': {
'aaaaaa': {
}
}
}
}
},
'ab': {}
},
'b': {
'ba': {},
'bb': {}
}
};
let nodes = {};
function aNodeWithIdTreeDataProvider(): vscode.TreeDataProvider<{ key: string }> {
return {
getChildren: (element: { key: string }): { key: string }[] => {
return getChildren(element ? element.key : undefined).map(key => getNode(key));
},
getTreeItem: (element: { key: string }): vscode.TreeItem => {
const treeItem = getTreeItem(element.key);
treeItem.id = element.key;
return treeItem;
},
getParent: ({ key }: { key: string }): { key: string } => {
const parentKey = key.substring(0, key.length - 1);
return parentKey ? new Key(parentKey) : void 0;
}
};
}
function getChildren(key: string): string[] {
if (!key) {
return Object.keys(tree);
}
let treeElement = getTreeElement(key);
if (treeElement) {
return Object.keys(treeElement);
}
return [];
}
function getTreeItem(key: string): vscode.TreeItem {
const treeElement = getTreeElement(key);
return {
label: key,
tooltip: `Tooltip for ${key}`,
collapsibleState: treeElement && Object.keys(treeElement).length ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None
};
}
function getTreeElement(element): any {
let parent = tree;
for (let i = 0; i < element.length; i++) {
parent = parent[element.substring(0, i + 1)];
if (!parent) {
return null;
}
}
return parent;
}
function getNode(key: string): { key: string } {
if (!nodes[key]) {
nodes[key] = new Key(key);
}
return nodes[key];
}
class Key {
constructor(readonly key: string) { }
}