From 4db1a3218e560f645c325123947db153b5f0a583 Mon Sep 17 00:00:00 2001 From: Pho Hale Date: Thu, 15 Sep 2022 00:50:36 -0400 Subject: [PATCH 1/2] Additional contexts for which the "Add to Favorites" and "Add to Favorites Group" context menu items appear enabled the "Add to Favorites" and "Add to Favorites Group" context menu items in additional context menus such as when right clicking an open file tab, or in the text of an open editor. --- CHANGELOG.md | 5 +++ package.json | 42 ++++++++++++++++++++- src/command/index.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7e0603..777a0fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.4.6 | 2022/09/15 + +- enabled the "Add to Favorites" and "Add to Favorites Group" context menu items in additional context menus such as when right clicking an open file tab, or in the text of an open editor. + + ## 2.4.5 | 2018/11/29 - fixed vulnerability issue diff --git a/package.json b/package.json index 649fe43..0425efd 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "favorites", "displayName": "Favorites", "description": "Add files and directories to workspace favorites. Create groups of directories and files. Time saver for complex projects.", - "version": "2.4.5", + "version": "2.4.6", "categories": [ "Other" ], @@ -134,6 +134,38 @@ "group": "favorites" } ], + "editor/title": [ + { + "command": "favorites.addToFavorites", + "group": "favorites" + }, + { + "command": "favorites.addToFavoritesGroup", + "group": "favorites" + } + ], + "editor/title/context": [ + { + "command": "favorites.addToFavorites", + "group": "favorites" + }, + { + "command": "favorites.addToFavoritesGroup", + "group": "favorites" + } + ], + "editor/context": [ + { + "when": "resourceScheme == file", + "command": "favorites.addFileToFavorites", + "group": "favorites" + }, + { + "when": "resourceScheme == file", + "command": "favorites.addFileToFavoritesGroup", + "group": "favorites" + } + ], "view/title": [ { "command": "favorites.collapse", @@ -488,6 +520,14 @@ "command": "favorites.addToFavoritesGroup", "title": "Favorites: Add to group of favorites" }, + { + "command": "favorites.addFileToFavorites", + "title": "Favorites: Add file to favorites" + }, + { + "command": "favorites.addFileToFavoritesGroup", + "title": "Favorites: Add file to group of favorites" + }, { "command": "favorites.deleteFavorite", "title": "Remove from favorites" diff --git a/src/command/index.ts b/src/command/index.ts index a6f0864..410f149 100644 --- a/src/command/index.ts +++ b/src/command/index.ts @@ -35,6 +35,8 @@ export class Commands { context.subscriptions.push(this.addExternal()); context.subscriptions.push(this.addToFavorites()); context.subscriptions.push(this.addToFavoritesGroup()); + context.subscriptions.push(this.addFileToFavorites()); + context.subscriptions.push(this.addFileToFavoritesGroup()); context.subscriptions.push(this.deleteFavorite()); context.subscriptions.push(this.setSortAsc()); context.subscriptions.push(this.setSortDesc()); @@ -568,6 +570,91 @@ export class Commands { }); } + + addFileToFavorites = () => { + // Note implementation is duplicated verbatim from addToFavorites. TODO: figure out how to call same code to prevent needless code duplication. + return vscode.commands.registerCommand("favorites.addFileToFavorites", (fileUri: vscode.Uri, list: any[]) => { + + const isList = Array.isArray(list); + const isFile = (fileUri && fileUri.fsPath) != null ? true : false; + const isActiveEditor = vscode.window.activeTextEditor != null ? true : false; + + if (!isList && isFile) { + list = [fileUri]; + } + if (!isList && !isFile && isActiveEditor) { + list = [vscode.window.activeTextEditor.document.uri]; + } + + const run = async () => { + + for (const uri of list) { + const itemPath = uri.fsPath; + const workspacePath = workspace.workspaceRoot(itemPath); + if (workspacePath) { + await this.favorites.addPathToGroup(null, itemPath); + } else { + await this.favorites.addExternalPathToGroup(null, itemPath); + } + } + }; + + run(); + + }); + } + addFileToFavoritesGroup = () => { + // Note implementation is duplicated verbatim from addToFavoritesGroup. TODO: figure out how to call same code to prevent needless code duplication. + return vscode.commands.registerCommand("favorites.addFileToFavoritesGroup", (fileUri: vscode.Uri, list: any[]) => { + + const isList = Array.isArray(list); + const isFile = (fileUri && fileUri.fsPath) != null ? true : false; + const isActiveEditor = vscode.window.activeTextEditor != null ? true : false; + + if (!isList && isFile) { + list = [fileUri]; + } + if (!isList && !isFile && isActiveEditor) { + list = [vscode.window.activeTextEditor.document.uri]; + } + + const run = async (group_id: string) => { + + for (const uri of list) { + const itemPath = uri.fsPath; + const workspacePath = workspace.workspaceRoot(itemPath); + + if (workspacePath) { + await this.favorites.addPathToGroup(group_id, itemPath); + } else { + await this.favorites.addExternalPathToGroup(group_id, itemPath); + } + } + }; + + this.favorites.generateGroupQuickPickList() + .then((result) => { + + if (result.length === 0) { + vscode.window.showWarningMessage("No group definition found. Create group first."); + return; + } + vscode.window.showQuickPick(result) + .then((pickedItem) => { + if (pickedItem == null) { + // canceled + return; + } + run(pickedItem.id); + }); + + }) + .catch((e) => { + console.log(e); + }); + + }); + } collapse = () => { return vscode.commands.registerCommand("favorites.collapse", (v) => { From 3e42b5e4b46fa7a60f00ecedd060d6e801cb9ac9 Mon Sep 17 00:00:00 2001 From: Pho Hale Date: Thu, 15 Sep 2022 19:32:55 -0400 Subject: [PATCH 2/2] added control of which context menus the "Add to * favorites" commands will appear in via settings `favorites.includeInDocumentBodyContextMenu` : boolean (default `false`) - if set to `true`, the two "Add to * favorites" commands will be included in the editor context menu that appears when right-clicking the body of an open document. `favorites.includeInEditorTabContextMenu` : boolean (default `true`) - if set to `true`, the two "Add to * favorites" commands will be included in the context menu for the tab of a specific file (e.g. the menu that appears when right-clicking the tab of an open document). --- README.md | 10 ++++++++-- package.json | 16 ++++++++++++++-- src/types/index.ts | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b047c6d..e12ac6d 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,12 @@ Open Visual Studio Code press CTRL+p and type or copy and paste: `favorites.useTrash`: boolean (default `false`) - if set to `true`, extension will try to use system trash when resource (file or directory is deleted) +`favorites.includeInDocumentBodyContextMenu` : boolean (default `false`) +- if set to `true`, the two "Add to * favorites" commands will be included in the editor context menu that appears when right-clicking the body of an open document. + +`favorites.includeInEditorTabContextMenu` : boolean (default `true`) +- if set to `true`, the two "Add to * favorites" commands will be included in the context menu for the tab of a specific file (e.g. the menu that appears when right-clicking the tab of an open document). + ## Keyboard browsing You can browse favorites using **keyboard only** by executing command `Favorites: Browse` command from palette. Assign keyboard shortcut if needed. @@ -89,9 +95,9 @@ You can browse favorites using **keyboard only** by executing command `Favorites ## Usage #### Adding to favorites -Right-click item in File explorer and select `Add to favorites`. +Right-click item in File explorer, an open file tab, or the background of an open editor and select `Add to favorites`. #### Adding to favorites group or subgroup -Right-click item in File explorer and select `Add to favorites group`, then select group from list. +Right-click item in File explorer, an open file tab, or the background of an open editor and select `Add to favorites group`, then select group from list. #### Removing from favorites Right-click item in Favorites view and select `Remove from favorites` #### Create favorites group diff --git a/package.json b/package.json index 0425efd..f976d31 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,16 @@ ], "default": "ASC", "description": "Specify an order for all favorites" + }, + "favorites.includeInDocumentBodyContextMenu": { + "type": "boolean", + "default": false, + "description": "Display the 'Favorites: Add to * favorites' commands in the editor context menu" + }, + "favorites.includeInEditorTabContextMenu": { + "type": "boolean", + "default": true, + "description": "Display the 'Favorites: Add to * favorites' commands in the context menu for the tab of a specific file" } } }, @@ -146,22 +156,24 @@ ], "editor/title/context": [ { + "when": "config.favorites.includeInEditorTabContextMenu", "command": "favorites.addToFavorites", "group": "favorites" }, { + "when": "config.favorites.includeInEditorTabContextMenu", "command": "favorites.addToFavoritesGroup", "group": "favorites" } ], "editor/context": [ { - "when": "resourceScheme == file", + "when": "config.favorites.includeInDocumentBodyContextMenu && resourceScheme == file", "command": "favorites.addFileToFavorites", "group": "favorites" }, { - "when": "resourceScheme == file", + "when": "config.favorites.includeInDocumentBodyContextMenu && resourceScheme == file", "command": "favorites.addFileToFavoritesGroup", "group": "favorites" } diff --git a/src/types/index.ts b/src/types/index.ts index 4fd9b31..2104dde 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -24,6 +24,8 @@ export interface WorkspaceConfiguration { storageRegistry: string[]; groupsFirst: boolean; sortDirection: "ASC" | "DESC"; + includeInDocumentBodyContextMenu: boolean; + includeInEditorTabContextMenu: boolean; } export interface StoredResource {