From b1a06ca832c8d58be01b39771a9c856ba79044a1 Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Mon, 23 Oct 2017 22:40:25 -0700 Subject: [PATCH] Add support for "hidden" commands This allows for commands to opt-into not appear in command palette. Frequently commands are an implementation detail, or even only a means of providing a keybinding, and do not benefit from being shown and selectable to the user from the command palette. Options like "Vim Mode Plus: Set Input Char M" can be confusing to the user and cloud high-signal commands from surfacing in command palette results. Additionally, showing so many commands can [bog down the command palette and make it slow to start](https://github.com/atom/command-palette/issues/80) -- though there are certainly other means of improving its performance. The name is the most concise I could come up with to convey what we want here. There might be other ways of surfacing commands in an interface that might not want to obey this (or those that might), but for now I'm erring on the side of being specific to command palette. Totally open to making this more generic if there's a good argument or I'm missing something. --- lib/command-palette-view.js | 4 ++- test/command-palette-view.test.js | 52 +++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/command-palette-view.js b/lib/command-palette-view.js index 1526f72..0946ff8 100644 --- a/lib/command-palette-view.js +++ b/lib/command-palette-view.js @@ -105,7 +105,9 @@ export default class CommandPaletteView { this.activeElement = (document.activeElement === document.body) ? atom.views.getView(atom.workspace) : document.activeElement this.keyBindingsForActiveElement = atom.keymaps.findKeyBindings({target: this.activeElement}) - this.commandsForActiveElement = atom.commands.findCommands({target: this.activeElement}) + this.commandsForActiveElement = atom.commands + .findCommands({target: this.activeElement}) + .filter(command => !command.hiddenInCommandPalette) this.commandsForActiveElement.sort((a, b) => a.displayName.localeCompare(b.displayName)) await this.selectListView.update({items: this.commandsForActiveElement}) diff --git a/test/command-palette-view.test.js b/test/command-palette-view.test.js index 2e50a8e..c128418 100644 --- a/test/command-palette-view.test.js +++ b/test/command-palette-view.test.js @@ -5,6 +5,7 @@ import assert from 'assert' import semver from 'semver' import sinon from 'sinon' import CommandPaletteView from '../lib/command-palette-view' +import {CompositeDisposable} from 'event-kit' describe('CommandPaletteView', () => { let sandbox @@ -198,25 +199,22 @@ describe('CommandPaletteView', () => { return } - before(() => { - atom.commands.add( - '*', - { - 'foo:with-description': { - displayName: 'A Custom Display Name', - description: 'Awesome description here', - didDispatch () {} - }, - 'foo:with-tags': { - displayName: 'A Custom Display Name', - tags: ['bar', 'baz'], - didDispatch () {} - } - } - ) + let disposable + beforeEach(() => { + disposable = new CompositeDisposable() + }) + + afterEach(() => { + disposable.dispose() }) it('highlights partial matches in the description', async () => { + disposable.add(atom.commands.add('*', 'foo:with-description', { + displayName: 'A Custom Display Name', + description: 'Awesome description here', + didDispatch () {} + })) + const commandPalette = new CommandPaletteView() await commandPalette.toggle() commandPalette.selectListView.refs.queryEditor.setText('Awesome') @@ -230,6 +228,12 @@ describe('CommandPaletteView', () => { }) it('highlights partial matches in the tags', async () => { + disposable.add(atom.commands.add('*', 'foo:with-tags', { + displayName: 'A Custom Display Name', + tags: ['bar', 'baz'], + didDispatch () {} + })) + const commandPalette = new CommandPaletteView() await commandPalette.toggle() commandPalette.selectListView.refs.queryEditor.setText('bar') @@ -241,6 +245,22 @@ describe('CommandPaletteView', () => { assert(matches.length > 0) assert.equal(matches[0].textContent, 'bar') }) + + it ('doesn\'t show results that are marked `hiddenInCommandPalette`', () => { + disposable.add(atom.commands.add('*', 'foo:hidden-in-command-palette', { + hiddenInCommandPalette: true, + didDispatch () {} + })) + + const commandPalette = new CommandPaletteView() + await commandPalette.toggle() + commandPalette.selectListView.refs.queryEditor.setText('hidden in command palette') + await commandPalette.selectListView.update() + const {element} = commandPalette.selectListView + + const li = element.querySelector(`[data-event-name='foo:hidden-in-command-palette']`) + assert(li == null) + }) }) }) })