mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Vim mode supports command mode and insert mode with i, esc, and x bindings
This commit is contained in:
@@ -23,6 +23,13 @@ describe "Editor", ->
|
||||
it "has a buffer", ->
|
||||
expect(editor.buffer).toBeDefined()
|
||||
|
||||
fdescribe '.set/getCursor', ->
|
||||
it "moves the cursor", ->
|
||||
editor.buffer.setText("012345")
|
||||
expect(editor.getCursor()).toEqual {column: 6, row: 0}
|
||||
editor.setCursor(column: 2, row: 0)
|
||||
expect(editor.getCursor()).toEqual {column: 2, row: 0}
|
||||
|
||||
describe 'destroy', ->
|
||||
it 'destroys the ace editor', ->
|
||||
spyOn(editor.aceEditor, 'destroy').andCallThrough()
|
||||
|
||||
46
spec/atom/vim-mode-spec.coffee
Normal file
46
spec/atom/vim-mode-spec.coffee
Normal file
@@ -0,0 +1,46 @@
|
||||
Editor = require 'editor'
|
||||
VimMode = require 'vim-mode'
|
||||
|
||||
describe "VimMode", ->
|
||||
editor = null
|
||||
|
||||
beforeEach ->
|
||||
editor = Editor.build()
|
||||
editor.enableKeymap()
|
||||
vimMode = new VimMode(editor)
|
||||
|
||||
describe "initialize", ->
|
||||
it "puts the editor in command-mode initially", ->
|
||||
expect(editor).toHaveClass 'command-mode'
|
||||
|
||||
describe "command-mode", ->
|
||||
describe "the i keybinding", ->
|
||||
it "puts the editor into insert mode", ->
|
||||
expect(editor).not.toHaveClass 'insert-mode'
|
||||
|
||||
editor.trigger keydownEvent('i')
|
||||
|
||||
expect(editor).toHaveClass 'insert-mode'
|
||||
expect(editor).not.toHaveClass 'command-mode'
|
||||
|
||||
fdescribe "the x keybinding", ->
|
||||
it "deletes a charachter", ->
|
||||
editor.buffer.setText("12345")
|
||||
editor.setCursor(column: 1, row: 0)
|
||||
|
||||
editor.trigger keydownEvent('x')
|
||||
|
||||
expect(editor.buffer.getText()).toBe '1345'
|
||||
expect(editor.getCursor()).toEqual(column: 1, row: 0)
|
||||
|
||||
describe "insert-mode", ->
|
||||
beforeEach ->
|
||||
editor.trigger keydownEvent('i')
|
||||
|
||||
it "puts the editor into command mode when <esc> is pressed", ->
|
||||
expect(editor).not.toHaveClass 'command-mode'
|
||||
|
||||
editor.trigger keydownEvent('<esc>')
|
||||
|
||||
expect(editor).toHaveClass 'command-mode'
|
||||
expect(editor).not.toHaveClass 'insert-mode'
|
||||
@@ -55,3 +55,14 @@ class Editor extends Template
|
||||
@buffer.url = url
|
||||
@buffer.save()
|
||||
|
||||
setCursor: ({column, row}) ->
|
||||
@aceEditor.moveCursorToPosition({column, row})
|
||||
|
||||
getCursor: ->
|
||||
@getAceSession().getSelection().getCursor()
|
||||
|
||||
delete: ->
|
||||
@aceEditor.remove 'right'
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ Editor = require 'editor'
|
||||
FileFinder = require 'file-finder'
|
||||
Project = require 'project'
|
||||
GlobalKeymap = require 'global-keymap'
|
||||
VimMode = require 'vim-mode'
|
||||
|
||||
module.exports =
|
||||
class RootView extends Template
|
||||
@@ -22,6 +23,7 @@ class RootView extends Template
|
||||
globalKeymap: null
|
||||
|
||||
initialize: ({url}) ->
|
||||
new VimMode(@editor)
|
||||
@editor.keyEventHandler = atom.globalKeymap
|
||||
@createProject(url)
|
||||
|
||||
|
||||
28
src/atom/vim-mode.coffee
Normal file
28
src/atom/vim-mode.coffee
Normal file
@@ -0,0 +1,28 @@
|
||||
module.exports =
|
||||
class VimMode
|
||||
editor: null
|
||||
|
||||
constructor: (@editor) ->
|
||||
atom.bindKeys '.command-mode'
|
||||
'i': 'insert-mode:activate'
|
||||
'x': 'command-mode:delete'
|
||||
|
||||
atom.bindKeys '.insert-mode'
|
||||
'<esc>': 'command-mode:activate'
|
||||
|
||||
@editor.addClass('command-mode')
|
||||
|
||||
@editor.on 'insert-mode:activate', => @activateInsertMode()
|
||||
@editor.on 'command-mode:activate', => @activateCommandMode()
|
||||
@editor.on 'command-mode:delete', => @delete()
|
||||
|
||||
activateInsertMode: ->
|
||||
@editor.removeClass('command-mode')
|
||||
@editor.addClass('insert-mode')
|
||||
|
||||
activateCommandMode: ->
|
||||
@editor.removeClass('insert-mode')
|
||||
@editor.addClass('command-mode')
|
||||
|
||||
delete: ->
|
||||
@editor.delete()
|
||||
Reference in New Issue
Block a user