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

Commit 3742526

Browse files
committed
Copy item's command-name and display-name from contextMenu
1 parent 2b7db27 commit 3742526

File tree

4 files changed

+162
-1
lines changed

4 files changed

+162
-1
lines changed

lib/command-palette-view.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,43 @@ export default class CommandPaletteView {
9494
}
9595
})
9696
this.selectListView.element.classList.add('command-palette')
97+
this.registerCommands()
9798
}
9899

99100
async destroy () {
100101
await this.selectListView.destroy()
101102
}
102103

104+
copyItemText (propertyName, event) {
105+
const item = event
106+
? this.selectListView.getItemForElement(event.target.closest("li")) // by contextMenu
107+
: this.selectListView.getSelectedItem() // by keyborad shortcut
108+
if (item) atom.clipboard.write("`" + item[propertyName] + "`")
109+
}
110+
111+
registerCommands () {
112+
atom.commands.add(this.selectListView.element, {
113+
"command-palette:copy-display-name": () => this.copyItemText("displayName"),
114+
"command-palette:copy-display-name-from-context-menu": event => this.copyItemText("displayName", event),
115+
"command-palette:copy-command-name": () => this.copyItemText("name"),
116+
"command-palette:copy-command-name-from-context-menu": event => this.copyItemText("name", event)
117+
})
118+
const queryEditor = this.selectListView.refs.queryEditor
119+
atom.commands.add(queryEditor.element, {
120+
"core:copy": event => {
121+
const propertyToCopy = atom.config.get('command-palette.copySelectedItemTextWhenEmptySelection')
122+
if (propertyToCopy !== "none" && queryEditor.getSelectedBufferRange().isEmpty()) {
123+
event.stopImmediatePropagation()
124+
if (propertyToCopy === "display-name") {
125+
this.copyItemText("displayName")
126+
} else if (propertyToCopy === "command-name") {
127+
this.copyItemText("name")
128+
}
129+
}
130+
}
131+
})
132+
}
133+
103134
toggle () {
104135
if (this.panel && this.panel.isVisible()) {
105136
this.hide()

menus/command-palette.cson

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@
2323
]
2424
}
2525
]
26+
27+
'context-menu':
28+
'.command-palette .list-group': [
29+
{label: 'Copy Display Name', command: 'command-palette:copy-display-name-from-context-menu'}
30+
{label: 'Copy Command Name', command: 'command-palette:copy-command-name-from-context-menu'}
31+
]

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"atomTestRunner": "atom-mocha-test-runner",
1717
"dependencies": {
18-
"atom-select-list": "^0.7.0",
18+
"atom-select-list": "atom/atom-select-list#24/head",
1919
"fuzzaldrin": "^2.1.0",
2020
"fuzzaldrin-plus": "^0.4.1",
2121
"underscore-plus": "^1.0.0"
@@ -29,14 +29,23 @@
2929
},
3030
"configSchema": {
3131
"useAlternateScoring": {
32+
"order": 0,
3233
"type": "boolean",
3334
"default": true,
3435
"description": "Use an alternative scoring approach which prefers run of consecutive characters, acronyms and start of words."
3536
},
3637
"preserveLastSearch": {
38+
"order": 1,
3739
"type": "boolean",
3840
"default": false,
3941
"description": "Preserve the last search when reopening the command palette."
42+
},
43+
"copySelectedItemTextWhenEmptySelection": {
44+
"order": 2,
45+
"type": "string",
46+
"default": "none",
47+
"enum": ["none", "command-name", "display-name"],
48+
"description": "Copy selected item's command name or display name by `core:copy`."
4049
}
4150
}
4251
}

test/command-palette-view.test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,119 @@ describe('CommandPaletteView', () => {
351351
})
352352
})
353353
})
354+
355+
describe('`command-palette:copy-display-name` and `command-palette:copy-command-name`', () => {
356+
beforeEach(() => {
357+
atom.commands.add('*', {
358+
'test-xxxxx-command:command-one': () => {},
359+
'test-xxxxx-command:command-two': () => {},
360+
'test-xxxxx-command:command-three': () => {},
361+
'test-xxxxx-command:command-four': () => {},
362+
'test-xxxxx-command:command-five': () => {},
363+
})
364+
})
365+
366+
it('[by keyboard shortcut] copy name for selected item', async () => {
367+
const spy = sandbox.spy(atom.clipboard, 'write')
368+
369+
const commandPalette = new CommandPaletteView()
370+
const selectListView = commandPalette.selectListView
371+
await commandPalette.toggle()
372+
373+
selectListView.selectItem(selectListView.items.find(item => item.name === "test-xxxxx-command:command-one"))
374+
atom.commands.dispatch(selectListView.element, 'command-palette:copy-display-name')
375+
assert(spy.calledWith("`Test Xxxxx Command: Command One`"))
376+
atom.commands.dispatch(selectListView.element, 'command-palette:copy-command-name')
377+
assert(spy.calledWith("`test-xxxxx-command:command-one`"))
378+
379+
spy.reset()
380+
selectListView.selectItem(selectListView.items.find(item => item.name === "test-xxxxx-command:command-two"))
381+
atom.commands.dispatch(selectListView.element, 'command-palette:copy-display-name')
382+
assert(spy.calledWith("`Test Xxxxx Command: Command Two`"))
383+
atom.commands.dispatch(selectListView.element, 'command-palette:copy-command-name')
384+
assert(spy.calledWith("`test-xxxxx-command:command-two`"))
385+
})
386+
387+
it('[by context menu] copy name for selected item', async () => {
388+
const spy = sandbox.spy(atom.clipboard, 'write')
389+
390+
const commandPalette = new CommandPaletteView()
391+
const selectListView = commandPalette.selectListView
392+
await commandPalette.toggle()
393+
394+
selectListView.refs.queryEditor.setText('xxxxx')
395+
await selectListView.update()
396+
const elements = Array.from(selectListView.refs.items.children)
397+
assert.equal(elements.length, 5)
398+
399+
const elementOne = selectListView.element.querySelector("[data-event-name='test-xxxxx-command:command-one']")
400+
atom.commands.dispatch(elementOne, 'command-palette:copy-display-name-from-context-menu')
401+
assert(spy.calledWith("`Test Xxxxx Command: Command One`"))
402+
atom.commands.dispatch(elementOne, 'command-palette:copy-command-name-from-context-menu')
403+
assert(spy.calledWith("`test-xxxxx-command:command-one`"))
404+
405+
spy.reset()
406+
const elementTwo = selectListView.element.querySelector("[data-event-name='test-xxxxx-command:command-two']")
407+
atom.commands.dispatch(elementTwo, 'command-palette:copy-display-name-from-context-menu')
408+
assert(spy.calledWith("`Test Xxxxx Command: Command Two`"))
409+
atom.commands.dispatch(elementTwo, 'command-palette:copy-command-name-from-context-menu')
410+
assert(spy.calledWith("`test-xxxxx-command:command-two`"))
411+
412+
spy.reset()
413+
const elementThree = selectListView.element.querySelector("[data-event-name='test-xxxxx-command:command-three']")
414+
atom.commands.dispatch(elementThree, 'command-palette:copy-display-name-from-context-menu')
415+
assert(spy.calledWith("`Test Xxxxx Command: Command Three`"))
416+
atom.commands.dispatch(elementThree, 'command-palette:copy-command-name-from-context-menu')
417+
assert(spy.calledWith("`test-xxxxx-command:command-three`"))
418+
419+
spy.reset()
420+
const elementFour = selectListView.element.querySelector("[data-event-name='test-xxxxx-command:command-four']")
421+
atom.commands.dispatch(elementFour, 'command-palette:copy-display-name-from-context-menu')
422+
assert(spy.calledWith("`Test Xxxxx Command: Command Four`"))
423+
atom.commands.dispatch(elementFour, 'command-palette:copy-command-name-from-context-menu')
424+
assert(spy.calledWith("`test-xxxxx-command:command-four`"))
425+
426+
spy.reset()
427+
const elementFive = selectListView.element.querySelector("[data-event-name='test-xxxxx-command:command-five']")
428+
atom.commands.dispatch(elementFive, 'command-palette:copy-display-name-from-context-menu')
429+
assert(spy.calledWith("`Test Xxxxx Command: Command Five`"))
430+
atom.commands.dispatch(elementFive, 'command-palette:copy-command-name-from-context-menu')
431+
assert(spy.calledWith("`test-xxxxx-command:command-five`"))
432+
})
433+
434+
describe('core:copy behavior with command-palette.copySelectedItemTextWhenEmptySelection setting', () => {
435+
let spy, selectListView
436+
beforeEach(async () => {
437+
spy = sandbox.spy(atom.clipboard, 'write')
438+
439+
const commandPalette = new CommandPaletteView()
440+
selectListView = commandPalette.selectListView
441+
await commandPalette.toggle()
442+
443+
selectListView.refs.queryEditor.setText('xxxxx')
444+
await selectListView.update()
445+
assert.equal(selectListView.getSelectedItem().name, 'test-xxxxx-command:command-one')
446+
const elements = Array.from(selectListView.refs.items.children)
447+
assert.equal(elements.length, 5)
448+
})
449+
450+
it('[value = "none"]: copy line text', async () => {
451+
atom.config.set("command-palette.copySelectedItemTextWhenEmptySelection", "none")
452+
atom.commands.dispatch(selectListView.refs.queryEditor.element, 'core:copy')
453+
assert(spy.calledWith("xxxxx")) // default behavoir of atom-text-editor
454+
})
455+
456+
it('[value = "display-name"]: copy displayName of selected item', async () => {
457+
atom.config.set("command-palette.copySelectedItemTextWhenEmptySelection", "display-name")
458+
atom.commands.dispatch(selectListView.refs.queryEditor.element, 'core:copy')
459+
assert(spy.calledWith("`Test Xxxxx Command: Command One`"))
460+
})
461+
462+
it('[value = "command-name]": copy command name of selected item', async () => {
463+
atom.config.set("command-palette.copySelectedItemTextWhenEmptySelection", "command-name")
464+
atom.commands.dispatch(selectListView.refs.queryEditor.element, 'core:copy')
465+
assert(spy.calledWith("`test-xxxxx-command:command-one`"))
466+
})
467+
})
468+
})
354469
})

0 commit comments

Comments
 (0)