Skip to content

Commit

Permalink
feat: add hideContent option
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Oct 23, 2024
1 parent 42b78c2 commit 3b459bc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const EXTENSION_NAME = 'my-folders';
export const REGISTER_TREE_DATA_PROVIDER = 'my-folders';
export const configFileName: string = 'my-folders.json';
export const MY_FOLDER_DIRECTORY_CONTEXT = 'directlyBookmarkedDirectory';
47 changes: 27 additions & 20 deletions src/operator/DirectoryWorker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as vscode from 'vscode';

import { MY_FOLDER_DIRECTORY_CONTEXT } from '../constants';
import type { IConfiguration } from '../types/Configuration';
import { FileSystemObject } from '../types/FileSystemObject';
import type { FileSystemObject } from '../types/FileSystemObject';
import type { TypedDirectory } from '../types/TypedDirectory';
import { buildTypedDirectory, getConfigurationAsync, updateConfigurationAsync } from '../utils';
import {
buildTypedDirectory,
createFileSystemObject,
focusFileExplorer,
getConfigurationAsync,
updateConfigurationAsync,
} from '../utils';

export class DirectoryWorker {
readonly bookmarkedDirectoryContextValue: string = 'directlyBookmarkedDirectory';
readonly myFolderDirContextValue: string = MY_FOLDER_DIRECTORY_CONTEXT;
private bookmarkedDirectories: TypedDirectory[] = [];
private hideContent: boolean = false;

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

public async getChildren(element?: FileSystemObject): Promise<FileSystemObject[]> {
if (element) {
return this.directorySearch(element.resourceUri);
if (this.hideContent) {
focusFileExplorer(element.resourceUri);
return [];
} else {
return this.directorySearch(element.resourceUri);
}
} else {
return this.bookmarkedDirectories.length > 0
? this.createEntries(this.bookmarkedDirectories)
Expand Down Expand Up @@ -61,15 +73,11 @@ export class DirectoryWorker {
.sort((a, b) => a[0].localeCompare(b[0]))
.map((item) => {
const [name, type] = item;
const isDirectory =
type === vscode.FileType.Directory
? vscode.TreeItemCollapsibleState.Collapsed
: vscode.TreeItemCollapsibleState.None;

return new FileSystemObject(
return createFileSystemObject(
name,
isDirectory,
type,
vscode.Uri.file(`${uri.path}/${name}`),
this.hideContent,
);
});
}
Expand All @@ -79,16 +87,15 @@ export class DirectoryWorker {

for (const dir of bookmarkedDirectories) {
const { path: filePath, type: type, name: folderName } = dir;
const file = vscode.Uri.file(filePath);

const fileUri = vscode.Uri.file(filePath);
fileSystem.push(
new FileSystemObject(
`${folderName}`,
type === vscode.FileType.File
? vscode.TreeItemCollapsibleState.None
: vscode.TreeItemCollapsibleState.Collapsed,
file,
).setContextValue(this.bookmarkedDirectoryContextValue),
createFileSystemObject(
folderName,
type,
fileUri,
this.hideContent,
this.myFolderDirContextValue,
),
);
}

Expand Down
30 changes: 22 additions & 8 deletions src/types/FileSystemObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,32 @@ export class FileSystemObject extends vscode.TreeItem {
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
uri: vscode.Uri,
private hideContent: boolean,
) {
super(label, collapsibleState);
this.tooltip = uri.fsPath;
this.resourceUri = uri;
this.command =
collapsibleState === vscode.TreeItemCollapsibleState.None
? {
arguments: [this],
command: ExtensionCommands.OpenItem,
title: this.label,
}
: undefined;
this.command = this.createCommand(collapsibleState);
}

private createCommand(collapsibleState: vscode.TreeItemCollapsibleState) {
// If the item is a file, return a command to open the file
if (collapsibleState === vscode.TreeItemCollapsibleState.None) {
return {
arguments: [this],
command: ExtensionCommands.OpenItem,
title: this.label,
};
}
if (!this.hideContent) {
return;
} else {
return {
arguments: [this.resourceUri],
command: 'revealInExplorer',
title: this.label,
};
}
}

setContextValue(value: string) {
Expand Down
24 changes: 24 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as vscode from 'vscode';

import { configFileName as CONFIG_FILE_NAME } from './constants';
import type { IConfiguration } from './types/Configuration';
import { FileSystemObject } from './types/FileSystemObject';
import { TypedDirectory } from './types/TypedDirectory';

export function getConfigurationDirUri(
Expand Down Expand Up @@ -38,3 +39,26 @@ export async function buildTypedDirectory(uri: vscode.Uri, name?: string | undef
const type = (await vscode.workspace.fs.stat(uri)).type;
return new TypedDirectory(uri.path, type, name);
}

export function createFileSystemObject(
folderName: string,
type: vscode.FileType,
fileUri: vscode.Uri,
hideContent: boolean,
directoryContext?: string,
): FileSystemObject {
const collapsibleState =
type === vscode.FileType.File
? vscode.TreeItemCollapsibleState.None
: vscode.TreeItemCollapsibleState.Collapsed;

const fObj = new FileSystemObject(folderName, collapsibleState, fileUri, hideContent);
if (directoryContext) {
fObj.setContextValue(directoryContext);
}
return fObj;
}

export function focusFileExplorer(uri: vscode.Uri) {
vscode.commands.executeCommand('revealInExplorer', uri);
}

0 comments on commit 3b459bc

Please sign in to comment.