Skip to content

Commit

Permalink
feat: add rename
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Oct 21, 2024
1 parent 729af97 commit 937f8f7
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ node_modules
# esbuild-visualizer
meta.json
stats.html

test-workspace/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
92 changes: 89 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/commands/CrudCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export enum DirectoryProviderCommands {
CantRemoveItem = 'directoryprovider/cantremoveitem',
RemoveItem = 'directoryprovider/removeitem',
RemoveAllItems = 'directoryprovider/removeallitems',
RenameItem = 'directoryprovider/renameitem',
}
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () =>
Expand All @@ -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',
Expand Down
12 changes: 5 additions & 7 deletions src/operator/DirectoryWorker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as path from 'path';

import * as vscode from 'vscode';

import { FileSystemObject } from '../types/FileSystemObject';
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';
Expand All @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions src/provider/DirectoryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class DirectoryProvider implements vscode.TreeDataProvider<FileSystemObje
return await this.directoryOperator.getChildren(element);
}

async selectItem(uri: vscode.Uri | undefined) {
await this.directoryOperator.selectItem(uri);
async selectItem(uri: vscode.Uri | undefined, name?: string | undefined) {
await this.directoryOperator.selectItem(uri, name);
this.refresh();
}

Expand All @@ -35,6 +35,19 @@ export class DirectoryProvider implements vscode.TreeDataProvider<FileSystemObje
this.refresh();
}

async renameItem(element: FileSystemObject): Promise<void> {
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();
}
Expand Down
10 changes: 7 additions & 3 deletions src/types/TypedDirectory.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 937f8f7

Please sign in to comment.