Skip to content

Commit 3b459bc

Browse files
committed
feat: add hideContent option
1 parent 42b78c2 commit 3b459bc

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const EXTENSION_NAME = 'my-folders';
22
export const REGISTER_TREE_DATA_PROVIDER = 'my-folders';
33
export const configFileName: string = 'my-folders.json';
4+
export const MY_FOLDER_DIRECTORY_CONTEXT = 'directlyBookmarkedDirectory';

src/operator/DirectoryWorker.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import * as vscode from 'vscode';
22

3+
import { MY_FOLDER_DIRECTORY_CONTEXT } from '../constants';
34
import type { IConfiguration } from '../types/Configuration';
4-
import { FileSystemObject } from '../types/FileSystemObject';
5+
import type { FileSystemObject } from '../types/FileSystemObject';
56
import type { TypedDirectory } from '../types/TypedDirectory';
6-
import { buildTypedDirectory, getConfigurationAsync, updateConfigurationAsync } from '../utils';
7+
import {
8+
buildTypedDirectory,
9+
createFileSystemObject,
10+
focusFileExplorer,
11+
getConfigurationAsync,
12+
updateConfigurationAsync,
13+
} from '../utils';
714

815
export class DirectoryWorker {
9-
readonly bookmarkedDirectoryContextValue: string = 'directlyBookmarkedDirectory';
16+
readonly myFolderDirContextValue: string = MY_FOLDER_DIRECTORY_CONTEXT;
1017
private bookmarkedDirectories: TypedDirectory[] = [];
1118
private hideContent: boolean = false;
1219

@@ -20,7 +27,12 @@ export class DirectoryWorker {
2027

2128
public async getChildren(element?: FileSystemObject): Promise<FileSystemObject[]> {
2229
if (element) {
23-
return this.directorySearch(element.resourceUri);
30+
if (this.hideContent) {
31+
focusFileExplorer(element.resourceUri);
32+
return [];
33+
} else {
34+
return this.directorySearch(element.resourceUri);
35+
}
2436
} else {
2537
return this.bookmarkedDirectories.length > 0
2638
? this.createEntries(this.bookmarkedDirectories)
@@ -61,15 +73,11 @@ export class DirectoryWorker {
6173
.sort((a, b) => a[0].localeCompare(b[0]))
6274
.map((item) => {
6375
const [name, type] = item;
64-
const isDirectory =
65-
type === vscode.FileType.Directory
66-
? vscode.TreeItemCollapsibleState.Collapsed
67-
: vscode.TreeItemCollapsibleState.None;
68-
69-
return new FileSystemObject(
76+
return createFileSystemObject(
7077
name,
71-
isDirectory,
78+
type,
7279
vscode.Uri.file(`${uri.path}/${name}`),
80+
this.hideContent,
7381
);
7482
});
7583
}
@@ -79,16 +87,15 @@ export class DirectoryWorker {
7987

8088
for (const dir of bookmarkedDirectories) {
8189
const { path: filePath, type: type, name: folderName } = dir;
82-
const file = vscode.Uri.file(filePath);
83-
90+
const fileUri = vscode.Uri.file(filePath);
8491
fileSystem.push(
85-
new FileSystemObject(
86-
`${folderName}`,
87-
type === vscode.FileType.File
88-
? vscode.TreeItemCollapsibleState.None
89-
: vscode.TreeItemCollapsibleState.Collapsed,
90-
file,
91-
).setContextValue(this.bookmarkedDirectoryContextValue),
92+
createFileSystemObject(
93+
folderName,
94+
type,
95+
fileUri,
96+
this.hideContent,
97+
this.myFolderDirContextValue,
98+
),
9299
);
93100
}
94101

src/types/FileSystemObject.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,32 @@ export class FileSystemObject extends vscode.TreeItem {
1010
public readonly label: string,
1111
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
1212
uri: vscode.Uri,
13+
private hideContent: boolean,
1314
) {
1415
super(label, collapsibleState);
1516
this.tooltip = uri.fsPath;
1617
this.resourceUri = uri;
17-
this.command =
18-
collapsibleState === vscode.TreeItemCollapsibleState.None
19-
? {
20-
arguments: [this],
21-
command: ExtensionCommands.OpenItem,
22-
title: this.label,
23-
}
24-
: undefined;
18+
this.command = this.createCommand(collapsibleState);
19+
}
20+
21+
private createCommand(collapsibleState: vscode.TreeItemCollapsibleState) {
22+
// If the item is a file, return a command to open the file
23+
if (collapsibleState === vscode.TreeItemCollapsibleState.None) {
24+
return {
25+
arguments: [this],
26+
command: ExtensionCommands.OpenItem,
27+
title: this.label,
28+
};
29+
}
30+
if (!this.hideContent) {
31+
return;
32+
} else {
33+
return {
34+
arguments: [this.resourceUri],
35+
command: 'revealInExplorer',
36+
title: this.label,
37+
};
38+
}
2539
}
2640

2741
setContextValue(value: string) {

src/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vscode from 'vscode';
44

55
import { configFileName as CONFIG_FILE_NAME } from './constants';
66
import type { IConfiguration } from './types/Configuration';
7+
import { FileSystemObject } from './types/FileSystemObject';
78
import { TypedDirectory } from './types/TypedDirectory';
89

910
export function getConfigurationDirUri(
@@ -38,3 +39,26 @@ export async function buildTypedDirectory(uri: vscode.Uri, name?: string | undef
3839
const type = (await vscode.workspace.fs.stat(uri)).type;
3940
return new TypedDirectory(uri.path, type, name);
4041
}
42+
43+
export function createFileSystemObject(
44+
folderName: string,
45+
type: vscode.FileType,
46+
fileUri: vscode.Uri,
47+
hideContent: boolean,
48+
directoryContext?: string,
49+
): FileSystemObject {
50+
const collapsibleState =
51+
type === vscode.FileType.File
52+
? vscode.TreeItemCollapsibleState.None
53+
: vscode.TreeItemCollapsibleState.Collapsed;
54+
55+
const fObj = new FileSystemObject(folderName, collapsibleState, fileUri, hideContent);
56+
if (directoryContext) {
57+
fObj.setContextValue(directoryContext);
58+
}
59+
return fObj;
60+
}
61+
62+
export function focusFileExplorer(uri: vscode.Uri) {
63+
vscode.commands.executeCommand('revealInExplorer', uri);
64+
}

0 commit comments

Comments
 (0)