Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Problem

When users pressed Ctrl+Shift+M without configuring custom menu commands (smart.menuCommands), the extension threw an error:

command 'extension.openMenu' not found.

This occurred because the extension.openMenu command was only registered when menuItems.length > 0, but the keybinding Ctrl+Shift+M was always active, creating a mismatch between the registered commands and keybindings.

Solution

1. Always Register the Command

Modified src/extension.ts to always register the extension.openMenu command, regardless of whether custom menu items are configured:

// Always register the command
let disposable = vscode.commands.registerCommand('extension.openMenu', () => {
    const items = getMenuItems();
    if (!items || items.length === 0) {
        // Directly open settings and show a temporary notification
        vscode.commands.executeCommand('workbench.action.openSettings', 'smart.menuCommands');
        vscode.window.setStatusBarMessage('$(info) 自定义菜单命令未配置,请添加自定义命令', 5000);
    } else {
        // Existing behavior: show QuickPick menu
        vscode.window.showQuickPick(items).then(selectedItem => {
            if (selectedItem) {
                executeCommand(selectedItem);
            }
        });
    }
});

When menu commands are not configured, the settings page opens immediately and a non-intrusive status bar notification appears for 5 seconds before automatically dismissing.

2. Enhanced Configuration Schema

Improved package.json configuration with user-friendly descriptions and examples in Chinese:

smart.menuCommands:

  • Added clear description with keyboard shortcut reference
  • Provided markdown examples showing common usage patterns (scons -c, scons --dist, etc.)
  • Added scope and order properties for better organization

smart.parallelBuidNumber:

  • Added detailed description explaining the purpose
  • Provided examples for different core counts (1, 4, 8)
  • Added minimum: 1 validation to prevent invalid values

Benefits

  • No more errors: Users get helpful guidance instead of cryptic error messages
  • Immediate access: Settings open directly without requiring button clicks
  • Non-intrusive notification: Status bar message auto-dismisses after 5 seconds
  • Better discoverability: Clear examples help users understand how to configure settings
  • Input validation: Prevents invalid configuration values
  • 100% backward compatible: No breaking changes, existing configurations continue to work

Testing

  • ✅ TypeScript compilation successful
  • ✅ ESLint passed with no errors
  • ✅ CodeQL security scan: 0 alerts
  • ✅ Verified both empty and configured menu scenarios
  • ✅ Status bar icon behavior preserved (only shows when menu items exist)

Fixes #31

Original prompt

This section details on the original issue you should resolve

<issue_title>feature: 当自定义菜单不存在时,使用Ctrl+Shift+M,会报错</issue_title>
<issue_description>当自定义菜单不存在时,使用Ctrl+Shift+M,会报错,显示:

  • command 'extension.openMenu' not found.

期望如果自定义菜单不存在时,进入这部分的设置界面,可以由用户来添加自定义项。同时,希望插件的设置可以做得更人性化些,而不仅仅是当前的比较原始的模式:

        "configuration": {
            "type": "object",
            "title": "Custom Commands(menu)",
            "properties": {
                "smart.menuCommands": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "default": []
                },
                "smart.parallelBuidNumber": {
                    "type": "number",
                    "default": 1
                }
            }
        },
        "keybindings": [
            {
                "command": "extension.buildProject",
                "key": "ctrl+shift+b"
            },
            {
                "command": "extension.openMenu",
                "key": "ctrl+shift+m"
            }
        ]

</issue_description>

<agent_instructions>根据issue的情况修正代码,并增强插件设置提示。完善代码后,确保构建没问题。</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Register extension.openMenu command regardless of menu items existence
- Show helpful message and open settings when menu is empty
- Improve package.json configuration with descriptions and examples
- Add minimum value validation for parallelBuidNumber

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
src/extension.ts Outdated
const items = getMenuItems();
if (!items || items.length === 0) {
// Show message and open settings when no menu items configured
vscode.window.showInformationMessage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot 可以直接打开,而不是还需要点击打开设置才打开。可以是直接打开,然后简单给一份提示,然后这份提示过一段时间后自动消失。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改。现在按 Ctrl+Shift+M 时,设置页面会直接打开,同时在状态栏显示一条提示消息(5秒后自动消失)。

更改内容:

  • 移除了需要点击的信息对话框和按钮
  • 直接调用 vscode.commands.executeCommand('workbench.action.openSettings', 'smart.menuCommands')
  • 使用 vscode.window.setStatusBarMessage() 显示临时提示(5秒后自动消失)

提交: d103bd6

Copilot AI changed the title [WIP] Fix error when custom menu does not exist Fix: Handle empty custom menu commands gracefully (Ctrl+Shift+M) Oct 24, 2025
Copilot AI requested a review from BernardXiong October 24, 2025 16:26
- Settings now open automatically without requiring button click
- Status bar message shows temporary notification (5 seconds)
- More streamlined user experience as requested in review

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
@BernardXiong BernardXiong marked this pull request as ready for review October 24, 2025 16:54
@BernardXiong BernardXiong merged commit f73d001 into main Oct 24, 2025
4 checks passed
@BernardXiong BernardXiong deleted the copilot/fix-custom-menu-error branch October 24, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: 当自定义菜单不存在时,使用Ctrl+Shift+M,会报错

2 participants