]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/lib/ex-state.coffee
Adds atom
[rbdr/dotfiles] / atom / packages / ex-mode / lib / ex-state.coffee
1 {Emitter, Disposable, CompositeDisposable} = require 'event-kit'
2
3 Command = require './command'
4 CommandError = require './command-error'
5
6 class ExState
7 constructor: (@editorElement, @globalExState) ->
8 @emitter = new Emitter
9 @subscriptions = new CompositeDisposable
10 @editor = @editorElement.getModel()
11 @opStack = []
12 @history = []
13
14 @registerOperationCommands
15 open: (e) => new Command(@editor, @)
16
17 destroy: ->
18 @subscriptions.dispose()
19
20 getExHistoryItem: (index) ->
21 @globalExState.commandHistory[index]
22
23 pushExHistory: (command) ->
24 @globalExState.commandHistory.unshift command
25
26 registerOperationCommands: (commands) ->
27 for commandName, fn of commands
28 do (fn) =>
29 pushFn = (e) => @pushOperations(fn(e))
30 @subscriptions.add(
31 atom.commands.add(@editorElement, "ex-mode:#{commandName}", pushFn)
32 )
33
34 onDidFailToExecute: (fn) ->
35 @emitter.on('failed-to-execute', fn)
36
37 pushOperations: (operations) ->
38 @opStack.push operations
39
40 @processOpStack() if @opStack.length == 2
41
42 clearOpStack: ->
43 @opStack = []
44
45 processOpStack: ->
46 [command, input] = @opStack
47 if input.characters.length > 0
48 @history.unshift command
49 try
50 command.execute(input)
51 catch e
52 if (e instanceof CommandError)
53 atom.notifications.addError("Command error: #{e.message}")
54 @emitter.emit('failed-to-execute')
55 else
56 throw e
57 @clearOpStack()
58
59 module.exports = ExState