diff --git a/spec/app/autocomplete-spec.coffee b/spec/app/autocomplete-spec.coffee index b52aa4839..56c953d36 100644 --- a/spec/app/autocomplete-spec.coffee +++ b/spec/app/autocomplete-spec.coffee @@ -237,6 +237,31 @@ describe "Autocomplete", -> miniEditor.trigger 'move-down' expect(matchesList.scrollTop()).toBe 0 + describe "when a match is clicked in the match list", -> + it "selects and confirms the match", -> + editor.buffer.insert([10,0] ,"t") + editor.setCursorBufferPosition([10, 0]) + autocomplete.attach() + + matchToSelect = autocomplete.matchesList.find('li:eq(1)') + matchToSelect.mousedown() + expect(matchToSelect).toMatchSelector('.selected') + matchToSelect.mouseup() + + expect(autocomplete.parent()).not.toExist() + expect(editor.lineForBufferRow(10)).toBe matchToSelect.text() + + it "cancels the autocomplete when clicking on the 'No matches found' li", -> + editor.buffer.insert([10,0] ,"t") + editor.setCursorBufferPosition([10, 0]) + autocomplete.attach() + + miniEditor.insertText('xxx') + autocomplete.matchesList.find('li').mousedown().mouseup() + + expect(autocomplete.parent()).not.toExist() + expect(editor.lineForBufferRow(10)).toBe "t" + describe "when the mini-editor receives keyboard input", -> describe "when text is removed from the mini-editor", -> it "reloads the match list based on the mini-editor's text", -> diff --git a/src/app/autocomplete.coffee b/src/app/autocomplete.coffee index 81dc6b08d..ea6a5aeaa 100644 --- a/src/app/autocomplete.coffee +++ b/src/app/autocomplete.coffee @@ -8,7 +8,7 @@ fuzzyFilter = require 'fuzzy-filter' module.exports = class Autocomplete extends View @content: -> - @div class: 'autocomplete', => + @div class: 'autocomplete', tabindex: -1, => @ol outlet: 'matchesList' @subview 'miniEditor', new Editor(mini: true) @@ -42,6 +42,17 @@ class Autocomplete extends View @editor.on 'autocomplete:cancel', => @cancel() @on 'autocomplete:confirm', => @confirm() + @matchesList.on 'mousedown', (e) => + index = $(e.target).attr('index') + @selectMatchAtIndex(index) if index? + false + + @matchesList.on 'mouseup', => + if @selectedMatch() + @confirm() + else + @cancel() + @miniEditor.buffer.on 'change', (e) => @filterMatches() if @parent()[0] @@ -138,7 +149,7 @@ class Autocomplete extends View renderMatchList: -> @matchesList.empty() if @filteredMatches.length > 0 - @matchesList.append($$ -> @li match.word) for match in @filteredMatches + @matchesList.append($$ -> @li match.word, index: index) for match, index in @filteredMatches else @matchesList.append($$ -> @li "No matches found")