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

feat: support renaming alias name #14

Merged
merged 1 commit into from
Dec 6, 2024
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ You can create a new alias.

### Rename Alias

It supports renaming an alias.
It supports renaming alias name and command for an alias.

![rename](https://github.com/user-attachments/assets/a2c71fc5-0dc0-4873-9427-7bd509193b5b)
![rename](https://github.com/user-attachments/assets/088510aa-d8dc-487b-bc17-a408579fa9d2)

### Delete Alias

Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@

### 重命名

支持对已有别名重命名
支持对已有别名的名称和指令名称重命名

![rename](https://github.com/user-attachments/assets/a2c71fc5-0dc0-4873-9427-7bd509193b5b)
![rename](https://github.com/user-attachments/assets/088510aa-d8dc-487b-bc17-a408579fa9d2)

### 删除

Expand Down
4 changes: 4 additions & 0 deletions l10n/bundle.l10n.zh-cn.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"Please enter new alias": "请输入新的别名",
"Please enter new alias name": "请输入新的别名名称",
"Please enter new alias command": "请输入新的指令名称",
"Please enter new description": "请输入新的备注",
"Duplicate alias": "别名重复",
"Please check the format of the input content": "请检查输入内容的格式",
"Alias is mandatory to execute this action": "别名不能为空",
"Alias name is mandatory to execute this action": "别名名称不能为空",
"Alias command is mandatory to execute this action": "指令名称不能为空",
"No alias": "没有任何别名",
"Alias has been added to the clipboard Successfully": "别名已成功复制到剪贴板",
"Please enter new group": "请输入新的组名",
Expand Down
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@
"icon": "$(run)"
},
{
"command": "aliasView.rename",
"title": "%renameAlias%"
"command": "aliasView.renameAliasName",
"title": "%renameAliasName%"
},
{
"command": "aliasView.renameAliasCommand",
"title": "%renameAliasCommand%"
},
{
"command": "aliasView.copy",
Expand Down Expand Up @@ -127,7 +131,11 @@
"when": "false"
},
{
"command": "aliasView.rename",
"command": "aliasView.renameAliasName",
"when": "false"
},
{
"command": "aliasView.renameAliasCommand",
"when": "false"
},
{
Expand Down Expand Up @@ -203,7 +211,11 @@
"group": "inline"
},
{
"command": "aliasView.rename",
"command": "aliasView.renameAliasName",
"when": "view == aliasView && viewItem == alias_system_child"
},
{
"command": "aliasView.renameAliasCommand",
"when": "view == aliasView && viewItem == alias_system_child"
},
{
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"deleteAllAlias": "Delete All Alias",
"run": "Run",
"setDescription": "Set Description",
"renameAlias": "Rename Alias",
"renameAliasName": "Rename Alias Name",
"renameAliasCommand": "Rename Alias Command",
"copyAlias": "Copy Alias",
"removeFromCurrentGroup": "Remove From Current Group",
"addToAnotherGroup": "Add To Another Group",
Expand Down
3 changes: 2 additions & 1 deletion package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"deleteAllAlias": "删除所有别名",
"run": "运行",
"setDescription": "更新备注",
"renameAlias": "重命名别名",
"renameAliasName": "重命名别名名称",
"renameAliasCommand": "重命名指令名称",
"copyAlias": "复制别名",
"removeFromCurrentGroup": "从当前组中移除",
"addToAnotherGroup": "添加到另一个组",
Expand Down
11 changes: 3 additions & 8 deletions src/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function deleteAliases(specificAlias?: Alias) {
reloadStoreFile();
}

export function renameAliases(specificAlias: Alias, command: string) {
export function renameAliases(specificAlias: Alias, targetAlias: Pick<Alias, 'aliasName' | 'command'>) {
const content = getAliasFromPath();

if (isEmpty(content)) {
Expand All @@ -96,13 +96,8 @@ export function renameAliases(specificAlias: Alias, command: string) {
.reduce((acc: string[], text) => {
const alias = resolveAlias(text);

if (alias) {
const { aliasName } = alias;
if (isSameAlias(alias, specificAlias)) {
acc.push(`alias ${aliasName}='${command}'`);
} else {
acc.push(text);
}
if (alias && isSameAlias(alias, specificAlias)) {
acc.push(`alias ${targetAlias.aliasName}='${targetAlias.command}'`);
} else {
acc.push(text);
}
Expand Down
55 changes: 50 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ export function activate(context: vscode.ExtensionContext) {
);

context.subscriptions.push(
vscode.commands.registerCommand('aliasView.rename', (alias: AliasItem) => aliasView.renameAlias(alias)),
vscode.commands.registerCommand('aliasView.renameAliasName', (alias: AliasItem) =>
aliasView.renameAliasName(alias),
),
);

context.subscriptions.push(
vscode.commands.registerCommand('aliasView.renameAliasCommand', (alias: AliasItem) =>
aliasView.renameAliasCommand(alias),
),
);

context.subscriptions.push(
Expand Down Expand Up @@ -238,13 +246,50 @@ class AliasView implements vscode.TreeDataProvider<AliasItem> {
this.refresh();
}

async renameAlias(alias: AliasItem) {
async renameAliasName(alias: AliasItem) {
if (!alias.data) {
return;
}

const aliasName = await vscode.window.showInputBox({
placeHolder: vscode.l10n.t('Please enter new alias name'),
value: alias.data.aliasName,
});

// cancel input aliasName
if (aliasName === undefined) {
return;
}

if (!aliasName.length) {
vscode.window.showErrorMessage(vscode.l10n.t('Alias name is mandatory to execute this action'));
return;
}

renameAliases(alias.data, { aliasName, command: alias.data.command });

// rename one alias under every groups
for (const groupName of this.globalState.keys()) {
const aliases = normalizeAliasesToArray<Alias>(this.globalState.get(groupName));
const sameAlias = aliases.find((aliasItem) => isSameAlias(alias.data as Alias, aliasItem));

if (sameAlias) {
sameAlias.aliasName = aliasName;
this.globalState.update(groupName, aliases);
}
}

executeCommandInTerminal(`alias ${aliasName}='${alias.data.command}'`);
this.refresh();
}

async renameAliasCommand(alias: AliasItem) {
if (!alias.data) {
return;
}

const command = await vscode.window.showInputBox({
placeHolder: vscode.l10n.t('Please enter new alias'),
placeHolder: vscode.l10n.t('Please enter new alias command'),
value: alias.data.command,
});

Expand All @@ -254,11 +299,11 @@ class AliasView implements vscode.TreeDataProvider<AliasItem> {
}

if (!command.length) {
vscode.window.showErrorMessage(vscode.l10n.t('Alias is mandatory to execute this action'));
vscode.window.showErrorMessage(vscode.l10n.t('Alias command is mandatory to execute this action'));
return;
}

renameAliases(alias.data, command);
renameAliases(alias.data, { aliasName: alias.data.aliasName, command });

// rename one alias under every groups
for (const groupName of this.globalState.keys()) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function isValid(value: string, skipNoQuote = false) {
* @param {value} string
* @returns If it is not valid, return undefined
*/
export function resolveAlias(value: string): Omit<Alias, 'frequency' | 'description'> | undefined {
export function resolveAlias(value: string): Pick<Alias, 'aliasName' | 'command'> | undefined {
if (!value.startsWith('alias')) {
return;
}
Expand Down
Loading