New command defining format, minor improvements

- If the second address is empty, it is assumed to be `.`
- Regex addresses and `:substitute` now integrate with search history and
  respect case sensitivity settings
- Patterns for `:substitute` can't be delimited by
- `:set` now supports inverting options using `:set inv{option}` and
  `:set {option}!`
- New commands: `:new`, `:vnew`, `:exit`, `:xall`, `:wall`, `:qall`, `:update`
This commit is contained in:
jazzpi 2015-07-31 12:01:47 +02:00
parent 962e4a35ba
commit 91f3f82730
10 changed files with 862 additions and 400 deletions

View file

@ -0,0 +1,185 @@
helpers = require './spec-helper'
Command = require '../lib/command'
ExCommands = require('../lib/ex-commands')
describe "command parsing", ->
[editor, editorElement, vimState, exState, command, lines] = []
beforeEach ->
vimMode = atom.packages.loadPackage('vim-mode')
exMode = atom.packages.loadPackage('ex-mode')
exMode.activate()
waitsForPromise ->
vimMode.activate().then ->
helpers.activateExMode().then ->
helpers.getEditorElement (element) ->
editorElement = element
editor = editorElement.getModel()
atom.commands.dispatch(element, 'ex-mode:open')
atom.commands.dispatch(editor.normalModeInputView.editorElement,
'core:cancel')
vimState = vimMode.mainModule.getEditorState(editor)
exState = exMode.mainModule.exStates.get(editor)
command = new Command(editor, exState)
vimState.activateNormalMode()
vimState.resetNormalMode()
editor.setText(
'abc\nabc\nabc\nabc\nabc\nabc\nabc\nabc\nabc\nabc\nabc\nabc'
'\nabc\nabc\n')
lines = editor.getBuffer().getLines()
editor.setCursorBufferPosition([0, 0])
it "parses a simple command (e.g. `:quit`)", ->
expect(command.parseLine('quit')).toEqual
command: ExCommands.commands.quit.callback
args: ''
range: [0, 0]
it "matches sub-commands (e.g. `:q`)", ->
expect(command.parseLine('q')).toEqual
command: ExCommands.commands.quit.callback
args: ''
range: [0, 0]
it "uses the command with the highest priority if multiple match an input", ->
expect(command.parseLine('s').command)
.toEqual(ExCommands.commands.substitute.callback)
it "ignores leading blanks and spaces", ->
expect(command.parseLine(':::: :::: : : : ')).toBeUndefined
expect(command.parseLine(':: :::::: :quit')).toEqual
command: ExCommands.commands.quit.callback
args: ''
range: [0, 0]
expect(atom.notifications.notifications.length).toBe(0)
it 'ignores the line if it starts with a "', ->
expect(command.parseLine('"quit')).toBe(undefined)
expect(atom.notifications.notifications.length).toBe(0)
describe "address parsing", ->
describe "with only one address", ->
it "parses . as an address", ->
expect(command.parseLine('.').range).toEqual([0, 0])
editor.setCursorBufferPosition([2, 0])
expect(command.parseLine('.').range).toEqual([2, 2])
it "parses $ as an address", ->
expect(command.parseLine('$').range)
.toEqual([lines.length - 1, lines.length - 1])
it "parses a number as an address", ->
expect(command.parseLine('3').range).toEqual([2, 2])
expect(command.parseLine('7').range).toEqual([6, 6])
it "parses 'a as an address", ->
vimState.setMark('a', [3, 1])
expect(command.parseLine("'a").range).toEqual([3, 3])
it "throws an error if the mark is not set", ->
vimState.marks.a = undefined
expect(-> command.parseLine("'a")).toThrow()
it "parses /a and ?a as addresses", ->
expect(command.parseLine('/abc').range).toEqual([1, 1])
editor.setCursorBufferPosition([1, 0])
expect(command.parseLine('?abc').range).toEqual([0, 0])
editor.setCursorBufferPosition([0, 0])
expect(command.parseLine('/bc').range).toEqual([1, 1])
it "integrates the search history for :/", ->
vimState.pushSearchHistory('abc')
expect(command.parseLine('//').range).toEqual([1, 1])
command.parseLine('/ab/,/bc/+2')
expect(vimState.getSearchHistoryItem(0)).toEqual('bc')
describe "case sensitivity for search patterns", ->
beforeEach ->
editor.setText('abca\nABC\ndefdDEF\nabcaABC')
describe "respects the smartcase setting", ->
describe "with smartcasse off", ->
beforeEach ->
atom.config.set('vim-mode.useSmartcaseForSearch', false)
editor.setCursorBufferPosition([0, 0])
it "uses case sensitive search if pattern is lowercase", ->
expect(command.parseLine('/abc').range).toEqual([3, 3])
it "uses case sensitive search if the pattern is uppercase", ->
expect(command.parseLine('/ABC').range).toEqual([1, 1])
describe "with smartcase on", ->
beforeEach ->
atom.config.set('vim-mode.useSmartcaseForSearch', true)
it "uses case insensitive search if the pattern is lowercase", ->
editor.setCursorBufferPosition([0, 0])
expect(command.parseLine('/abc').range).toEqual([1, 1])
it "uses case sensitive search if the pattern is uppercase", ->
editor.setCursorBufferPosition([3, 3])
expect(command.parseLine('/ABC').range).toEqual([1, 1])
describe "\\c and \\C", ->
describe "only \\c in the pattern", ->
beforeEach ->
atom.config.set('vim-mode.useSmartcaseForSearch', false)
editor.setCursorBufferPosition([0, 0])
it "uses case insensitive search if smartcase is off", ->
expect(command.parseLine('/abc\\c').range).toEqual([1, 1])
it "doesn't matter where it is", ->
expect(command.parseLine('/ab\\cc').range).toEqual([1, 1])
describe "only \\C in the pattern with smartcase on", ->
beforeEach ->
atom.config.set('vim-mode.useSmartcaseForSearch', true)
editor.setCursorBufferPosition([0, 0])
it "uses case sensitive search if the pattern is lowercase", ->
expect(command.parseLine('/abc\\C').range).toEqual([3, 3])
it "doesn't matter where it is", ->
expect(command.parseLine('/ab\\Cc').range).toEqual([3, 3])
describe "with \\c and \\C in the pattern", ->
beforeEach ->
atom.config.set('vim-mode.useSmartcaseForSearch', false)
editor.setCursorBufferPosition([0, 0])
it "uses case insensitive search if \\C comes first", ->
expect(command.parseLine('/a\\Cb\\cc').range).toEqual([1, 1])
it "uses case insensitive search if \\c comes first", ->
expect(command.parseLine('/a\\cb\\Cc').range).toEqual([1, 1])
describe "with two addresses", ->
it "parses both", ->
expect(command.parseLine('5,10').range).toEqual([4, 9])
it "throws an error if it is in reverse order", ->
expect(-> command.parseLine('10,5').range).toThrow()
it "uses the current line as second address if empty", ->
editor.setCursorBufferPosition([3, 0])
expect(command.parseLine('-2,').range).toEqual([1, 3])
it "parses a command with a range and no arguments", ->
expect(command.parseLine('2,/abc/+4delete')).toEqual
command: ExCommands.commands.delete.callback
args: ''
range: [1, 5]
it "parses a command with no range and arguments", ->
expect(command.parseLine('edit edit-test test-2')).toEqual
command: ExCommands.commands.edit.callback
args: 'edit-test test-2'
range: [0, 0]
it "parses a command with range and arguments", ->
expect(command.parseLine('3,5+2s/abc/def/gi')).toEqual
command: ExCommands.commands.substitute.callback
args: '/abc/def/gi'
range: [2, 6]

View file

@ -3,8 +3,7 @@ path = require 'path'
os = require 'os'
uuid = require 'node-uuid'
helpers = require './spec-helper'
Ex = require('../lib/ex').singleton()
ExCommands = require('../lib/ex-commands')
describe "the commands", ->
[editor, editorElement, vimState, exState, dir, dir2] = []
@ -177,11 +176,25 @@ describe "the commands", ->
describe ":tabclose", ->
it "acts as an alias to :quit", ->
spyOn(Ex, 'tabclose').andCallThrough()
spyOn(Ex, 'quit').andCallThrough()
spyOn(ExCommands.commands.tabclose, 'callback').andCallThrough()
spyOn(ExCommands.commands.quit, 'callback').andCallThrough()
keydown(':')
submitNormalModeInputText('tabclose')
expect(Ex.quit).toHaveBeenCalledWith(Ex.tabclose.calls[0].args...)
expect(ExCommands.commands.quit.callback)
.toHaveBeenCalledWith(ExCommands.commands.tabclose.callback
.calls[0].args[0])
describe ":qall", ->
beforeEach ->
waitsForPromise ->
atom.workspace.open().then -> atom.workspace.open()
.then -> atom.workspace.open()
it "closes the window", ->
spyOn(atom, 'close')
keydown(':')
submitNormalModeInputText('qall')
expect(atom.close).toHaveBeenCalled()
describe ":tabnext", ->
pane = null
@ -223,45 +236,81 @@ describe "the commands", ->
submitNormalModeInputText('tabprevious')
expect(pane.getActiveItemIndex()).toBe(pane.getItems().length - 1)
describe ":update", ->
it "acts as an alias to :write", ->
spyOn(ExCommands.commands.update, 'callback')
.andCallThrough()
spyOn(ExCommands.commands.write, 'callback')
keydown(':')
submitNormalModeInputText('update')
expect(ExCommands.commands.write.callback).toHaveBeenCalledWith(
ExCommands.commands.update.callback.calls[0].args[0])
describe ":wall", ->
it "saves all open files", ->
spyOn(atom.workspace, 'saveAll')
keydown(':')
submitNormalModeInputText('wall')
expect(atom.workspace.saveAll).toHaveBeenCalled()
describe ":wq", ->
beforeEach ->
spyOn(Ex, 'write').andCallThrough()
spyOn(Ex, 'quit')
spyOn(ExCommands.commands.write, 'callback').andCallThrough()
spyOn(ExCommands.commands.quit, 'callback')
it "writes the file, then quits", ->
spyOn(atom, 'showSaveDialogSync').andReturn(projectPath('wq-1'))
keydown(':')
submitNormalModeInputText('wq')
expect(Ex.write).toHaveBeenCalled()
expect(ExCommands.commands.write.callback).toHaveBeenCalled()
# Since `:wq` only calls `:quit` after `:write` is finished, we need to
# wait a bit for the `:quit` call to occur
waitsFor((-> Ex.quit.wasCalled), "the :quit command to be called", 100)
waitsFor((-> ExCommands.commands.quit.callback.wasCalled),
"the :quit command to be called", 100)
it "doesn't quit when the file is new and no path is specified in the save dialog", ->
spyOn(atom, 'showSaveDialogSync').andReturn(undefined)
keydown(':')
submitNormalModeInputText('wq')
expect(Ex.write).toHaveBeenCalled()
expect(ExCommands.commands.write.callback).toHaveBeenCalled()
wasNotCalled = false
# FIXME: This seems dangerous, but setTimeout somehow doesn't work.
setImmediate((->
wasNotCalled = not Ex.quit.wasCalled))
wasNotCalled = not ExCommands.commands.quit.callback.wasCalled))
waitsFor((-> wasNotCalled), 100)
it "passes the file name", ->
keydown(':')
submitNormalModeInputText('wq wq-2')
expect(Ex.write)
expect(ExCommands.commands.write.callback)
.toHaveBeenCalled()
expect(Ex.write.calls[0].args[1].trim()).toEqual('wq-2')
waitsFor((-> Ex.quit.wasCalled), "the :quit command to be called", 100)
expect(ExCommands.commands.write.callback.calls[0].args[0].args)
.toEqual('wq-2')
waitsFor((-> ExCommands.commands.quit.callback.wasCalled),
"the :quit command to be called", 100)
describe ":xit", ->
it "acts as an alias to :wq", ->
spyOn(Ex, 'wq')
spyOn(ExCommands.commands.wq, 'callback')
keydown(':')
submitNormalModeInputText('xit')
expect(Ex.wq).toHaveBeenCalled()
expect(ExCommands.commands.wq.callback).toHaveBeenCalled()
describe ":exit", ->
it "is an alias to :xit", ->
spyOn(ExCommands.commands.xit, 'callback')
keydown(':')
submitNormalModeInputText('exit')
expect(ExCommands.commands.xit.callback).toHaveBeenCalled()
describe ":xall", ->
it "saves all open files and closes the window", ->
spyOn(atom.workspace, 'saveAll')
spyOn(atom, 'close')
keydown(':')
submitNormalModeInputText('xall')
expect(atom.workspace.saveAll).toHaveBeenCalled()
expect(atom.close).toHaveBeenCalled()
describe ":edit", ->
describe "without a file name", ->
@ -339,19 +388,20 @@ describe "the commands", ->
describe ":tabedit", ->
it "acts as an alias to :edit if supplied with a path", ->
spyOn(Ex, 'tabedit').andCallThrough()
spyOn(Ex, 'edit')
spyOn(ExCommands.commands.tabedit, 'callback').andCallThrough()
spyOn(ExCommands.commands.edit, 'callback')
keydown(':')
submitNormalModeInputText('tabedit tabedit-test')
expect(Ex.edit).toHaveBeenCalledWith(Ex.tabedit.calls[0].args...)
expect(ExCommands.commands.edit.callback).toHaveBeenCalledWith(
ExCommands.commands.tabedit.callback.calls[0].args...)
it "acts as an alias to :tabnew if not supplied with a path", ->
spyOn(Ex, 'tabedit').andCallThrough()
spyOn(Ex, 'tabnew')
spyOn(ExCommands.commands.tabedit, 'callback').andCallThrough()
spyOn(ExCommands.commands.tabnew, 'callback')
keydown(':')
submitNormalModeInputText('tabedit ')
expect(Ex.tabnew)
.toHaveBeenCalledWith(Ex.tabedit.calls[0].args...)
expect(ExCommands.commands.tabnew.callback).toHaveBeenCalledWith(
ExCommands.commands.tabedit.callback.calls[0].args...)
describe ":tabnew", ->
it "opens a new tab", ->
@ -372,6 +422,15 @@ describe "the commands", ->
# FIXME: Should test whether the new pane contains a TextEditor
# pointing to the same path
describe ":new", ->
it "splits a new file upwards", ->
pane = atom.workspace.getActivePane()
spyOn(pane, 'splitUp').andCallThrough()
keydown(':')
submitNormalModeInputText('new')
expect(pane.splitUp).toHaveBeenCalled()
# FIXME: Should test whether the new pane contains an empty file
describe ":vsplit", ->
it "splits the current file to the left", ->
pane = atom.workspace.getActivePane()
@ -384,6 +443,15 @@ describe "the commands", ->
# FIXME: Should test whether the new pane contains a TextEditor
# pointing to the same path
describe ":vnew", ->
it "splits a new file to the left", ->
pane = atom.workspace.getActivePane()
spyOn(pane, 'splitLeft').andCallThrough()
keydown(':')
submitNormalModeInputText('vnew')
expect(pane.splitLeft).toHaveBeenCalled()
# FIXME: Should test whether the new pane contains an empty file
describe ":delete", ->
beforeEach ->
editor.setText('abc\ndef\nghi\njkl')
@ -449,13 +517,89 @@ describe "the commands", ->
submitNormalModeInputText(':%substitute/abc/ghi/ig')
expect(editor.getText()).toEqual('ghiaghi\ndefdDEF\nghiaghi')
it "can't be delimited by letters", ->
it "can't be delimited by letters or \\", ->
keydown(':')
submitNormalModeInputText(':substitute nanxngi')
expect(atom.notifications.notifications[0].message).toEqual(
"Command error: Regular expressions can't be delimited by letters")
expect(editor.getText()).toEqual('abcaABC\ndefdDEF\nabcaABC')
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText(':substitute\\a\\x\\gi')
expect(atom.notifications.notifications[1].message).toEqual(
"Command error: Regular expressions can't be delimited by \\")
expect(editor.getText()).toEqual('abcaABC\ndefdDEF\nabcaABC')
describe "case sensitivity", ->
describe "respects the smartcase setting", ->
beforeEach ->
editor.setText('abcaABC\ndefdDEF\nabcaABC')
it "uses case sensitive search if smartcase is off and the pattern is lowercase", ->
atom.config.set('vim-mode.useSmartcaseForSearch', false)
keydown(':')
submitNormalModeInputText(':substitute/abc/ghi/g')
expect(editor.getText()).toEqual('ghiaABC\ndefdDEF\nabcaABC')
it "uses case sensitive search if smartcase is off and the pattern is uppercase", ->
editor.setText('abcaABC\ndefdDEF\nabcaABC')
keydown(':')
submitNormalModeInputText(':substitute/ABC/ghi/g')
expect(editor.getText()).toEqual('abcaghi\ndefdDEF\nabcaABC')
it "uses case insensitive search if smartcase is on and the pattern is lowercase", ->
editor.setText('abcaABC\ndefdDEF\nabcaABC')
atom.config.set('vim-mode.useSmartcaseForSearch', true)
keydown(':')
submitNormalModeInputText(':substitute/abc/ghi/g')
expect(editor.getText()).toEqual('ghiaghi\ndefdDEF\nabcaABC')
it "uses case sensitive search if smartcase is on and the pattern is uppercase", ->
editor.setText('abcaABC\ndefdDEF\nabcaABC')
keydown(':')
submitNormalModeInputText(':substitute/ABC/ghi/g')
expect(editor.getText()).toEqual('abcaghi\ndefdDEF\nabcaABC')
describe "\\c and \\C in the pattern", ->
beforeEach ->
editor.setText('abcaABC\ndefdDEF\nabcaABC')
it "uses case insensitive search if smartcase is off and \c is in the pattern", ->
atom.config.set('vim-mode.useSmartcaseForSearch', false)
keydown(':')
submitNormalModeInputText(':substitute/abc\\c/ghi/g')
expect(editor.getText()).toEqual('ghiaghi\ndefdDEF\nabcaABC')
it "doesn't matter where in the pattern \\c is", ->
atom.config.set('vim-mode.useSmartcaseForSearch', false)
keydown(':')
submitNormalModeInputText(':substitute/a\\cbc/ghi/g')
expect(editor.getText()).toEqual('ghiaghi\ndefdDEF\nabcaABC')
it "uses case sensitive search if smartcase is on, \\C is in the pattern and the pattern is lowercase", ->
atom.config.set('vim-mode.useSmartcaseForSearch', true)
keydown(':')
submitNormalModeInputText(':substitute/a\\Cbc/ghi/g')
expect(editor.getText()).toEqual('ghiaABC\ndefdDEF\nabcaABC')
it "overrides \\C with \\c if \\C comes first", ->
atom.config.set('vim-mode.useSmartcaseForSearch', true)
keydown(':')
submitNormalModeInputText(':substitute/a\\Cb\\cc/ghi/g')
expect(editor.getText()).toEqual('ghiaghi\ndefdDEF\nabcaABC')
it "overrides \\C with \\c if \\c comes first", ->
atom.config.set('vim-mode.useSmartcaseForSearch', true)
keydown(':')
submitNormalModeInputText(':substitute/a\\cb\\Cc/ghi/g')
expect(editor.getText()).toEqual('ghiaghi\ndefdDEF\nabcaABC')
it "overrides an appended /i flag with \\C", ->
atom.config.set('vim-mode.useSmartcaseForSearch', true)
keydown(':')
submitNormalModeInputText(':substitute/ab\\Cc/ghi/gi')
expect(editor.getText()).toEqual('ghiaABC\ndefdDEF\nabcaABC')
describe "capturing groups", ->
beforeEach ->
editor.setText('abcaABC\ndefdDEF\nabcaABC')
@ -478,7 +622,7 @@ describe "the commands", ->
describe ":set", ->
it "throws an error without a specified option", ->
keydown(':')
submitNormalModeInputText(':set')
submitNormalModeInputText('set')
expect(atom.notifications.notifications[0].message).toEqual(
'Command error: No option specified')
@ -486,33 +630,47 @@ describe "the commands", ->
atom.config.set('editor.showInvisibles', false)
atom.config.set('editor.showLineNumbers', false)
keydown(':')
submitNormalModeInputText(':set list number')
submitNormalModeInputText('set list number')
expect(atom.config.get('editor.showInvisibles')).toBe(true)
expect(atom.config.get('editor.showLineNumbers')).toBe(true)
describe "the options", ->
beforeEach ->
atom.config.set('editor.showInvisibles', false)
atom.config.set('editor.showLineNumbers', false)
it "sets options to false with no{option}", ->
atom.config.set('editor.showInvisibles', true)
keydown(':')
submitNormalModeInputText('set nolist')
expect(atom.config.get('editor.showInvisibles')).toBe(false)
it "sets (no)list", ->
it "inverts options with inv{option}", ->
atom.config.set('editor.showInvisibles', true)
keydown(':')
submitNormalModeInputText('set invlist')
expect(atom.config.get('editor.showInvisibles')).toBe(false)
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText('set invlist')
expect(atom.config.get('editor.showInvisibles')).toBe(true)
it "inverts options with {option}!", ->
atom.config.set('editor.showInvisibles', true)
keydown(':')
submitNormalModeInputText('set list!')
expect(atom.config.get('editor.showInvisibles')).toBe(false)
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText('set list!')
expect(atom.config.get('editor.showInvisibles')).toBe(true)
describe "the options", ->
it "sets list", ->
atom.config.set('editor.showInvisibles', false)
keydown(':')
submitNormalModeInputText(':set list')
expect(atom.config.get('editor.showInvisibles')).toBe(true)
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText(':set nolist')
expect(atom.config.get('editor.showInvisibles')).toBe(false)
it "sets (no)nu(mber)", ->
it "sets nu[mber]", ->
atom.config.set('editor.showLineNumbers', false)
keydown(':')
submitNormalModeInputText(':set nu')
expect(atom.config.get('editor.showLineNumbers')).toBe(true)
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText(':set nonu')
expect(atom.config.get('editor.showLineNumbers')).toBe(false)
atom.config.set('editor.showLineNumbers', false)
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText(':set number')
expect(atom.config.get('editor.showLineNumbers')).toBe(true)
atom.commands.dispatch(editorElement, 'ex-mode:open')
submitNormalModeInputText(':set nonumber')
expect(atom.config.get('editor.showLineNumbers')).toBe(false)