Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/view-models/search-view-model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class SearchViewModel extends ViewModel
super(@searchMotion, class: 'search')
@historyIndex = -1

@view.editor.on('core:move-up', @increaseHistorySearch)
@view.editor.on('core:move-down', @decreaseHistorySearch)
atom.commands.add(@view.editorElement, 'core:move-up', @increaseHistorySearch)
atom.commands.add(@view.editorElement, 'core:move-down', @decreaseHistorySearch)

restoreHistory: (index) ->
@view.editor.setText(@history(index).value)
@view.editorElement.getModel().setText(@history(index).value)

history: (index) ->
@vimState.getSearchHistoryItem(index)
Expand All @@ -24,7 +24,7 @@ class SearchViewModel extends ViewModel
if @historyIndex <= 0
# get us back to a clean slate
@historyIndex = -1
@view.editor.setText('')
@view.editorElement.getModel().setText('')
else
@historyIndex -= 1
@restoreHistory(@historyIndex)
Expand Down
39 changes: 17 additions & 22 deletions lib/view-models/vim-command-mode-input-view.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{View, TextEditorView} = require 'atom'
{View} = require 'atom'

module.exports =
class VimCommandModeInputView extends View
@content: ->
@div class: 'command-mode-input', =>
@div class: 'editor-container', outlet: 'editorContainer', =>
@subview 'editor', new TextEditorView(mini: true)
@div class: 'editor-container', outlet: 'editorContainer'

initialize: (@viewModel, opts = {})->
if opts.class?
@editorContainer.addClass opts.class

if opts.hidden
@editorContainer.addClass 'hidden-input'
@editorContainer.height(0)

@editorElement = document.createElement "atom-text-editor"
@editorElement.classList.add('editor')
@editorElement.getModel().setMini(true)
@editorContainer.append(@editorElement)

@singleChar = opts.singleChar
@defaultText = opts.defaultText ? ''
Expand All @@ -24,35 +28,26 @@ class VimCommandModeInputView extends View

handleEvents: ->
if @singleChar?
@editor.find('input').on 'textInput', @autosubmit
@editor.on 'core:confirm', @confirm
@editor.on 'core:cancel', @cancel
@editor.find('input').on 'blur', @cancel

stopHandlingEvents: ->
if @singleChar?
@editor.find('input').off 'textInput', @autosubmit
@editor.off 'core:confirm', @confirm
@editor.off 'core:cancel', @cancel
@editor.find('input').off 'blur', @cancel

autosubmit: (event) =>
@editor.setText(event.originalEvent.data)
@confirm()
@editorElement.getModel().getBuffer().onDidChange (e) =>
@confirm() if e.newText
else
atom.commands.add(@editorElement, 'editor:newline', @confirm)
atom.commands.add(@editorElement, 'core:confirm', @confirm)
atom.commands.add(@editorElement, 'core:cancel', @cancel)
atom.commands.add(@editorElement, 'blur', @cancel)

confirm: =>
@value = @editor.getText() or @defaultText
@value = @editorElement.getModel().getText() or @defaultText
@viewModel.confirm(@)
@remove()

focus: =>
@editorContainer.find('.editor').focus()
@editorElement.focus()

cancel: (e) =>
@viewModel.cancel(@)
@remove()

remove: =>
@stopHandlingEvents()
atom.workspace.getActivePane().activate()
@panel.destroy()
95 changes: 42 additions & 53 deletions spec/motions-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ describe "Motions", ->
helpers.keydown(key, options)

commandModeInputKeydown = (key, opts = {}) ->
opts.element = editor.commandModeInputView.editor.find('input').get(0)
opts.raw = true
keydown(key, opts)
editor.commandModeInputView.editorElement.getModel().setText(key)

submitCommandModeInputText = (text) ->
commandEditor = editor.commandModeInputView.editorElement
commandEditor.getModel().setText(text)
atom.commands.dispatch(commandEditor, "core:confirm")

describe "simple motions", ->
beforeEach ->
Expand Down Expand Up @@ -966,25 +969,22 @@ describe "Motions", ->
it "moves the cursor to the specified search pattern", ->
keydown('/')

editor.commandModeInputView.editor.setText 'def'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'def'

expect(editor.getCursorBufferPosition()).toEqual [1, 0]
expect(pane.activate).toHaveBeenCalled()

it "loops back around", ->
editor.setCursorBufferPosition([3, 0])
keydown('/')
editor.commandModeInputView.editor.setText 'def'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'def'

expect(editor.getCursorBufferPosition()).toEqual [1, 0]

it "uses a valid regex as a regex", ->
keydown('/')
# Cycle through the 'abc' on the first line with a character pattern
editor.commandModeInputView.editor.setText '[abc]'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText '[abc]'
expect(editor.getCursorBufferPosition()).toEqual [0, 1]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [0, 2]
Expand All @@ -993,8 +993,7 @@ describe "Motions", ->
# Go straight to the literal [abc
editor.setText("abc\n[abc]\n")
keydown('/')
editor.commandModeInputView.editor.setText '[abc'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText '[abc'
expect(editor.getCursorBufferPosition()).toEqual [1, 0]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [1, 0]
Expand All @@ -1003,8 +1002,7 @@ describe "Motions", ->
editor.setText('one two three')
keydown('v')
keydown('/')
editor.commandModeInputView.editor.setText 'th'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'th'
expect(editor.getCursorBufferPosition()).toEqual [0, 9]
keydown('d')
expect(editor.getText()).toBe 'hree'
Expand All @@ -1013,8 +1011,7 @@ describe "Motions", ->
editor.setText('line1\nline2\nline3')
keydown('v')
keydown('/')
editor.commandModeInputView.editor.setText 'line'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'line'
{start, end} = editor.getSelectedBufferRange()
expect(start.row).toEqual 0
expect(end.row).toEqual 1
Expand All @@ -1030,38 +1027,33 @@ describe "Motions", ->
keydown('/')

it "works in case sensitive mode", ->
editor.commandModeInputView.editor.setText 'ABC'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'ABC'
expect(editor.getCursorBufferPosition()).toEqual [2, 0]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [2, 0]

it "works in case insensitive mode", ->
editor.commandModeInputView.editor.setText '\\cAbC'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText '\\cAbC'
expect(editor.getCursorBufferPosition()).toEqual [1, 0]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [2, 0]

it "works in case insensitive mode wherever \\c is", ->
editor.commandModeInputView.editor.setText 'AbC\\c'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'AbC\\c'
expect(editor.getCursorBufferPosition()).toEqual [1, 0]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [2, 0]

it "uses case insensitive search if useSmartcaseForSearch is true and searching lowercase", ->
atom.config.set 'vim-mode.useSmartcaseForSearch', true
editor.commandModeInputView.editor.setText 'abc'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'abc'
expect(editor.getCursorBufferPosition()).toEqual [1, 0]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [2, 0]

it "uses case sensitive search if useSmartcaseForSearch is true and searching uppercase", ->
atom.config.set 'vim-mode.useSmartcaseForSearch', true
editor.commandModeInputView.editor.setText 'ABC'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'ABC'
expect(editor.getCursorBufferPosition()).toEqual [2, 0]
keydown('n')
expect(editor.getCursorBufferPosition()).toEqual [2, 0]
Expand All @@ -1073,8 +1065,7 @@ describe "Motions", ->

beforeEach ->
keydown('/')
editor.commandModeInputView.editor.setText 'def'
editor.commandModeInputView.editor.trigger 'core:confirm'
submitCommandModeInputText 'def'

describe "the n keybinding", ->
it "repeats the last search", ->
Expand All @@ -1093,31 +1084,27 @@ describe "Motions", ->
it "composes with operators", ->
keydown('d')
keydown('/')
editor.commandModeInputView.editor.setText('def')
editor.commandModeInputView.editor.trigger('core:confirm')
submitCommandModeInputText('def')
expect(editor.getText()).toEqual "def\nabc\ndef\n"

it "repeats correctly with operators", ->
keydown('d')
keydown('/')
editor.commandModeInputView.editor.setText('def')
editor.commandModeInputView.editor.trigger('core:confirm')
submitCommandModeInputText('def')

keydown('.')
expect(editor.getText()).toEqual "def\n"

describe "when reversed as ?", ->
it "moves the cursor backwards to the specified search pattern", ->
keydown('?')
editor.commandModeInputView.editor.setText('def')
editor.commandModeInputView.editor.trigger('core:confirm')
submitCommandModeInputText('def')
expect(editor.getCursorBufferPosition()).toEqual [3, 0]

describe "repeating", ->
beforeEach ->
keydown('?')
editor.commandModeInputView.editor.setText('def')
editor.commandModeInputView.editor.trigger('core:confirm')
submitCommandModeInputText('def')

describe 'the n keybinding', ->
it "repeats the last search backwards", ->
Expand All @@ -1132,36 +1119,38 @@ describe "Motions", ->
expect(editor.getCursorBufferPosition()).toEqual [1, 0]

describe "using search history", ->
commandEditor = null

beforeEach ->
keydown('/')
editor.commandModeInputView.editor.setText('def')
editor.commandModeInputView.editor.trigger('core:confirm')
submitCommandModeInputText('def')
expect(editor.getCursorBufferPosition()).toEqual [1, 0]

keydown('/')
editor.commandModeInputView.editor.setText('abc')
editor.commandModeInputView.editor.trigger('core:confirm')
submitCommandModeInputText('abc')
expect(editor.getCursorBufferPosition()).toEqual [2, 0]

commandEditor = editor.commandModeInputView.editorElement

it "allows searching history in the search field", ->
keydown('/')
editor.commandModeInputView.editor.trigger('core:move-up')
expect(editor.commandModeInputView.editor.getText()).toEqual('abc')
editor.commandModeInputView.editor.trigger('core:move-up')
expect(editor.commandModeInputView.editor.getText()).toEqual('def')
editor.commandModeInputView.editor.trigger('core:move-up')
expect(editor.commandModeInputView.editor.getText()).toEqual('def')
atom.commands.dispatch(commandEditor, 'core:move-up')
expect(commandEditor.getModel().getText()).toEqual('abc')
atom.commands.dispatch(commandEditor, 'core:move-up')
expect(commandEditor.getModel().getText()).toEqual('def')
atom.commands.dispatch(commandEditor, 'core:move-up')
expect(commandEditor.getModel().getText()).toEqual('def')

it "resets the search field to empty when scrolling back", ->
keydown('/')
editor.commandModeInputView.editor.trigger('core:move-up')
expect(editor.commandModeInputView.editor.getText()).toEqual('abc')
editor.commandModeInputView.editor.trigger('core:move-up')
expect(editor.commandModeInputView.editor.getText()).toEqual('def')
editor.commandModeInputView.editor.trigger('core:move-down')
expect(editor.commandModeInputView.editor.getText()).toEqual('abc')
editor.commandModeInputView.editor.trigger('core:move-down')
expect(editor.commandModeInputView.editor.getText()).toEqual ''
atom.commands.dispatch(commandEditor, 'core:move-up')
expect(commandEditor.getModel().getText()).toEqual('abc')
atom.commands.dispatch(commandEditor, 'core:move-up')
expect(commandEditor.getModel().getText()).toEqual('def')
atom.commands.dispatch(commandEditor, 'core:move-down')
expect(commandEditor.getModel().getText()).toEqual('abc')
atom.commands.dispatch(commandEditor, 'core:move-down')
expect(commandEditor.getModel().getText()).toEqual ''

describe "the * keybinding", ->
beforeEach ->
Expand Down
6 changes: 2 additions & 4 deletions spec/operators-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ describe "Operators", ->
helpers.keydown(key, options)

commandModeInputKeydown = (key, opts = {}) ->
opts.element = editor.commandModeInputView.editor.find('input').get(0)
opts.raw = true
keydown(key, opts)
editor.commandModeInputView.editorElement.getModel().setText(key)

describe "cancelling operations", ->
it "does not throw an error even if no operation is pending", ->
Expand Down Expand Up @@ -1241,7 +1239,7 @@ describe "Operators", ->

it "replaces a single character with a line break", ->
keydown('r')
editor.commandModeInputView.editor.trigger 'core:confirm'
atom.commands.dispatch(editor.commandModeInputView.editorElement, 'core:confirm')
expect(editor.getText()).toBe '\n2\n\n4\n\n'
expect(editor.getCursorBufferPositions()).toEqual [[1, 0], [3, 0]]

Expand Down
4 changes: 1 addition & 3 deletions spec/text-objects-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ describe "TextObjects", ->
helpers.keydown(key, options)

commandModeInputKeydown = (key, opts = {}) ->
opts.element = editor.commandModeInputView.editor.find('input').get(0)
opts.raw = true
keydown(key, opts)
editor.commandModeInputView.editorElement.getModel().setText(key)

describe "the 'iw' text object", ->
beforeEach ->
Expand Down
6 changes: 2 additions & 4 deletions spec/vim-state-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ describe "VimState", ->
helpers.keydown(key, options)

commandModeInputKeydown = (key, opts = {}) ->
opts.element = editor.commandModeInputView.editor.find('input').get(0)
opts.raw = true
keydown(key, opts)
editor.commandModeInputView.editorElement.getModel().setText(key)

describe "initialization", ->
it "puts the editor in command-mode initially by default", ->
Expand Down Expand Up @@ -152,7 +150,7 @@ describe "VimState", ->
keydown('r')
expect(vimState.mode).toBe 'command'
expect(vimState.opStack.length).toBe 0
commandModeInputKeydown('escape')
atom.commands.dispatch(editor.commandModeInputView.editorElement, "core:cancel")
keydown('d')
expect(editor.getText()).toBe '012345\nabcdef'

Expand Down