aboutsummaryrefslogtreecommitdiff
path: root/atom/packages/ex-mode/lib/ex-state.coffee
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2015-07-10 11:12:25 -0500
committerBen Beltran <ben@nsovocal.com>2015-07-10 11:12:25 -0500
commit24c7594d62d8d7fbbcdb64b11ce4adc5d8e6991a (patch)
treeded312222bb108923da1820ba40b04d710d20e5b /atom/packages/ex-mode/lib/ex-state.coffee
parenteb786e82d170e2abc351a432ade616d6ecdeeb6b (diff)
Adds atom
Diffstat (limited to 'atom/packages/ex-mode/lib/ex-state.coffee')
-rw-r--r--atom/packages/ex-mode/lib/ex-state.coffee59
1 files changed, 59 insertions, 0 deletions
diff --git a/atom/packages/ex-mode/lib/ex-state.coffee b/atom/packages/ex-mode/lib/ex-state.coffee
new file mode 100644
index 0000000..b676535
--- /dev/null
+++ b/atom/packages/ex-mode/lib/ex-state.coffee
@@ -0,0 +1,59 @@
+{Emitter, Disposable, CompositeDisposable} = require 'event-kit'
+
+Command = require './command'
+CommandError = require './command-error'
+
+class ExState
+ constructor: (@editorElement, @globalExState) ->
+ @emitter = new Emitter
+ @subscriptions = new CompositeDisposable
+ @editor = @editorElement.getModel()
+ @opStack = []
+ @history = []
+
+ @registerOperationCommands
+ open: (e) => new Command(@editor, @)
+
+ destroy: ->
+ @subscriptions.dispose()
+
+ getExHistoryItem: (index) ->
+ @globalExState.commandHistory[index]
+
+ pushExHistory: (command) ->
+ @globalExState.commandHistory.unshift command
+
+ registerOperationCommands: (commands) ->
+ for commandName, fn of commands
+ do (fn) =>
+ pushFn = (e) => @pushOperations(fn(e))
+ @subscriptions.add(
+ atom.commands.add(@editorElement, "ex-mode:#{commandName}", pushFn)
+ )
+
+ onDidFailToExecute: (fn) ->
+ @emitter.on('failed-to-execute', fn)
+
+ pushOperations: (operations) ->
+ @opStack.push operations
+
+ @processOpStack() if @opStack.length == 2
+
+ clearOpStack: ->
+ @opStack = []
+
+ processOpStack: ->
+ [command, input] = @opStack
+ if input.characters.length > 0
+ @history.unshift command
+ try
+ command.execute(input)
+ catch e
+ if (e instanceof CommandError)
+ atom.notifications.addError("Command error: #{e.message}")
+ @emitter.emit('failed-to-execute')
+ else
+ throw e
+ @clearOpStack()
+
+module.exports = ExState