From a27b54b21b8b0cf7b405cfc6742aed67ef28c98b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 28 Mar 2012 18:08:00 -0700 Subject: [PATCH] Up/down in command panel navigates command history. --- spec/atom/command-panel-spec.coffee | 18 ++++++++++++++++++ src/atom/command-panel.coffee | 25 ++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/spec/atom/command-panel-spec.coffee b/spec/atom/command-panel-spec.coffee index 829ff4e66..57f316b98 100644 --- a/spec/atom/command-panel-spec.coffee +++ b/spec/atom/command-panel-spec.coffee @@ -68,6 +68,24 @@ describe "CommandPanel", -> expect(commandPanel).not.toHaveClass 'error' + describe "when move-up and move-down are triggerred on the editor", -> + it "navigates forward and backward through the command history", -> + commandPanel.execute 's/war/peace/g' + commandPanel.execute 's/twinkies/wheatgrass/g' + + rootView.trigger 'command-panel:toggle' + + commandPanel.editor.trigger 'move-up' + expect(commandPanel.editor.getText()).toBe 's/twinkies/wheatgrass/g' + commandPanel.editor.trigger 'move-up' + expect(commandPanel.editor.getText()).toBe 's/war/peace/g' + commandPanel.editor.trigger 'move-up' + expect(commandPanel.editor.getText()).toBe 's/war/peace/g' + commandPanel.editor.trigger 'move-down' + expect(commandPanel.editor.getText()).toBe 's/twinkies/wheatgrass/g' + commandPanel.editor.trigger 'move-down' + expect(commandPanel.editor.getText()).toBe '' + describe ".execute()", -> it "executes the command and closes the command panel", -> rootView.activeEditor().setText("i hate love") diff --git a/src/atom/command-panel.coffee b/src/atom/command-panel.coffee index 40ffbc63b..55d9c46e6 100644 --- a/src/atom/command-panel.coffee +++ b/src/atom/command-panel.coffee @@ -11,6 +11,8 @@ class CommandPanel extends View @subview 'editor', new Editor commandInterpreter: null + history: null + historyIndex: 0 initialize: ({@rootView})-> requireStylesheet 'command-panel.css' @@ -21,12 +23,17 @@ class CommandPanel extends View window.keymap.bindKeys '.editor', 'meta-g': 'command-panel:repeat-relative-address' + @commandInterpreter = new CommandInterpreter() + @history = [] + @rootView.on 'command-panel:toggle', => @toggle() @rootView.on 'command-panel:execute', => @execute() @rootView.on 'command-panel:repeat-relative-address', => @repeatRelativeAddress() @editor.addClass 'single-line' - @commandInterpreter = new CommandInterpreter() + @editor.off 'move-up move-down' + @editor.on 'move-up', => @navigateBackwardInHistory() + @editor.on 'move-down', => @navigateForwardInHistory() toggle: -> if @parent().length then @hide() else @show() @@ -41,9 +48,9 @@ class CommandPanel extends View @detach() @rootView.activeEditor().focus() - execute: -> + execute: (command = @editor.getText()) -> try - @commandInterpreter.eval(@rootView.activeEditor(), @editor.getText()) + @commandInterpreter.eval(@rootView.activeEditor(), command) catch error if error instanceof SyntaxError @addClass 'error' @@ -53,7 +60,19 @@ class CommandPanel extends View else throw error + @history.push(command) + @historyIndex = @history.length @hide() + navigateBackwardInHistory: -> + return if @historyIndex == 0 + @historyIndex-- + @editor.setText(@history[@historyIndex]) + + navigateForwardInHistory: -> + return if @historyIndex == @history.length + @historyIndex++ + @editor.setText(@history[@historyIndex] or '') + repeatRelativeAddress: -> @commandInterpreter.repeatRelativeAddress(@rootView.activeEditor())