Improve format for calling commands

Commands (from the Ex class) are now called with an object
containing the range, arguments, vim state, ex state and
editor instead of a long list of arguments.
This commit is contained in:
jazzpi 2015-11-21 14:48:13 +01:00
parent 472ec2140e
commit af0ba7c01c
3 changed files with 58 additions and 49 deletions

View file

@ -150,7 +150,7 @@ class Command
# If the command matches an existing one exactly, execute that one
if (func = Ex.singleton()[command])?
func(range, args)
func({ range, args, @vimState, @exState, @editor })
else
# Step 8: Match command against existing commands
matching = (name for name, val of Ex.singleton() when \
@ -162,7 +162,7 @@ class Command
func = Ex.singleton()[command]
if func?
func(range, args)
func({ range, args, @vimState, @exState, @editor })
else
throw new CommandError("Not an editor command: #{input.characters}")

View file

@ -32,8 +32,7 @@ trySave = (func) ->
deferred.promise
saveAs = (filePath) ->
editor = atom.workspace.getActiveTextEditor()
saveAs = (filePath, editor) ->
fs.writeFileSync(filePath, editor.getText())
getFullPath = (filePath) ->
@ -76,21 +75,21 @@ class Ex
q: => @quit()
tabedit: (range, args) =>
if args.trim() isnt ''
@edit(range, args)
tabedit: (args) =>
if args.args.trim() isnt ''
@edit(args)
else
@tabnew(range, args)
@tabnew(args)
tabe: (args...) => @tabedit(args...)
tabe: (args) => @tabedit(args)
tabnew: (range, args) =>
tabnew: ({ range, args }) =>
if args.trim() is ''
atom.workspace.open()
else
@tabedit(range, args)
tabclose: (args...) => @quit(args...)
tabclose: (args) => @quit(args)
tabc: => @tabclose()
@ -106,15 +105,14 @@ class Ex
tabp: => @tabprevious()
edit: (range, filePath) ->
filePath = filePath.trim()
edit: ({ range, args, editor }) ->
filePath = args.trim()
if filePath[0] is '!'
force = true
filePath = filePath[1..].trim()
else
force = false
editor = atom.workspace.getActiveTextEditor()
if editor.isModified() and not force
throw new CommandError('No write since last change (add ! to override)')
if filePath.indexOf(' ') isnt -1
@ -132,14 +130,15 @@ class Ex
else
throw new CommandError('No file name')
e: (args...) => @edit(args...)
e: (args) => @edit(args)
enew: ->
buffer = atom.workspace.getActiveTextEditor().buffer
buffer.setPath(undefined)
buffer.load()
write: (range, filePath) ->
write: ({ range, args, editor }) ->
filePath = args
if filePath[0] is '!'
force = true
filePath = filePath[1..]
@ -166,22 +165,22 @@ class Ex
if not saved and fullPath?
if not force and fs.existsSync(fullPath)
throw new CommandError("File exists (add ! to override)")
trySave(-> saveAs(fullPath)).then(deferred.resolve)
trySave(-> saveAs(fullPath, editor)).then(deferred.resolve)
deferred.promise
w: (args...) =>
@write(args...)
w: (args) =>
@write(args)
wq: (args...) =>
@write(args...).then => @quit()
wq: (args) =>
@write(args).then => @quit()
xit: (args...) => @wq(args...)
xit: (args) => @wq(args)
wa: ->
atom.workspace.saveAll()
split: (range, args) ->
split: ({ range, args }) ->
args = args.trim()
filePaths = args.split(' ')
filePaths = undefined if filePaths.length is 1 and filePaths[0] is ''
@ -194,9 +193,9 @@ class Ex
else
pane.splitUp(copyActiveItem: true)
sp: (args...) => @split(args...)
sp: (args) => @split(args)
substitute: (range, args) ->
substitute: ({ range, args, editor, vimState }) ->
args = args.trimLeft()
delim = args[0]
if /[a-z1-9\\"|]/i.test(delim)
@ -240,9 +239,9 @@ class Ex
replace(replaceGroups(match[..], spl[1]))
)
s: (args...) => @substitute(args...)
s: (args) => @substitute(args)
vsplit: (range, args) ->
vsplit: ({ range, args }) ->
args = args.trim()
filePaths = args.split(' ')
filePaths = undefined if filePaths.length is 1 and filePaths[0] is ''
@ -255,13 +254,13 @@ class Ex
else
pane.splitLeft(copyActiveItem: true)
vsp: (args...) => @vsplit(args...)
vsp: (args) => @vsplit(args)
delete: (range) ->
delete: ({ range }) ->
range = [[range[0], 0], [range[1] + 1, 0]]
atom.workspace.getActiveTextEditor().buffer.setTextInRange(range, '')
set: (range, args) ->
set: ({ range, args }) ->
args = args.trim()
if args == ""
throw new CommandError("No option specified")

View file

@ -12,27 +12,37 @@ describe "the commands", ->
beforeEach ->
vimMode = atom.packages.loadPackage('vim-mode')
exMode = atom.packages.loadPackage('ex-mode')
exMode.activate()
waitsForPromise ->
activationPromise = exMode.activate()
helpers.activateExMode()
activationPromise
runs ->
spyOn(exMode.mainModule.globalExState, 'setVim').andCallThrough()
waitsForPromise ->
vimMode.activate().then ->
helpers.activateExMode().then ->
dir = path.join(os.tmpdir(), "atom-ex-mode-spec-#{uuid.v4()}")
dir2 = path.join(os.tmpdir(), "atom-ex-mode-spec-#{uuid.v4()}")
fs.makeTreeSync(dir)
fs.makeTreeSync(dir2)
atom.project.setPaths([dir, dir2])
vimMode.activate()
helpers.getEditorElement (element) ->
atom.commands.dispatch(element, 'ex-mode:open')
keydown('escape')
editorElement = element
editor = editorElement.getModel()
vimState = vimMode.mainModule.getEditorState(editor)
exState = exMode.mainModule.exStates.get(editor)
vimState.activateNormalMode()
vimState.resetNormalMode()
editor.setText("abc\ndef\nabc\ndef")
waitsFor ->
exMode.mainModule.globalExState.setVim.calls.length > 0
runs ->
dir = path.join(os.tmpdir(), "atom-ex-mode-spec-#{uuid.v4()}")
dir2 = path.join(os.tmpdir(), "atom-ex-mode-spec-#{uuid.v4()}")
fs.makeTreeSync(dir)
fs.makeTreeSync(dir2)
atom.project.setPaths([dir, dir2])
helpers.getEditorElement (element) ->
atom.commands.dispatch(element, "ex-mode:open")
keydown('escape')
editorElement = element
editor = editorElement.getModel()
vimState = vimMode.mainModule.getEditorState(editor)
exState = exMode.mainModule.exStates.get(editor)
vimState.activateNormalMode()
vimState.resetNormalMode()
editor.setText("abc\ndef\nabc\ndef")
afterEach ->
fs.removeSync(dir)
@ -253,7 +263,7 @@ describe "the commands", ->
submitNormalModeInputText('wq wq-2')
expect(Ex.write)
.toHaveBeenCalled()
expect(Ex.write.calls[0].args[1].trim()).toEqual('wq-2')
expect(Ex.write.calls[0].args[0].args.trim()).toEqual('wq-2')
waitsFor((-> Ex.quit.wasCalled), "the :quit command to be called", 100)
describe ":xit", ->