Skip to content

Commit

Permalink
Additional contexts for which the "Add to Favorites" and "Add to Favo…
Browse files Browse the repository at this point in the history
…rites 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.
  • Loading branch information
CommanderPho committed Sep 15, 2022
1 parent 340a3a1 commit 4db1a32
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
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
42 changes: 41 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 @@ -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",
Expand Down Expand Up @@ -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"
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

0 comments on commit 4db1a32

Please sign in to comment.