Skip to content

Commit

Permalink
[OneExplorer] Enhance node map (Samsung#1708)
Browse files Browse the repository at this point in the history
This commit enhances node map to have multiple nodes.

ONE-vscode-DCO-1.0-Signed-off-by: Dayoung Lee <dayoung.lee@samsung.com>
  • Loading branch information
dayo09 authored Oct 12, 2023
1 parent e597fb9 commit b8ce2ea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/OneExplorer/OneExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,15 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
}
}),
provider.fileWatcher.onDidDelete((uri: vscode.Uri) => {
const node = OneStorage.getNode(uri.fsPath);
if (!node) {
const nodes = OneStorage.getNodes(uri.fsPath);
if (nodes.length === 0) {
return;
}

OneStorage.delete(node, true);
provider.refresh(node.parent);
nodes.forEach((node) => {
OneStorage.delete(node, true);
provider.refresh(node.parent);
});
}),
vscode.workspace.onDidChangeWorkspaceFolders(() => {
provider._workspaceRoots = obtainWorkspaceRoots().map((root) =>
Expand All @@ -577,14 +579,14 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
vscode.commands.registerCommand(
"one.explorer.revealInOneExplorer",
(path: string) => {
const node = OneStorage.getNode(path);
if (node) {
const nodes = OneStorage.getNodes(path);
nodes.forEach((node) => {
provider._treeView?.reveal(node, {
select: true,
focus: true,
expand: true,
});
}
});
}
),
vscode.commands.registerCommand(
Expand Down
56 changes: 53 additions & 3 deletions src/OneExplorer/OneStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,50 @@ import { Node, NodeType } from "./OneExplorer";

export { CfgToCfgObjMap as _unit_test_CfgToCfgObjMap };

export class MultiMap<U, T> {
private _map: Map<U, T[]>;

constructor() {
this._map = new Map<U, T[]>();
}

get(u: U): T[] {
return this._map.get(u) ?? [];
}

exist(u: U, t: T): boolean {
return this._map.get(u)?.includes(t) ? true : false;
}

insert(u: U, t: T) {
if (this.exist(u, t)) {
return;
}

if (!this._map.get(u)) {
this._map.set(u, [t]);
} else {
this._map.set(u, [...this._map.get(u)!, t]);
}
}

delete(u: U, t?: T) {
if (!t) {
this._map.delete(u);
return;
}

if (!this.exist(u, t)) {
return;
}

this._map.set(
u,
this._map.get(u)!.filter((_t) => _t !== t)
);
}
}

class CfgToCfgObjMap {
private _map: Map<string, ConfigObj>;

Expand Down Expand Up @@ -130,7 +174,7 @@ export class OneStorage {
* pre-built. Mind that it will slow down the extension to gather data about unseen nodes.
* Currently, only the two depths from those shown nodes are built.
*/
private _nodeMap: Map<string, Node> = new Map<string, Node>();
private _nodeMap: MultiMap<string, Node> = new MultiMap<string, Node>();

/**
* @brief A map of ConfigObj (key: cfg path)
Expand Down Expand Up @@ -232,13 +276,19 @@ export class OneStorage {
/**
* Get Node from the map
*/
public static getNode(fsPath: string): Node | undefined {
public static getNodes(fsPath: string): Node[] {
return OneStorage.get()._nodeMap.get(fsPath);
}

public static insert(node: Node) {
const inst = OneStorage.get();
inst._nodeMap.set(node.path, node);

if (inst._nodeMap.exist(node.path, node)) {
return;
}

inst._nodeMap.insert(node.path, node);

if (node.type === NodeType.config) {
if (!inst._cfgToCfgObjMap.get(node.path)) {
const cfgObj = ConfigObj.createConfigObj(node.uri);
Expand Down

0 comments on commit b8ce2ea

Please sign in to comment.