Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Add support for "hidden" commands
Browse files Browse the repository at this point in the history
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](#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.
  • Loading branch information
Will Binns-Smith committed Oct 24, 2017
1 parent ba73eaf commit f08d19a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
4 changes: 3 additions & 1 deletion lib/command-palette-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
50 changes: 34 additions & 16 deletions test/command-palette-view.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -198,25 +199,21 @@ 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')
Expand All @@ -230,6 +227,11 @@ describe('CommandPaletteView', () => {
})

it('highlights partial matches in the tags', async () => {
disposable.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')
Expand All @@ -241,6 +243,22 @@ describe('CommandPaletteView', () => {
assert(matches.length > 0)
assert.equal(matches[0].textContent, 'bar')
})

it ('doesn\'t show results that are marked `hiddenInCommandPalette`', () => {
disposable.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)
})
})
})
})

0 comments on commit f08d19a

Please sign in to comment.