Move cursor to end of completed word on confirm

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-04-19 12:32:44 -06:00
parent 905f91a179
commit 12a150b446
2 changed files with 17 additions and 13 deletions

View File

@@ -103,10 +103,11 @@ describe "Autocomplete", ->
expect(autocomplete.matchesList.find('li:eq(0)')).toHaveText('concat')
describe 'autocomplete:confirm event', ->
it 'replaces selection with selected match, removes autocomplete view and returns focus to editor', ->
it 'replaces selection with selected match, moves the cursor to the end of the match, and removes the autocomplete menu', ->
editor.buffer.insert([10,0] ,"extra:sort:extra")
editor.setSelectionBufferRange [[10,7], [10,10]]
editor.setSelectionBufferRange [[10,7], [10,9]]
editor.trigger "autocomplete:toggle"
editor.trigger "autocomplete:confirm"
expect(editor.lineForBufferRow(10)).toBe "extra:shift:extra"
@@ -258,22 +259,22 @@ describe "Autocomplete", ->
describe '.wordMatches(prefix, suffix)', ->
it 'returns wordMatches on buffer starting with given prefix and ending with given suffix', ->
wordMatches = autocomplete.wordMatches("s", "").map (match) -> match[0]
wordMatches = autocomplete.wordMatches("s", "").map (match) -> match.word
expect(wordMatches.length).toBe 2
expect(wordMatches).toContain("sort")
expect(wordMatches).toContain("shift")
wordMatches = autocomplete.wordMatches("l", "t").map (match) -> match[0]
wordMatches = autocomplete.wordMatches("l", "t").map (match) -> match.word
expect(wordMatches.length).toBe 1
expect(wordMatches).toContain("left")
it 'ignores case when finding matches', ->
wordMatches = autocomplete.wordMatches("S", "").map (match) -> match[0]
wordMatches = autocomplete.wordMatches("S", "").map (match) -> match.word
expect(wordMatches.length).toBe 2
expect(wordMatches).toContain("sort")
expect(wordMatches).toContain("shift")
wordMatches = autocomplete.wordMatches("l", "t").map (match) -> match[0]
wordMatches = autocomplete.wordMatches("l", "t").map (match) -> match.word
expect(wordMatches.length).toBe 1
expect(wordMatches).toContain("left")

View File

@@ -42,7 +42,9 @@ class Autocomplete extends View
confirm: ->
@editor.getSelection().clearSelection()
@detach()
@editor.focus()
match = @selectedMatch()
position = @editor.getCursorBufferPosition()
@editor.setCursorBufferPosition([position.row, position.column + match.suffix.length])
cancel: ->
@detach()
@@ -122,12 +124,11 @@ class Autocomplete extends View
return
currentWord = prefix + @editor.getSelectedText() + suffix
@matches = (match for match in @wordMatches(prefix, suffix) when match[0] != currentWord)
@matches = (match for match in @wordMatches(prefix, suffix) when match.word != currentWord)
@matchesList.empty()
if @matches.length > 0
@matchesList.append($$ -> @li match[0]) for match in @matches
@matchesList.append($$ -> @li match.word) for match in @matches
else
@matchesList.append($$ -> @li "No matches found")
@@ -138,14 +139,16 @@ class Autocomplete extends View
wordMatches: (prefix, suffix) ->
regex = new RegExp("^#{prefix}(.+)#{suffix}$", "i")
regex.exec(word) for word in @wordList when regex.test(word)
for word in @wordList when regex.test(word)
match = regex.exec(word)
{prefix, suffix, word, infix: match[1]}
completeUsingMatch: (match) ->
selection = @editor.getSelection()
startPosition = selection.getBufferRange().start
@isAutocompleting = true
@editor.insertText(match[1])
@editor.setSelectionBufferRange([startPosition, [startPosition.row, startPosition.column + match[1].length]])
@editor.insertText(match.infix)
@editor.setSelectionBufferRange([startPosition, [startPosition.row, startPosition.column + match.infix.length]])
@currentSelectionBufferRange = @editor.getSelection().getBufferRange()
@isAutocompleting = false