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

Add options to have history commands #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/command-palette-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
39 changes: 38 additions & 1 deletion lib/command-palette-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -69,6 +70,15 @@ export default class CommandPaletteView {
},
didConfirmSelection: (keyBinding) => {
this.hide()
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)
},
Expand Down Expand Up @@ -106,7 +116,26 @@ 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))
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})

this.previouslyFocusedElement = document.activeElement
Expand All @@ -127,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
}
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}