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:
parent
472ec2140e
commit
af0ba7c01c
3 changed files with 58 additions and 49 deletions
|
|
@ -150,7 +150,7 @@ class Command
|
||||||
|
|
||||||
# If the command matches an existing one exactly, execute that one
|
# If the command matches an existing one exactly, execute that one
|
||||||
if (func = Ex.singleton()[command])?
|
if (func = Ex.singleton()[command])?
|
||||||
func(range, args)
|
func({ range, args, @vimState, @exState, @editor })
|
||||||
else
|
else
|
||||||
# Step 8: Match command against existing commands
|
# Step 8: Match command against existing commands
|
||||||
matching = (name for name, val of Ex.singleton() when \
|
matching = (name for name, val of Ex.singleton() when \
|
||||||
|
|
@ -162,7 +162,7 @@ class Command
|
||||||
|
|
||||||
func = Ex.singleton()[command]
|
func = Ex.singleton()[command]
|
||||||
if func?
|
if func?
|
||||||
func(range, args)
|
func({ range, args, @vimState, @exState, @editor })
|
||||||
else
|
else
|
||||||
throw new CommandError("Not an editor command: #{input.characters}")
|
throw new CommandError("Not an editor command: #{input.characters}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,7 @@ trySave = (func) ->
|
||||||
|
|
||||||
deferred.promise
|
deferred.promise
|
||||||
|
|
||||||
saveAs = (filePath) ->
|
saveAs = (filePath, editor) ->
|
||||||
editor = atom.workspace.getActiveTextEditor()
|
|
||||||
fs.writeFileSync(filePath, editor.getText())
|
fs.writeFileSync(filePath, editor.getText())
|
||||||
|
|
||||||
getFullPath = (filePath) ->
|
getFullPath = (filePath) ->
|
||||||
|
|
@ -76,21 +75,21 @@ class Ex
|
||||||
|
|
||||||
q: => @quit()
|
q: => @quit()
|
||||||
|
|
||||||
tabedit: (range, args) =>
|
tabedit: (args) =>
|
||||||
if args.trim() isnt ''
|
if args.args.trim() isnt ''
|
||||||
@edit(range, args)
|
@edit(args)
|
||||||
else
|
else
|
||||||
@tabnew(range, args)
|
@tabnew(args)
|
||||||
|
|
||||||
tabe: (args...) => @tabedit(args...)
|
tabe: (args) => @tabedit(args)
|
||||||
|
|
||||||
tabnew: (range, args) =>
|
tabnew: ({ range, args }) =>
|
||||||
if args.trim() is ''
|
if args.trim() is ''
|
||||||
atom.workspace.open()
|
atom.workspace.open()
|
||||||
else
|
else
|
||||||
@tabedit(range, args)
|
@tabedit(range, args)
|
||||||
|
|
||||||
tabclose: (args...) => @quit(args...)
|
tabclose: (args) => @quit(args)
|
||||||
|
|
||||||
tabc: => @tabclose()
|
tabc: => @tabclose()
|
||||||
|
|
||||||
|
|
@ -106,15 +105,14 @@ class Ex
|
||||||
|
|
||||||
tabp: => @tabprevious()
|
tabp: => @tabprevious()
|
||||||
|
|
||||||
edit: (range, filePath) ->
|
edit: ({ range, args, editor }) ->
|
||||||
filePath = filePath.trim()
|
filePath = args.trim()
|
||||||
if filePath[0] is '!'
|
if filePath[0] is '!'
|
||||||
force = true
|
force = true
|
||||||
filePath = filePath[1..].trim()
|
filePath = filePath[1..].trim()
|
||||||
else
|
else
|
||||||
force = false
|
force = false
|
||||||
|
|
||||||
editor = atom.workspace.getActiveTextEditor()
|
|
||||||
if editor.isModified() and not force
|
if editor.isModified() and not force
|
||||||
throw new CommandError('No write since last change (add ! to override)')
|
throw new CommandError('No write since last change (add ! to override)')
|
||||||
if filePath.indexOf(' ') isnt -1
|
if filePath.indexOf(' ') isnt -1
|
||||||
|
|
@ -132,14 +130,15 @@ class Ex
|
||||||
else
|
else
|
||||||
throw new CommandError('No file name')
|
throw new CommandError('No file name')
|
||||||
|
|
||||||
e: (args...) => @edit(args...)
|
e: (args) => @edit(args)
|
||||||
|
|
||||||
enew: ->
|
enew: ->
|
||||||
buffer = atom.workspace.getActiveTextEditor().buffer
|
buffer = atom.workspace.getActiveTextEditor().buffer
|
||||||
buffer.setPath(undefined)
|
buffer.setPath(undefined)
|
||||||
buffer.load()
|
buffer.load()
|
||||||
|
|
||||||
write: (range, filePath) ->
|
write: ({ range, args, editor }) ->
|
||||||
|
filePath = args
|
||||||
if filePath[0] is '!'
|
if filePath[0] is '!'
|
||||||
force = true
|
force = true
|
||||||
filePath = filePath[1..]
|
filePath = filePath[1..]
|
||||||
|
|
@ -166,22 +165,22 @@ class Ex
|
||||||
if not saved and fullPath?
|
if not saved and fullPath?
|
||||||
if not force and fs.existsSync(fullPath)
|
if not force and fs.existsSync(fullPath)
|
||||||
throw new CommandError("File exists (add ! to override)")
|
throw new CommandError("File exists (add ! to override)")
|
||||||
trySave(-> saveAs(fullPath)).then(deferred.resolve)
|
trySave(-> saveAs(fullPath, editor)).then(deferred.resolve)
|
||||||
|
|
||||||
deferred.promise
|
deferred.promise
|
||||||
|
|
||||||
w: (args...) =>
|
w: (args) =>
|
||||||
@write(args...)
|
@write(args)
|
||||||
|
|
||||||
wq: (args...) =>
|
wq: (args) =>
|
||||||
@write(args...).then => @quit()
|
@write(args).then => @quit()
|
||||||
|
|
||||||
xit: (args...) => @wq(args...)
|
xit: (args) => @wq(args)
|
||||||
|
|
||||||
wa: ->
|
wa: ->
|
||||||
atom.workspace.saveAll()
|
atom.workspace.saveAll()
|
||||||
|
|
||||||
split: (range, args) ->
|
split: ({ range, args }) ->
|
||||||
args = args.trim()
|
args = args.trim()
|
||||||
filePaths = args.split(' ')
|
filePaths = args.split(' ')
|
||||||
filePaths = undefined if filePaths.length is 1 and filePaths[0] is ''
|
filePaths = undefined if filePaths.length is 1 and filePaths[0] is ''
|
||||||
|
|
@ -194,9 +193,9 @@ class Ex
|
||||||
else
|
else
|
||||||
pane.splitUp(copyActiveItem: true)
|
pane.splitUp(copyActiveItem: true)
|
||||||
|
|
||||||
sp: (args...) => @split(args...)
|
sp: (args) => @split(args)
|
||||||
|
|
||||||
substitute: (range, args) ->
|
substitute: ({ range, args, editor, vimState }) ->
|
||||||
args = args.trimLeft()
|
args = args.trimLeft()
|
||||||
delim = args[0]
|
delim = args[0]
|
||||||
if /[a-z1-9\\"|]/i.test(delim)
|
if /[a-z1-9\\"|]/i.test(delim)
|
||||||
|
|
@ -240,9 +239,9 @@ class Ex
|
||||||
replace(replaceGroups(match[..], spl[1]))
|
replace(replaceGroups(match[..], spl[1]))
|
||||||
)
|
)
|
||||||
|
|
||||||
s: (args...) => @substitute(args...)
|
s: (args) => @substitute(args)
|
||||||
|
|
||||||
vsplit: (range, args) ->
|
vsplit: ({ range, args }) ->
|
||||||
args = args.trim()
|
args = args.trim()
|
||||||
filePaths = args.split(' ')
|
filePaths = args.split(' ')
|
||||||
filePaths = undefined if filePaths.length is 1 and filePaths[0] is ''
|
filePaths = undefined if filePaths.length is 1 and filePaths[0] is ''
|
||||||
|
|
@ -255,13 +254,13 @@ class Ex
|
||||||
else
|
else
|
||||||
pane.splitLeft(copyActiveItem: true)
|
pane.splitLeft(copyActiveItem: true)
|
||||||
|
|
||||||
vsp: (args...) => @vsplit(args...)
|
vsp: (args) => @vsplit(args)
|
||||||
|
|
||||||
delete: (range) ->
|
delete: ({ range }) ->
|
||||||
range = [[range[0], 0], [range[1] + 1, 0]]
|
range = [[range[0], 0], [range[1] + 1, 0]]
|
||||||
atom.workspace.getActiveTextEditor().buffer.setTextInRange(range, '')
|
atom.workspace.getActiveTextEditor().buffer.setTextInRange(range, '')
|
||||||
|
|
||||||
set: (range, args) ->
|
set: ({ range, args }) ->
|
||||||
args = args.trim()
|
args = args.trim()
|
||||||
if args == ""
|
if args == ""
|
||||||
throw new CommandError("No option specified")
|
throw new CommandError("No option specified")
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,21 @@ describe "the commands", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
vimMode = atom.packages.loadPackage('vim-mode')
|
vimMode = atom.packages.loadPackage('vim-mode')
|
||||||
exMode = atom.packages.loadPackage('ex-mode')
|
exMode = atom.packages.loadPackage('ex-mode')
|
||||||
exMode.activate()
|
waitsForPromise ->
|
||||||
|
activationPromise = exMode.activate()
|
||||||
|
helpers.activateExMode()
|
||||||
|
activationPromise
|
||||||
|
|
||||||
|
runs ->
|
||||||
|
spyOn(exMode.mainModule.globalExState, 'setVim').andCallThrough()
|
||||||
|
|
||||||
waitsForPromise ->
|
waitsForPromise ->
|
||||||
vimMode.activate().then ->
|
vimMode.activate()
|
||||||
helpers.activateExMode().then ->
|
|
||||||
|
waitsFor ->
|
||||||
|
exMode.mainModule.globalExState.setVim.calls.length > 0
|
||||||
|
|
||||||
|
runs ->
|
||||||
dir = path.join(os.tmpdir(), "atom-ex-mode-spec-#{uuid.v4()}")
|
dir = path.join(os.tmpdir(), "atom-ex-mode-spec-#{uuid.v4()}")
|
||||||
dir2 = 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(dir)
|
||||||
|
|
@ -24,7 +34,7 @@ describe "the commands", ->
|
||||||
atom.project.setPaths([dir, dir2])
|
atom.project.setPaths([dir, dir2])
|
||||||
|
|
||||||
helpers.getEditorElement (element) ->
|
helpers.getEditorElement (element) ->
|
||||||
atom.commands.dispatch(element, 'ex-mode:open')
|
atom.commands.dispatch(element, "ex-mode:open")
|
||||||
keydown('escape')
|
keydown('escape')
|
||||||
editorElement = element
|
editorElement = element
|
||||||
editor = editorElement.getModel()
|
editor = editorElement.getModel()
|
||||||
|
|
@ -253,7 +263,7 @@ describe "the commands", ->
|
||||||
submitNormalModeInputText('wq wq-2')
|
submitNormalModeInputText('wq wq-2')
|
||||||
expect(Ex.write)
|
expect(Ex.write)
|
||||||
.toHaveBeenCalled()
|
.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)
|
waitsFor((-> Ex.quit.wasCalled), "the :quit command to be called", 100)
|
||||||
|
|
||||||
describe ":xit", ->
|
describe ":xit", ->
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue