Path: blob/main/src/vs/editor/contrib/codeAction/test/browser/codeActionKeybindingResolver.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { KeyCodeChord } from '../../../../../base/common/keybindings.js';7import { KeyCode } from '../../../../../base/common/keyCodes.js';8import { OperatingSystem } from '../../../../../base/common/platform.js';9import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';10import { organizeImportsCommandId, refactorCommandId } from '../../browser/codeAction.js';11import { CodeActionKeybindingResolver } from '../../browser/codeActionKeybindingResolver.js';12import { CodeActionKind } from '../../common/types.js';13import { IKeybindingService } from '../../../../../platform/keybinding/common/keybinding.js';14import { ResolvedKeybindingItem } from '../../../../../platform/keybinding/common/resolvedKeybindingItem.js';15import { USLayoutResolvedKeybinding } from '../../../../../platform/keybinding/common/usLayoutResolvedKeybinding.js';1617suite('CodeActionKeybindingResolver', () => {1819ensureNoDisposablesAreLeakedInTestSuite();2021const refactorKeybinding = createCodeActionKeybinding(22KeyCode.KeyA,23refactorCommandId,24{ kind: CodeActionKind.Refactor.value });2526const refactorExtractKeybinding = createCodeActionKeybinding(27KeyCode.KeyB,28refactorCommandId,29{ kind: CodeActionKind.Refactor.append('extract').value });3031const organizeImportsKeybinding = createCodeActionKeybinding(32KeyCode.KeyC,33organizeImportsCommandId,34undefined);3536test('Should match refactor keybindings', async function () {37const resolver = new CodeActionKeybindingResolver(38createMockKeyBindingService([refactorKeybinding])39).getResolver();4041assert.strictEqual(42resolver({ title: '' }),43undefined);4445assert.strictEqual(46resolver({ title: '', kind: CodeActionKind.Refactor.value }),47refactorKeybinding.resolvedKeybinding);4849assert.strictEqual(50resolver({ title: '', kind: CodeActionKind.Refactor.append('extract').value }),51refactorKeybinding.resolvedKeybinding);5253assert.strictEqual(54resolver({ title: '', kind: CodeActionKind.QuickFix.value }),55undefined);56});5758test('Should prefer most specific keybinding', async function () {59const resolver = new CodeActionKeybindingResolver(60createMockKeyBindingService([refactorKeybinding, refactorExtractKeybinding, organizeImportsKeybinding])61).getResolver();6263assert.strictEqual(64resolver({ title: '', kind: CodeActionKind.Refactor.value }),65refactorKeybinding.resolvedKeybinding);6667assert.strictEqual(68resolver({ title: '', kind: CodeActionKind.Refactor.append('extract').value }),69refactorExtractKeybinding.resolvedKeybinding);70});7172test('Organize imports should still return a keybinding even though it does not have args', async function () {73const resolver = new CodeActionKeybindingResolver(74createMockKeyBindingService([refactorKeybinding, refactorExtractKeybinding, organizeImportsKeybinding])75).getResolver();7677assert.strictEqual(78resolver({ title: '', kind: CodeActionKind.SourceOrganizeImports.value }),79organizeImportsKeybinding.resolvedKeybinding);80});81});8283function createMockKeyBindingService(items: ResolvedKeybindingItem[]): IKeybindingService {84return <IKeybindingService>{85getKeybindings: (): readonly ResolvedKeybindingItem[] => {86return items;87},88};89}9091function createCodeActionKeybinding(keycode: KeyCode, command: string, commandArgs: any) {92return new ResolvedKeybindingItem(93new USLayoutResolvedKeybinding(94[new KeyCodeChord(false, true, false, false, keycode)],95OperatingSystem.Linux),96command,97commandArgs,98undefined,99false,100null,101false);102}103104105