Add move-down event to Autocomplete

This commit is contained in:
Corey Johnson 2012-04-17 17:30:31 -07:00
parent f8fd7251e4
commit 904c05660d
2 changed files with 24 additions and 0 deletions

View File

@ -78,6 +78,24 @@ describe "Autocomplete", ->
expect(editor.getCursorBufferPosition()).toEqual [5,11]
expect(editor.getSelection().getBufferRange()).toEqual [[5,7], [5,11]]
describe 'move-up event', ->
it 'replaces selection with previous match', ->
editor.buffer.insert([10,0] ,"extra:t:extra")
editor.setCursorBufferPosition([10,6])
autocomplete.trigger "autocomplete:toggle"
autocomplete.trigger "move-up"
expect(editor.lineForBufferRow(10)).toBe "extra:concat:extra"
expect(autocomplete.find('li:eq(0)')).not.toHaveClass('selected')
expect(autocomplete.find('li:eq(1)')).not.toHaveClass('selected')
expect(autocomplete.find('li:eq(7)')).toHaveClass('selected')
autocomplete.trigger "move-up"
expect(editor.lineForBufferRow(10)).toBe "extra:right:extra"
expect(autocomplete.find('li:eq(0)')).not.toHaveClass('selected')
expect(autocomplete.find('li:eq(7)')).not.toHaveClass('selected')
expect(autocomplete.find('li:eq(6)')).toHaveClass('selected')
describe 'move-down event', ->
it 'replaces selection with next match', ->
editor.buffer.insert([10,0] ,"extra:s:extra")

View File

@ -20,6 +20,7 @@ class Autocomplete extends View
initialize: (@editor) ->
requireStylesheet 'autocomplete.css'
@on 'autocomplete:toggle', => @toggle()
@on 'move-up', => @previousMatch()
@on 'move-down', => @nextMatch()
@editor.on 'buffer-path-change', => @setCurrentBuffer(@editor.buffer)
@ -45,6 +46,11 @@ class Autocomplete extends View
@css {left: left, top: top + @editor.lineHeight}
$(document.body).append(this)
previousMatch: ->
previousIndex = @currentMatchIndex - 1
previousIndex = @matches.length - 1 if previousIndex < 0
@selectMatch(previousIndex)
nextMatch: ->
nextIndex = (@currentMatchIndex + 1) % @matches.length
@selectMatch(nextIndex)