Skip to content

Commit

Permalink
Refactor: FileSystemObject with fileType
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Oct 25, 2024
1 parent 31a0826 commit 9f10fd7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
},
{
"command": "my-folders.focusInExplorer",
"title": "Focus on the selected item in file explorer view"
"title": "Focus on this in explorer view"
},
{
"command": "my-folders.clearFocusScope",
Expand Down Expand Up @@ -120,20 +120,20 @@
"view/item/context": [
{
"command": "my-folders.removeItem",
"when": "view == my-folders && viewItem == myFoldersDirectory"
"when": "view == my-folders && (viewItem == myFoldersDirectory || viewItem == myFoldersFile)"
},
{
"command": "my-folders.removeItem",
"when": "view == my-folders && viewItem == myFoldersDirectory",
"when": "view == my-folders && (viewItem == myFoldersDirectory || viewItem == myFoldersFile)",
"group": "inline"
},
{
"command": "my-folders.renameItem",
"when": "view == my-folders && viewItem == myFoldersDirectory"
"when": "view == my-folders && (viewItem == myFoldersDirectory || viewItem == myFoldersFile)"
},
{
"command": "my-folders.cantRemoveItem",
"when": "view == my-folders && viewItem != myFoldersDirectory"
"when": "view == my-folders && viewItem != myFoldersDirectory && viewItem != myFoldersFile"
},
{
"command": "my-folders.focusInExplorer",
Expand Down
3 changes: 2 additions & 1 deletion src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const EXTENSION_NAME = 'my-folders';
export const CONTEXT_IS_SCOPED = 'my-folders.isScoped';
export const REGISTER_TREE_DATA_PROVIDER = 'my-folders';
export const CONFIG_FILE_NAME = 'my-folders.json';
export const MY_FOLDER_DIRECTORY_CONTEXT = 'myFoldersDirectory';
export const MY_FOLDER_CONTEXT_DIRECTORY = 'myFoldersDirectory';
export const MY_FOLDER_CONTEXT_FILE = 'myFoldersFile';
12 changes: 1 addition & 11 deletions src/operator/DirectoryOperator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as vscode from 'vscode';

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

export class DirectoryOperator {
readonly myFolderDirContextValue: string = MY_FOLDER_DIRECTORY_CONTEXT;
private bookmarkedDirectories: ITypedDirectory[] = [];
private hideContent: boolean = false;

Expand Down Expand Up @@ -93,21 +91,13 @@ export class DirectoryOperator {

private async createEntries(bookmarkedDirectories: ITypedDirectory[]) {
const fileSystem: FileSystemObject[] = [];

for (const dir of bookmarkedDirectories) {
const { path: filePath, type: type, name: folderName } = dir;
const fileUri = vscode.Uri.file(filePath);
fileSystem.push(
createFileSystemObject(
folderName,
type,
fileUri,
this.hideContent,
this.myFolderDirContextValue,
),
createFileSystemObject(folderName, type, fileUri, this.hideContent, true),
);
}

return fileSystem;
}

Expand Down
41 changes: 21 additions & 20 deletions src/types/FileSystemObject.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import * as vscode from 'vscode';

import { ExtensionCommands } from '../core/commands';
import { MY_FOLDER_CONTEXT_DIRECTORY, MY_FOLDER_CONTEXT_FILE } from '../core/constants';

function getCollapsibleState(fileType: vscode.FileType) {
return fileType === vscode.FileType.File
? vscode.TreeItemCollapsibleState.None
: vscode.TreeItemCollapsibleState.Collapsed;
}

export class FileSystemObject extends vscode.TreeItem {
resourceUri: vscode.Uri;
command?: vscode.Command;

constructor(
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly fileType: vscode.FileType,
uri: vscode.Uri,
private hideContent: boolean,
) {
const collapsibleState = getCollapsibleState(fileType);
super(label, collapsibleState);
this.tooltip = uri.fsPath;
this.resourceUri = uri;
this.command = this.createCommand(collapsibleState);
this.iconPath = this.getIconPath(collapsibleState);
this.command = this.createCommand(fileType);
this.iconPath = this.getIconPath(fileType);
}

private getIconPath(
collapsibleState: vscode.TreeItemCollapsibleState,
):
| string
| vscode.Uri
| { light: string | vscode.Uri; dark: string | vscode.Uri }
| vscode.ThemeIcon {
if (this.isFile(collapsibleState)) {
private getIconPath(fileType: vscode.FileType) {
if (fileType === vscode.FileType.File) {
return new vscode.ThemeIcon('symbol-file');
} else {
return new vscode.ThemeIcon('symbol-folder');
}
}

private createCommand(collapsibleState: vscode.TreeItemCollapsibleState) {
private createCommand(fileType: vscode.FileType) {
// If the item is a file, return a command to open the file
if (this.isFile(collapsibleState)) {
if (fileType === vscode.FileType.File) {
return {
arguments: [this],
command: ExtensionCommands.OpenItem,
Expand All @@ -53,12 +55,11 @@ export class FileSystemObject extends vscode.TreeItem {
}
}

private isFile(collapsibleState: vscode.TreeItemCollapsibleState) {
return collapsibleState === vscode.TreeItemCollapsibleState.None;
}

setContextValue(value: string) {
this.contextValue = value;
return this;
setContextValue() {
if (this.fileType === vscode.FileType.Directory) {
this.contextValue = MY_FOLDER_CONTEXT_DIRECTORY;
} else {
this.contextValue = MY_FOLDER_CONTEXT_FILE;
}
}
}
14 changes: 5 additions & 9 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ export function createFileSystemObject(
type: vscode.FileType,
fileUri: vscode.Uri,
hideContent: boolean,
directoryContext?: string,
isMyFolderItem = false,
): 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);
const fObj = new FileSystemObject(folderName, type, fileUri, hideContent);
if (isMyFolderItem) {
fObj.setContextValue();
}

return fObj;
}

Expand Down

0 comments on commit 9f10fd7

Please sign in to comment.