mirror of
https://github.com/atom/atom.git
synced 2026-01-24 22:38:20 -05:00
Hook up command line to command interpreter
This commit is contained in:
@@ -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);'
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -17,4 +17,3 @@ class Substitution
|
||||
endPosition = buffer.positionForCharacterIndex(matchEndIndex)
|
||||
|
||||
buffer.change([startPosition, endPosition], @replaceText)
|
||||
|
||||
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user