diff --git a/spec/atom/command-interpreter-spec.coffee b/spec/atom/command-interpreter-spec.coffee index 13f6ab209..7ad07cf65 100644 --- a/spec/atom/command-interpreter-spec.coffee +++ b/spec/atom/command-interpreter-spec.coffee @@ -8,11 +8,11 @@ describe "CommandInterpreter", -> beforeEach -> buffer = new Buffer(require.resolve 'fixtures/sample.js') editor = new Editor({buffer}) - interpreter = new CommandInterpreter(editor) + interpreter = new CommandInterpreter() describe "substitution", -> it "performs the substitution within the current dot", -> editor.selection.setBufferRange([[6, 0], [6, 44]]) - interpreter.eval('s/current/foo/') + interpreter.eval(editor, 's/current/foo/') expect(buffer.lineForRow(6)).toBe ' foo < pivot ? left.push(current) : right.push(current);' diff --git a/spec/atom/command-panel-spec.coffee b/spec/atom/command-panel-spec.coffee index d728b8987..c4a127681 100644 --- a/spec/atom/command-panel-spec.coffee +++ b/spec/atom/command-panel-spec.coffee @@ -40,3 +40,21 @@ describe "CommandPanel", -> commandPanel.editor.trigger keydownEvent('escape') expect(rootView.find('.command-panel')).not.toExist() + describe "when return is pressed on the panel's editor", -> + it "calls execute", -> + spyOn(commandPanel, 'execute') + rootView.trigger 'command-panel:toggle' + commandPanel.editor.insertText 's/hate/love/g' + commandPanel.editor.trigger keydownEvent('enter') + + expect(commandPanel.execute).toHaveBeenCalled() + + describe ".execute()", -> + it "executes the command and closes the command panel", -> + rootView.activeEditor().setText("i hate love") + rootView.activeEditor().getSelection().setBufferRange [[0,0], [0,Infinity]] + rootView.trigger 'command-panel:toggle' + commandPanel.editor.insertText 's/hate/love/' + commandPanel.execute() + expect(rootView.activeEditor().getText()).toBe "i love love" + expect(rootView.find('.command-panel')).not.toExist() diff --git a/src/atom/command-interpreter.coffee b/src/atom/command-interpreter.coffee index a6a8ab2b0..bfcf5a4c0 100644 --- a/src/atom/command-interpreter.coffee +++ b/src/atom/command-interpreter.coffee @@ -3,10 +3,10 @@ PEG = require 'pegjs' module.exports = class CommandInterpreter - constructor: (@editor) -> + constructor: -> @parser = PEG.buildParser(fs.read(require.resolve 'commands.pegjs')) - eval: (command) -> + eval: (editor, command) -> operation = @parser.parse(command) - operation.perform(@editor) + operation.perform(editor) diff --git a/src/atom/command-interpreter/substitution.coffee b/src/atom/command-interpreter/substitution.coffee index acecfdb11..b032c1a15 100644 --- a/src/atom/command-interpreter/substitution.coffee +++ b/src/atom/command-interpreter/substitution.coffee @@ -17,4 +17,3 @@ class Substitution endPosition = buffer.positionForCharacterIndex(matchEndIndex) buffer.change([startPosition, endPosition], @replaceText) - diff --git a/src/atom/command-panel.coffee b/src/atom/command-panel.coffee index 6e8f7f568..611ecabd3 100644 --- a/src/atom/command-panel.coffee +++ b/src/atom/command-panel.coffee @@ -1,4 +1,5 @@ {View} = require 'space-pen' +CommandInterpreter = require 'command-interpreter' Editor = require 'editor' module.exports = @@ -8,14 +9,20 @@ class CommandPanel extends View @div ':', class: 'prompt', outlet: 'prompt' @subview 'editor', new Editor + commandInterpreter: null + initialize: ({@rootView})-> requireStylesheet 'command-panel.css' window.keymap.bindKeys '.command-panel .editor', escape: 'command-panel:toggle' + enter: 'command-panel:execute' @rootView.on 'command-panel:toggle', => @toggle() + @rootView.on 'command-panel:execute', => @execute() @editor.addClass 'single-line' + @commandInterpreter = new CommandInterpreter() + toggle: -> if @parent().length @detach() @@ -26,3 +33,6 @@ class CommandPanel extends View @editor.focus() @editor.buffer.setText('') + execute: -> + @commandInterpreter.eval(@rootView.activeEditor(), @editor.getText()) + @toggle() \ No newline at end of file diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 2781917f1..f445513eb 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -85,7 +85,7 @@ class Editor extends View @on 'select-left', => @selectLeft() @on 'select-up', => @selectUp() @on 'select-down', => @selectDown() - @on 'newline', => @insertText("\n") + @on 'newline', => @insertText("\n") @on 'backspace', => @backspace() @on 'delete', => @delete() @on 'cut', => @cutSelection() @@ -371,6 +371,9 @@ class Editor extends View selectToScreenPosition: (position) -> @selection.selectToScreenPosition(position) selectToBufferPosition: (position) -> @selection.selectToBufferPosition(position) + setText: (text) -> @buffer.setText(text) + getText: -> @buffer.getText() + insertText: (text) -> { text, shouldOutdent } = @autoIndentText(text) @selection.insertText(text)