diff --git a/spec/extensions/command-panel-spec.coffee b/spec/extensions/command-panel-spec.coffee index 7524cd91e..0ac0f483e 100644 --- a/spec/extensions/command-panel-spec.coffee +++ b/spec/extensions/command-panel-spec.coffee @@ -41,6 +41,16 @@ describe "CommandPanel", -> rootView.trigger 'command-panel:repeat-relative-address' expect(commandPanel.commandInterpreter.repeatRelativeAddress).toHaveBeenCalledWith(rootView.activeEditor()) + describe "when command-panel:set-selection-as-regex-address is triggered on the root view", -> + it "sets the @lastRelativeAddress to a RegexAddress of the current selection", -> + rootView.open(require.resolve('fixtures/sample.js')) + rootView.activeEditor().setSelectedBufferRange([[1,21],[1,28]]) + + commandInterpreter = commandPanel.commandInterpreter + expect(commandInterpreter.lastRelativeAddress).toBeUndefined() + rootView.trigger 'command-panel:set-selection-as-regex-address' + expect(commandInterpreter.lastRelativeAddress.regex.toString()).toEqual "/\\(items\\)/" + describe "when command-panel:find-in-file is triggered on an editor", -> it "pre-populates command panel's editor with /", -> rootView.activeEditor().trigger "command-panel:find-in-file" diff --git a/src/extensions/command-panel.coffee b/src/extensions/command-panel.coffee index 613c03977..7fa0f0fb0 100644 --- a/src/extensions/command-panel.coffee +++ b/src/extensions/command-panel.coffee @@ -1,8 +1,12 @@ {View} = require 'space-pen' CommandInterpreter = require 'command-interpreter' +RegexAddress = require 'command-interpreter/regex-address' +CompositeCommand = require 'command-interpreter/composite-command' Editor = require 'editor' {SyntaxError} = require('pegjs').parser +_ = require 'underscore' + module.exports = class CommandPanel extends View @activate: (rootView, state) -> @@ -38,6 +42,7 @@ class CommandPanel extends View @rootView.on 'command-panel:execute', => @execute() @rootView.on 'command-panel:find-in-file', => @show("/") @rootView.on 'command-panel:repeat-relative-address', => @repeatRelativeAddress() + @rootView.on 'command-panel:set-selection-as-regex-address', => @useSelectionAsLastRelativeAddress() @miniEditor.off 'move-up move-down' @miniEditor.on 'move-up', => @navigateBackwardInHistory() @@ -82,3 +87,8 @@ class CommandPanel extends View repeatRelativeAddress: -> @commandInterpreter.repeatRelativeAddress(@rootView.activeEditor()) + + useSelectionAsLastRelativeAddress: -> + selection = @rootView.activeEditor().getSelectedText() + regex = _.escapeRegExp(selection) + @commandInterpreter.lastRelativeAddress = new CompositeCommand([new RegexAddress(regex)]) \ No newline at end of file