diff --git a/.gitignore b/.gitignore index ddd018e..9fc46ef 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ node_modules # esbuild-visualizer meta.json stats.html + +test-workspace/ \ No newline at end of file diff --git a/README.md b/README.md index c766856..1d251b5 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ This is a vscode extension based on [Explorer Bookmark](https://github.com/UrosVuj/Explorer-Bookmark) +with some improvements: + +- Rename bookmark name + ## Development Install dependencies by: diff --git a/package.json b/package.json index 321193e..2ac9e22 100644 --- a/package.json +++ b/package.json @@ -41,12 +41,98 @@ ], "activationEvents": [], "contributes": { + "viewsWelcome": [ + { + "view": "my-folders", + "contents": "Right click and add a file or folder" + } + ], + "views": { + "explorer": [ + { + "id": "my-folders", + "name": "My Folders" + } + ] + }, "commands": [ { - "command": "my-folders.helloWorld", - "title": "Hello World" + "command": "directoryprovider/refreshentry", + "title": "Refresh", + "icon": "$(refresh)" + }, + { + "command": "directoryprovider/selectitem", + "title": "Add to My Folders" + }, + { + "command": "directoryprovider/removeitem", + "title": "Remove from My Folders", + "icon": "$(close)" + }, + { + "command": "directoryprovider/renameitem", + "title": "Rename bookmark", + "icon": "$(pencil)" + }, + { + "command": "directoryprovider/removeallitems", + "title": "Remove All Items", + "icon": "$(trash)" + }, + { + "command": "directoryprovider/cantremoveitemmsg", + "title": "Remove from My Folders" } - ] + ], + "menus": { + "view/title": [ + { + "command": "directoryprovider/refreshentry", + "when": "view == my-folders", + "group": "navigation" + }, + { + "command": "directoryprovider/removeallitems", + "when": "view == my-folders", + "group": "navigation" + } + ], + "explorer/context": [ + { + "command": "directoryprovider/selectitem" + } + ], + "view/item/context": [ + { + "command": "directoryprovider/removeitem", + "when": "view == my-folders && viewItem == directlyBookmarkedDirectory" + }, + { + "command": "directoryprovider/removeitem", + "when": "view == my-folders && viewItem == directlyBookmarkedDirectory", + "group": "inline" + }, + { + "command": "directoryprovider/renameitem", + "when": "view == my-folders && viewItem == directlyBookmarkedDirectory" + }, + { + "command": "directoryprovider/cantremoveitemmsg", + "when": "view == my-folders && viewItem != directlyBookmarkedDirectory" + } + ] + }, + "configuration": { + "title": "My Folders", + "properties": { + "my-folders.saveWorkspace": { + "type": "boolean", + "default": true, + "description": "Save the current explorer view when closing VS Code" + } + } + } }, "eslintConfig": { "extends": "@yutengjing/eslint-config-typescript", diff --git a/src/commands/CrudCommands.ts b/src/commands/CrudCommands.ts index 4471fcf..d3c3ec2 100644 --- a/src/commands/CrudCommands.ts +++ b/src/commands/CrudCommands.ts @@ -9,4 +9,5 @@ export enum DirectoryProviderCommands { CantRemoveItem = 'directoryprovider/cantremoveitem', RemoveItem = 'directoryprovider/removeitem', RemoveAllItems = 'directoryprovider/removeallitems', + RenameItem = 'directoryprovider/renameitem', } diff --git a/src/extension.ts b/src/extension.ts index 6cda0d2..6a0d2ba 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,7 +9,7 @@ export function activate(context: vscode.ExtensionContext) { const directoryProvider = new DirectoryProvider(directoryOperator); - vscode.window.registerTreeDataProvider('explorer-bookmark', directoryProvider); + vscode.window.registerTreeDataProvider('my-folders', directoryProvider); context.subscriptions.push( vscode.commands.registerCommand(DirectoryProviderCommands.RefreshEntry, () => @@ -27,6 +27,9 @@ export function activate(context: vscode.ExtensionContext) { vscode.commands.registerCommand(DirectoryProviderCommands.RemoveItem, (args) => { directoryProvider.removeItem(args.resourceUri); }), + vscode.commands.registerCommand(DirectoryProviderCommands.RenameItem, (args) => { + directoryProvider.renameItem(args); + }), vscode.commands.registerCommand(DirectoryProviderCommands.CantRemoveItem, () => { vscode.window.showInformationMessage( 'You can only remove items that were directly added to the view', diff --git a/src/operator/DirectoryWorker.ts b/src/operator/DirectoryWorker.ts index 4cfbf8b..e1de9d8 100644 --- a/src/operator/DirectoryWorker.ts +++ b/src/operator/DirectoryWorker.ts @@ -1,5 +1,3 @@ -import * as path from 'path'; - import * as vscode from 'vscode'; import { FileSystemObject } from '../types/FileSystemObject'; @@ -7,7 +5,7 @@ import type { TypedDirectory } from '../types/TypedDirectory'; import { buildTypedDirectory } from '../types/TypedDirectory'; export class DirectoryWorker { - readonly vsCodeExtensionConfigurationKey: string = 'explorer-bookmark'; + readonly vsCodeExtensionConfigurationKey: string = 'my-folders'; readonly saveWorkspaceConfigurationSettingKey: string = 'saveWorkspace'; readonly storedBookmarksContextKey: string = 'storedBookmarks'; readonly bookmarkedDirectoryContextValue: string = 'directlyBookmarkedDirectory'; @@ -32,9 +30,9 @@ export class DirectoryWorker { } } - public async selectItem(uri: vscode.Uri | undefined) { + public async selectItem(uri: vscode.Uri | undefined, name?: string | undefined) { if (uri) { - this.bookmarkedDirectories.push(await buildTypedDirectory(uri)); + this.bookmarkedDirectories.push(await buildTypedDirectory(uri, name)); } this.saveBookmarks(); } @@ -80,12 +78,12 @@ export class DirectoryWorker { const fileSystem: FileSystemObject[] = []; for (const dir of bookmarkedDirectories) { - const { path: filePath, type: type } = dir; + const { path: filePath, type: type, name: folderName } = dir; const file = vscode.Uri.file(filePath); fileSystem.push( new FileSystemObject( - `${path.basename(dir.path)}`, + `${folderName}`, type === vscode.FileType.File ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed, diff --git a/src/provider/DirectoryProvider.ts b/src/provider/DirectoryProvider.ts index 6422773..fa4860f 100644 --- a/src/provider/DirectoryProvider.ts +++ b/src/provider/DirectoryProvider.ts @@ -20,8 +20,8 @@ export class DirectoryProvider implements vscode.TreeDataProvider { + const value = await vscode.window.showInputBox({ + placeHolder: 'New name for bookmark', + }); + + if (!value) { + return; + } + + await this.removeItem(element.resourceUri); + await this.selectItem(element.resourceUri, value); + } + refresh(): void { this._onDidChangeTreeData.fire(); } diff --git a/src/types/TypedDirectory.ts b/src/types/TypedDirectory.ts index f90eb19..ebdbcef 100644 --- a/src/types/TypedDirectory.ts +++ b/src/types/TypedDirectory.ts @@ -1,16 +1,20 @@ +import * as filePath from 'path'; + import * as vscode from 'vscode'; export class TypedDirectory { + name: string; path: string; type: vscode.FileType; - constructor(path: string, type: vscode.FileType) { + constructor(path: string, type: vscode.FileType, name?: string | undefined) { this.path = path; this.type = type; + this.name = name || filePath.basename(path) || path; } } -export async function buildTypedDirectory(uri: vscode.Uri) { +export async function buildTypedDirectory(uri: vscode.Uri, name?: string | undefined) { const type = (await vscode.workspace.fs.stat(uri)).type; - return new TypedDirectory(uri.path, type); + return new TypedDirectory(uri.path, type, name); }