Repository: TabbyML/tabby
File: clients/vscode/src/keybindings.ts (TypeScript)

Code before:
      const parsed = JSON.parse(cleanData) as KeyBinding[];
      return parsed.filter((binding) => {
        const isValid = binding.key && binding.command;
        if (!isValid) {
          logger.warn("Invalid keybinding found:", binding);
        }
        return isValid;
      });
    } catch (error) {
      logger.error("Error parsing keybindings JSON:", error);
      return [];
    }
  }

  /**
   * Checks if the specified command is rebound by verifying its presence in the cached keybindings.
   */
  isCommandRebound(command: string): boolean {
    return this.keybindings ? this.keybindings.some((binding) => binding.command === command) : false;
  }

  /**
   * Retrieves the keybinding for a specified command from the cached keybindings.
   * Returns the key if found and not disabled; otherwise returns null.
   */
  getCommandBinding(command: string): string | null {
    if (!this.keybindings) {
      return null;
    }
    const binding = this.keybindings.find((b) => b.command === command && !b.command.startsWith("-"));
    return binding?.key || null;
  }

  /**
   * Checks if a command is disabled by verifying if a keybinding with a '-' prefix exists.
   */
  isKeyBindingDisabled(command: string): boolean {
    return this.keybindings ? this.keybindings.some((b) => b.command === `-${command}`) : false;
  }
}

export function getPackageCommandBinding(command: string): string {
  try {
    if (!pkg.contributes.keybindings || !Array.isArray(pkg.contributes.keybindings)) {
      logger.warn("No keybindings found in package.json");
      return "";
    }
    const binding = pkg.contributes.keybindings.find((b) => b.command === command);
    if (!binding) {
      return "";
    }

Review comment (suggestion):
```suggestion
  getKeybinding(command: string): string | null {
```

1. if any custom keybinding exists, return the first
2. if default keybinding (from package.json) is disabled (custom `-` prefix keybinding), return null
3. return default keybinding



Code after:
      const parsed = JSON.parse(cleanData) as KeyBinding[];
      return parsed.filter((binding) => {
        const isValid = binding.key && binding.command;
        if (!isValid) {
          logger.warn("Invalid keybinding found:", binding);
        }
        return isValid;
      });
    } catch (error) {
      logger.error("Error parsing keybindings JSON:", error);
      return [];
    }
  }

  /**
   * Retrieves the keybinding for a specified command from the cached keybindings.
   * Returns the key if found and not disabled; otherwise returns null.
   */
  getKeybinding(command: string): string | null {
    if (!this.keybindings) {
      return null;
    }

    const disabledBinding = this.keybindings.find((b) => b.command === `-${command}`);
    if (disabledBinding) {
      return null;
    }

    const customBinding = this.keybindings.find((b) => b.command === command);
    if (customBinding && customBinding.key) {
      return customBinding.key;
    }

    return getPackageCommandBinding(command);
  }
}

export function getPackageCommandBinding(command: string): string {
  try {
    if (!pkg.contributes.keybindings || !Array.isArray(pkg.contributes.keybindings)) {
      logger.warn("No keybindings found in package.json");
      return "";
    }
    const binding = pkg.contributes.keybindings.find((b) => b.command === command);
    if (!binding) {
      return "";
    }
    return isMac && binding.mac ? binding.mac : binding.key;
  } catch (error) {
    logger.error("Error reading package.json keybindings:", error);
    return "";