Skip to content

Commit

Permalink
[OneExplorer] Reveal focused cfg file in OneExplorer (#1264)
Browse files Browse the repository at this point in the history
* [OneExplorer] Reveal focused cfg file in OneExplorer

This commit reveals focused cfg file on CfgEditor in OneExplorer.

ONE-vscode-DCO-1.0-Signed-off-by: Dayoung Lee <[email protected]>

* Update src/OneExplorer/OneExplorer.ts

Co-authored-by: Dayoung Lee <[email protected]>
  • Loading branch information
dayo09 and dayo09 authored Sep 14, 2022
1 parent 5b46d8a commit 00d72f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@
"category": "ONE"
},
{
"command": "one.explorer.reveal",
"command": "one.explorer.revealInOneExplorer",
"title": "Reveal in ONE Explorer View",
"category": "ONE"
},
{
"command": "one.explorer.revealInDefaultExplorer",
"title": "Reveal in Explorer View",
"category": "ONE"
},
Expand Down Expand Up @@ -252,7 +257,7 @@
"group": "3_open@1"
},
{
"command": "one.explorer.reveal",
"command": "one.explorer.revealInDefaultExplorer",
"when": "view == OneExplorerView && viewItem != directory",
"group": "3_open@3"
},
Expand Down Expand Up @@ -306,7 +311,7 @@
"when": "false"
},
{
"command": "one.explorer.reveal",
"command": "one.explorer.revealInDefaultExplorer",
"when": "false"
},
{
Expand Down
3 changes: 3 additions & 0 deletions src/CfgEditor/CfgEditorPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export class CfgEditorPanel implements vscode.CustomTextEditorProvider {
});

webviewPanel.onDidChangeViewState(e => {
if (webviewPanel.visible) {
vscode.commands.executeCommand('one.explorer.revealInOneExplorer', document.fileName);
}
vscode.commands.executeCommand('setContext', CfgEditorPanel.viewType, webviewPanel.visible);
}, null, this._disposables);

Expand Down
47 changes: 38 additions & 9 deletions src/OneExplorer/OneExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {

private fileWatcher = vscode.workspace.createFileSystemWatcher(`**/*`);

private tree: DirectoryNode|undefined;
private _tree: DirectoryNode|undefined;
private _nodeMap: Map<string, Node> = new Map<string, Node>();

public static didHideExtra: boolean = false;

public static register(context: vscode.ExtensionContext) {
Expand All @@ -493,20 +495,30 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {

const provider = new OneTreeDataProvider(workspaceRoot, context.extension.extensionKind);

const _treeView = vscode.window.createTreeView(
'OneExplorerView',
{treeDataProvider: provider, showCollapseAll: true, canSelectMany: true});

let registrations = [
provider.fileWatcher.onDidCreate(() => provider.refresh()),
provider.fileWatcher.onDidChange(() => provider.refresh()),
provider.fileWatcher.onDidDelete(() => provider.refresh()),
vscode.window.createTreeView(
'OneExplorerView',
{treeDataProvider: provider, showCollapseAll: true, canSelectMany: true}),
_treeView,
vscode.commands.registerCommand(
'one.explorer.revealInOneExplorer',
(path: string) => {
const node = provider._nodeMap.get(path);
if (node) {
_treeView ?.reveal(node, {select: true, focus: true, expand: true});
}
}),
vscode.commands.registerCommand(
'one.explorer.openAsText',
(node: Node) => {
vscode.commands.executeCommand('vscode.openWith', node.uri, 'default');
}),
vscode.commands.registerCommand(
'one.explorer.reveal',
'one.explorer.revealInDefaultExplorer',
(node: Node) => {
vscode.commands.executeCommand('revealInExplorer', node.uri);
}),
Expand Down Expand Up @@ -602,7 +614,8 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {

if (!node) {
// Reset the root in order to build from scratch (at OneTreeDataProvider.getTree)
this.tree = undefined;
this._tree = undefined;
this._nodeMap.clear();
this._onDidChangeTreeData.fire(undefined);
} else {
this._onDidChangeTreeData.fire(node);
Expand Down Expand Up @@ -772,11 +785,27 @@ input_path=${modelName}.${extName}
return undefined;
}

if (!this.tree) {
this.tree = NodeFactory.create(NodeType.directory, this.workspaceRoot.fsPath, undefined) as
if (!this._tree) {
this._tree = NodeFactory.create(NodeType.directory, this.workspaceRoot.fsPath, undefined) as
DirectoryNode;

// NOTE That this change reverts the 'build children on demand' optimization.
//
// 'buildNodeMap' is required for revealing the corresponding TreeItem when a cfg file is
// opened in cfg editor, because it pre-builts all the nodes to find the Node from a given
// string path.
//
// TODO Let's try to build nodes on demand (only with string path)
const buildNodeMap = (node: Node) => {
node.getChildren().forEach(childNode => {
this._nodeMap.set(childNode.path, childNode);
buildNodeMap(childNode);
});
};

buildNodeMap(this._tree);
}

return this.tree;
return this._tree;
}
}

0 comments on commit 00d72f2

Please sign in to comment.