Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional contexts for which the "Add to Favorites *" context menus #67

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
54 changes: 53 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down Expand Up @@ -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"
}
}
},
Expand Down Expand Up @@ -134,6 +144,40 @@
"group": "favorites"
}
],
"editor/title": [
{
"command": "favorites.addToFavorites",
"group": "favorites"
},
{
"command": "favorites.addToFavoritesGroup",
"group": "favorites"
}
],
"editor/title/context": [
{
"when": "config.favorites.includeInEditorTabContextMenu",
"command": "favorites.addToFavorites",
"group": "favorites"
},
{
"when": "config.favorites.includeInEditorTabContextMenu",
"command": "favorites.addToFavoritesGroup",
"group": "favorites"
}
],
"editor/context": [
{
"when": "config.favorites.includeInDocumentBodyContextMenu && resourceScheme == file",
"command": "favorites.addFileToFavorites",
"group": "favorites"
},
{
"when": "config.favorites.includeInDocumentBodyContextMenu && resourceScheme == file",
"command": "favorites.addFileToFavoritesGroup",
"group": "favorites"
}
],
"view/title": [
{
"command": "favorites.collapse",
Expand Down Expand Up @@ -488,6 +532,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"
Expand Down
87 changes: 87 additions & 0 deletions src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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) => {

Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface WorkspaceConfiguration {
storageRegistry: string[];
groupsFirst: boolean;
sortDirection: "ASC" | "DESC";
includeInDocumentBodyContextMenu: boolean;
includeInEditorTabContextMenu: boolean;
}

export interface StoredResource {
Expand Down