From 8e293613d243850c3f01821a2e843b5e4c5fb44f Mon Sep 17 00:00:00 2001 From: Chuan Du Date: Sun, 17 Sep 2017 16:51:14 -0400 Subject: [PATCH 1/2] Support command history --- lib/command-palette-view.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/command-palette-view.js b/lib/command-palette-view.js index 1526f72..2f0701a 100644 --- a/lib/command-palette-view.js +++ b/lib/command-palette-view.js @@ -9,6 +9,7 @@ export default class CommandPaletteView { constructor () { this.keyBindingsForActiveElement = [] this.commandsForActiveElement = [] + this.recentCommands = [] this.selectListView = new SelectListView({ items: this.commandsForActiveElement, filter: this.filter, @@ -69,6 +70,12 @@ export default class CommandPaletteView { }, didConfirmSelection: (keyBinding) => { this.hide() + if (this.recentCommands.indexOf(keyBinding.name) === -1) { + this.recentCommands.unshift(keyBinding.name) + this.recentCommands.splice(10, 2); + } else { + this.recentCommands = this.recentCommands.sort((x,y) => x == keyBinding.name ? -1 : y == keyBinding.name ? 1 : 0); + } const event = new CustomEvent(keyBinding.name, {bubbles: true, cancelable: true}) this.activeElement.dispatchEvent(event) }, @@ -106,7 +113,21 @@ 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.sort((a, b) => a.displayName.localeCompare(b.displayName)) + this.commandsForActiveElement = this.commandsForActiveElement.sort((x,y) => { + var indexX = this.recentCommands.indexOf(x.name); + var indexY = this.recentCommands.indexOf(y.name); + if (indexX !== -1 && indexY !== -1) { + return indexX > indexY + } + else if (indexX !== -1){ + return -1 + } + else if (indexY !== -1){ + return 1 + } + return x.displayName.localeCompare(y.displayName) + }); + await this.selectListView.update({items: this.commandsForActiveElement}) this.previouslyFocusedElement = document.activeElement From c0a4b558e77949bf88d13c37f79d59b10c89085c Mon Sep 17 00:00:00 2001 From: Chuan Du Date: Sun, 17 Sep 2017 18:01:19 -0400 Subject: [PATCH 2/2] Add options --- lib/command-palette-package.js | 7 +++++ lib/command-palette-view.js | 54 ++++++++++++++++++++++------------ package.json | 10 +++++++ 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/lib/command-palette-package.js b/lib/command-palette-package.js index d4332b9..8a87f2e 100644 --- a/lib/command-palette-package.js +++ b/lib/command-palette-package.js @@ -16,6 +16,13 @@ class CommandPalettePackage { this.disposables.add(atom.config.observe('command-palette.preserveLastSearch', (newValue) => { this.commandPaletteView.update({preserveLastSearch: newValue}) })) + this.disposables.add(atom.config.observe('command-palette.historyCommands', (newValue) => { + this.commandPaletteView.update({historyCommands: newValue}) + })) + this.disposables.add(atom.config.observe('command-palette.maxHistoryCommands', (newValue) => { + this.commandPaletteView.update({maxHistoryCommands: newValue}) + })) + return this.commandPaletteView.show() } diff --git a/lib/command-palette-view.js b/lib/command-palette-view.js index 2f0701a..b9fae38 100644 --- a/lib/command-palette-view.js +++ b/lib/command-palette-view.js @@ -70,12 +70,15 @@ export default class CommandPaletteView { }, didConfirmSelection: (keyBinding) => { this.hide() - if (this.recentCommands.indexOf(keyBinding.name) === -1) { - this.recentCommands.unshift(keyBinding.name) - this.recentCommands.splice(10, 2); - } else { - this.recentCommands = this.recentCommands.sort((x,y) => x == keyBinding.name ? -1 : y == keyBinding.name ? 1 : 0); + if (this.historyCommands) { + if (this.recentCommands.indexOf(keyBinding.name) === -1) { + this.recentCommands.unshift(keyBinding.name) + this.recentCommands.splice(this.maxHistoryCommands, 1); + } else { + this.recentCommands = this.recentCommands.sort((x,y) => x == keyBinding.name ? -1 : y == keyBinding.name ? 1 : 0); + } } + const event = new CustomEvent(keyBinding.name, {bubbles: true, cancelable: true}) this.activeElement.dispatchEvent(event) }, @@ -113,20 +116,25 @@ 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 = this.commandsForActiveElement.sort((x,y) => { - var indexX = this.recentCommands.indexOf(x.name); - var indexY = this.recentCommands.indexOf(y.name); - if (indexX !== -1 && indexY !== -1) { - return indexX > indexY - } - else if (indexX !== -1){ - return -1 - } - else if (indexY !== -1){ - return 1 - } - return x.displayName.localeCompare(y.displayName) - }); + if (this.historyCommands) { + this.commandsForActiveElement.sort((x,y) => { + var indexX = this.recentCommands.indexOf(x.name); + var indexY = this.recentCommands.indexOf(y.name); + if (indexX !== -1 && indexY !== -1) { + return indexX > indexY + } + else if (indexX !== -1){ + return -1 + } + else if (indexY !== -1){ + return 1 + } + return x.displayName.localeCompare(y.displayName) + }); + } else { + this.commandsForActiveElement.sort((a,b) => a.displayName.localeCompare(b.displayName)); + } + await this.selectListView.update({items: this.commandsForActiveElement}) @@ -148,6 +156,14 @@ export default class CommandPaletteView { this.preserveLastSearch = props.preserveLastSearch } + if (props.hasOwnProperty('historyCommands')) { + this.historyCommands = props.historyCommands + } + + if (props.hasOwnProperty('maxHistoryCommands')) { + this.maxHistoryCommands = props.maxHistoryCommands + } + if (props.hasOwnProperty('useAlternateScoring')) { this.useAlternateScoring = props.useAlternateScoring } diff --git a/package.json b/package.json index 1a95a87..4b1ae31 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,16 @@ "type": "boolean", "default": false, "description": "Preserve the last search when reopening the command palette." + }, + "historyCommands": { + "type": "boolean", + "default": false, + "description": "Keep command history" + }, + "maxHistoryCommands": { + "type": "integer", + "default": 10, + "description": "Number of history command show on select-list" } } }